user872009
user872009

Reputation: 438

How to get specific lines in a text file

I am trying read the contents of a text file. The idea is to get the first line with the 'title :' keyword, read the file, get the next 'title:' keyword again, keep doing it until the file is read. I am trying to store it in a database. Other ideas to do this are welcomed as well. Thanks.

This is the text file I am trying to read from.

title : Mothers Day

mattiebelle : YEA! A movie that grabbed me from beginning to end! Love to come across this kind of movie. A must see for all! Enjoy!

title : Pregnant in Heels

CuittePie : I CAN'T WATCH ANY OF THEES. :@

title : The Flintstones Row_Sweet_Girl : Nice one to watch

title : Barter Kings dragon3476 : Barter Kings - Season 1 Episode 4 - Rock and a Hard Place Air Date: 19/06/2012 Summary:Traders barter for a car and a pool table.

Upvotes: 0

Views: 535

Answers (1)

Francisco Spaeth
Francisco Spaeth

Reputation: 23903

I think the easiest way would be using FileUtils from Apache Commons IO like this:

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.apache.commons.io.FileUtils;

public class ReadFileLines {

    public static void main(final String[] args) throws IOException {
        List lines = FileUtils.readLines(new File("/tmp/myFile.txt"), "UTF-8");
        for (Object line : lines) {
            if (String.valueOf(line).startsWith("title : ")) {
                System.out.println(line); // here you store it
            }
        }
    }

}

Upvotes: 2

Related Questions