Alex
Alex

Reputation: 57

What am I doing wrong loading a .txt file?

try (BufferedReader br = new BufferedReader(new FileReader("Templates/format/test.txt")))
{

    String sCurrentLine;

    while ((sCurrentLine = br.readLine()) != null) {
        System.out.println(sCurrentLine.toString().trim());
        if(sCurrentLine.toString().trim().equalsIgnoreCase("Test2")){
            System.out.println("HI: "+sCurrentLine.toString().trim());
        }

    }

} catch (IOException e) {
    e.printStackTrace();
} 

I'm trying the get the contents of a .txt but neither

if(sCurrentLine.toString().trim().equalsIgnoreCase("Test2")){
    System.out.println("HI: "+sCurrentLine.toString().trim());
}

or

if(sCurrentLine.toString().trim().equals("Test2")){
    System.out.println("HI: "+sCurrentLine.toString().trim());
}

works.

This is the content of the txt:

Text1
Text2
Text3

I also don't understand why this: System.out.println(sCurrentLine.toString().trim()); leads to the following output in the console. Why do I get breaks and the Symbol at the beginning?

þÿ T e s t 1
T e s t 2
T e s t 3

Thanks for helping me out here!

Upvotes: 0

Views: 353

Answers (1)

alan.sambol
alan.sambol

Reputation: 265

Convert your .txt file to UTF-8 without BOM. You can do that with Notepad++ or any other advanced text editor.

Upvotes: 1

Related Questions