-1

This is my code. What's going on with Java BufferedReader's readline() read result as null?

FileReader fr = new FileReader("D:\\db.txt");
BufferedReader br = new BufferedReader(fr);
String str =br.readLine(); 
Hakan Dilek
  • 2,178
  • 2
  • 23
  • 35
siyuan
  • 1
  • 2
  • Does this answer your question? [Read special characters in java with BufferedReader](https://stackoverflow.com/questions/9281629/read-special-characters-in-java-with-bufferedreader) – smoggers Jul 09 '20 at 10:19
  • 1
    @smoggers Where did you get the part about special characters from? – Thilo Jul 09 '20 at 10:32
  • What are the contents of the file? – Thilo Jul 09 '20 at 10:32
  • @Thilo originally the question featured several unknown chars like you would see with encoding scheme issues – smoggers Jul 09 '20 at 10:39
  • 2
    Before asking here on StackOverflow you should take a look at the documentation: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/io/BufferedReader.html#readLine() – vanje Jul 09 '20 at 12:53

2 Answers2

1

BufferedReader.readLine() returns null if the end of the stream has been reached. In your case the file is empty. If the path to the file would be wrong the constructor of FileReader would throw a FileNotFoundException .

rmunge
  • 3,653
  • 5
  • 19
-1

I recommend you to get the actual file location and copy that path, then replace all the \ with /. then paste it to your code.

KMS
  • 68
  • 9