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 7 of 7

Thread: Problem with Reading very long line using bufferedReader

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Problem with Reading very long line using bufferedReader

    I am reading file line by line using bufferedReader in java.
    My file contains very long lines (about 913434 characters per line).

    FileInputStream inStream=new FileInputStream(inFile);
    DataInputStream inDataStream=new DataInputStream(inStream);
    BufferedReader br=new BufferedReader(new InputStreamReader(inDataStream));

    String str=null;
    while ((str = br.readLine()) !=null){
    System.out.println(str);
    }

    But it's always give some portion of line . It's failed to read whole line.
    Please , help me on How to read whole line ?

    Thanks,


  2. #2

    Default Re: Problem with Reading very long line using bufferedReader

    Try using a scanner instead:

    Scanner scanner = new Scanner(new File(inFile));
     
    while(scanner.hasNextLine())
    {
    System.out.println(scanner.nextLine());
    }
    Kenneth Walter
    Software Developer
    http://kennywalter.com

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem with Reading very long line using bufferedReader

    It's failed to read whole line.
    How much of the line does it read?
    Does it read the same amount every time?

  4. #4

    Default Re: Problem with Reading very long line using bufferedReader

    Apparently, BufferedReader is better for reading line streams from a text file and the limit for strings is 2 billion characters. My suggesstion: read the file one character at a time...
    Kenneth Walter
    Software Developer
    http://kennywalter.com

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem with Reading very long line using bufferedReader

    This test program seems to write and read 913434 bytes with no problem.
        String inFile = "LargeFile.txt";   // the file name 
        FileOutputStream fos = new FileOutputStream(inFile);
        for(int i=0; i < 913434; i++) {
          fos.write('A');   // Write all 'A's to the file
        }
        fos.close();
        FileInputStream inStream = new FileInputStream(inFile);
        DataInputStream inDataStream = new DataInputStream(inStream);
        BufferedReader br = new BufferedReader(new InputStreamReader(inDataStream));
     
        String str = null;
        while ((str = br.readLine()) != null){
          System.out.println("len=" + str.length());  // len=913434
        }

    How do you know that there is no lineend in the file?
    What bytes are in the file at the location where readLine() stops reading a line?

  6. #6
    Junior Member
    Join Date
    Sep 2011
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Problem with Reading very long line using bufferedReader

    Sorry @all, :-)
    Problem is just because of i forgot to close FileWriter object.....
    Now its working correctly....
    Last edited by nicool; November 7th, 2011 at 01:35 PM.

  7. #7
    Junior Member
    Join Date
    Sep 2011
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Problem with Reading very long line using bufferedReader

    Thanks for your suggestions

Similar Threads

  1. Replies: 2
    Last Post: November 6th, 2011, 02:33 PM
  2. [SOLVED] Reading a file with bufferedreader
    By ppata in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: November 20th, 2010, 12:20 PM
  3. The input line is too long
    By vivekmk in forum Java IDEs
    Replies: 5
    Last Post: October 16th, 2009, 10:35 PM
  4. How to Read a file line by line using BufferedReader?
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: May 19th, 2008, 06:32 AM
  5. How to Read a file line by line using BufferedReader?
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: May 19th, 2008, 06:32 AM