As a beginner, I would like to read a text file that contains aproximately 1.500 lines of plain text.
So I created a text file (Code_A) with 1.500 lines of text. If I open the text file I notice that the file contains the 1.500 lines of text.
Subsequently, If I read the text file (Code_B) then only the lines 990 to 1.500 are displayed. The lines 1 to 899 are not displayed.
I have tried different posibilities to read the data eg:
BufferedReader
FileReader
scanner
String
They all produces the same problem and display only the last lines from 900 to 1.500.
I have no idea why only the lines 900-1.500 are displayed. Can someone point me to the right direction ??
Thanks
Code_A to write the text file
try { FileWriter writer = new FileWriter("MyFile.txt", true); BufferedWriter bufferedWriter = new BufferedWriter(writer); for(i= 1 ; i < 1500; i++){ writer.write(i + " Hello World this is my first JAVA test."); writer.write("\r\n"); // write new line } bufferedWriter.close(); } catch (IOException e) { e.printStackTrace(); }
Code_B to read the text file
try { FileReader reader = new FileReader("MyFile.txt"); BufferedReader bufferedReader = new BufferedReader(reader); String line; while ((line = bufferedReader.readLine()) != null) { System.out.println(line); /* only lines 900-1500 are displayed */ } reader.close(); } catch (IOException e) { e.printStackTrace(); }