0

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.

1
  • So if I understand correctly, what you want to do is read a file, and skip lines until you see "title :" at the front of the line? What have you tried? Commented Jun 21, 2012 at 17:27

1 Answer 1

2

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
            }
        }
    }

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.