Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Weared behaviour during reading a simple text file

  1. #1
    Junior Member
    Join Date
    Oct 2020
    Location
    The Netherlands
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Weared behaviour during reading a simple text file

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

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Weared behaviour during reading a simple text file

    Can you post code that compiles and executes for testing? The posted code is missing the import statements and the class and method declarations.

    What is the purpose of the BufferedWriter? The code uses the FileWriter class to write the file. Try removing the BufferedWriter usage or use it to write the file.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. reading i n a text file
    By zerocool18 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 24th, 2014, 04:38 PM
  2. Reading from a text file into an ArrayList
    By Spanky_10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 17th, 2013, 01:24 AM
  3. How do I copy a simple text-file to a new text-file?
    By get703 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: December 5th, 2012, 10:59 PM
  4. Not Reading From Text File
    By JavaLaxer15 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 18th, 2012, 11:16 AM
  5. simple program for reading text file
    By johnpipes in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 21st, 2011, 05:55 PM