Kirou Oreto
Kirou Oreto

Reputation: 3

How to get a text file read line by line?

I am creating an application which is text file reader but It reads the text file content line by line and then if a reserved word is red a function will execute. For example I have a text file that contains the reserve word [PlaySound]+ sound file on its first line, when the reader reads the line a function will execute and plays the music file that is with the reserve word.

So is it possible to create this? and if it is how can i make the line reader?

Upvotes: 0

Views: 763

Answers (1)

Web User
Web User

Reputation: 7736

File file = new File("inputFile.txt");
Scanner sc = null;
try {
    sc = new Scanner(file);
    String line = null;
    while (sc.hasNextLine()) {

        String soundFile = null;
        line = sc.nextLine();
        if (line.indexOf("[PlaySound]") >= 0) {
            soundFile = // some regex to extract the sound file from the line
            playSoundFile(soundFile);
        }
    }
    sc.close();
} 
catch (FileNotFoundException e) {
    e.printStackTrace();
}
finally {
  if (sc != null) { sc.close(); }
}

Upvotes: 1

Related Questions