0

I'm using a common way to read text file in java:

public String readIndex() throws IOException{
        if(!fileExisits)
            return "";
        String indexInFile = "";
        while((indexInFile = reader.readLine())!=null){
            indexInFile += reader.readLine();
            System.out.println("indexxxxxxxx: " + indexInFile);
        }
        System.out.println("reader get: " + indexInFile);
        return indexInFile;
    }

and the output is : is file :: true

indexxxxxxxx: 1fefefe\fefeef
indexxxxxxxx: effe
indexxxxxxxx: effe
indexxxxxxxx: null
reader get: null
null

as you can see in the last line the output String indexInFile is being set to null. my reader is:

reader = new BufferedReader(new FileReader(fileName));

any suggestions why it happens? hopes that I write all the relevant code.

4 Answers 4

2

When the last line is encountered the following reader.readLine() will return zero. Then the condition of the while loop, the assignment indexInFile=null will be called. The while loop will exit because (indexInFile=null)==null).

Also, you can possibly have a null inside the for loop since you call reader.readLine() there. The following code will fix your problem, I think:

public String readIndex() throws IOException{
    if(!fileExisits)
        return "";
    String line;
    String indexInFile = "";
    while((line = reader.readLine())!=null){
        indexInFile += line;
        System.out.println("indexxxxxxxx: " + line);
    }
    System.out.println("reader get: " + indexInFile);
    return indexInFile;
}
Sign up to request clarification or add additional context in comments.

2 Comments

@ItsikMauyhas why would you want to solve this? It seems to me that it is the intended behaviour, unless you would like to have the last line in the file?
@ItsikMauyhas I saw another problem in your code. I updated my anwser accordingly
1

Get rid of the second readLine():

public String readIndex() throws IOException{
    if(!fileExisits)
        return "";
    String indexInFile = "";
    while((indexInFile = reader.readLine())!=null){
        System.out.println("indexxxxxxxx: " + indexInFile);
    }
    System.out.println("reader get: " + indexInFile);
    return indexInFile;
}

Your already reading the line in your while condition. So when you reach the last line of text you are going to right away read the nextLine again before it can break out of the loop which will return null because there are no more lines. That is why null is being printed.

Comments

0

You are calling readLine twice per loop step. Also readLine strips the trailing newline char(s).

    StringBuilder totalText = new StringBuilder();

    String line;
    while ((line = reader.readLine()) != null) {
        totalText.append(line).append("\r\n");
        System.out.println("Line: " + line);
    }
    System.out.println("All: " + totalText.toString());

Using StringBuilder will greatly improve the speed.

Comments

0

Use the method reader.ready() to check (So you will also get rid of the doubled readLine())

public String readIndex() throws IOException{
    if(!fileExisits)
        return "";
    String indexInFile = "";
    while(reader.ready()){
        indexInFile += reader.readLine();
        System.out.println("indexxxxxxxx: " + indexInFile);
    }
    System.out.println("reader get: " + indexInFile);
    return indexInFile;
}

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.