简体   繁体   中英

bufferedReader.readline is returning null

just a really quick question i have a file in AFC/save.txt which has this in it

peter

now I use this code in Java and it returns null, any idea why?

//Android
try {
        InputStream fis = game.getFileIO().readFile("AFC/save.txt");
        BufferedReader br = new BufferedReader(new InputStreamReader(fis));
        if(br.readLine() != null)
        {
                            Log.d("File", "Value : " + br.readLine() );
            player = br.readLine();
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

the value is null.

Which value is null?

At if(br.readLine() != null) you are reading in the first line of the file.

At Log.d("File", "Value : " + br.readLine() ); you are at the second line of the file.

At player = br.readLine(); you are reading the third line of the file. If there is only one line in the file, this line will return null.

Try:

try {
    String temp;
    InputStream fis = game.getFileIO().readFile("AFC/save.txt");
    BufferedReader br = new BufferedReader(new InputStreamReader(fis));
    if((temp = br.readLine()) != null)
    {
          player = temp;
          Log.d("File", "Value : " + player );
    }
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM