Im trying to read a whole text file with a method i created. All the lines of the text file does get printed out as i wanted but the very last line of the file when its printed appears as null when its printed out.
private void readFile(String Path) throws IOException{
String text = ""; //String used in the process of reading a file
//The file reader
BufferedReader input = new BufferedReader(
new FileReader(Path));
//Creating a new string builder.
StringBuilder stringBuilder = new StringBuilder();
while(text != null)
{
//Read the next line
text = input.readLine();
stringBuilder.append(text); //Adds line of text into the String Builder
stringBuilder.append(newLine); //Adds a new line using the newLine string
}
//Sets the text that was created with the stringBuilder
SetText(stringBuilder.toString());
}
All the files gets printed out 100% as they should except the method adds an extra line at the bottom that says “null” How would i write code so this line wont appear at all?
Your loop exit condition is in the wrong place.