Itsik Mauyhas
Itsik Mauyhas

Reputation: 3984

java reader ends String with null

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.

Upvotes: 0

Views: 148

Answers (4)

mad_manny
mad_manny

Reputation: 1091

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

Upvotes: 0

Joop Eggen
Joop Eggen

Reputation: 109547

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.

Upvotes: 0

brso05
brso05

Reputation: 13222

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.

Upvotes: 1

Niels Billen
Niels Billen

Reputation: 2199

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

Upvotes: 2

Related Questions