6 Replies - 722 Views - Last Post: 07 November 2011 - 11:33 AM Rate Topic: -----

#1 nicool  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 23
  • Joined: 12-August 11

Problem with Reading very long line using bufferedReader

Posted 07 November 2011 - 03:22 AM

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,

This post has been edited by smohd: 07 November 2011 - 03:45 AM
Reason for edit:: code tags added


Is This A Good Question/Topic? 0
  • +

Replies To: Problem with Reading very long line using bufferedReader

#2 Veitch  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 27
  • View blog
  • Posts: 59
  • Joined: 05-November 11

Re: Problem with Reading very long line using bufferedReader

Posted 07 November 2011 - 03:28 AM

Maybe it helps to specifiy a bigger buffer size:
new BufferedReader(new InputStreamReader(inDataStream), size)

Was This Post Helpful? 1
  • +
  • -

#3 nicool  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 23
  • Joined: 12-August 11

Re: Problem with Reading very long line using bufferedReader

Posted 07 November 2011 - 03:30 AM

I tried this also but no any effect.....
Was This Post Helpful? 0
  • +
  • -

#4 Veitch  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 27
  • View blog
  • Posts: 59
  • Joined: 05-November 11

Re: Problem with Reading very long line using bufferedReader

Posted 07 November 2011 - 04:55 AM

Are you sure that your console is not just breaking the line itself?
Are you sure that you have no carriage return or linefeed in the line?
Was This Post Helpful? 1
  • +
  • -

#5 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2111
  • View blog
  • Posts: 8,789
  • Joined: 20-September 08

Re: Problem with Reading very long line using bufferedReader

Posted 07 November 2011 - 05:37 AM

Please post example file
Was This Post Helpful? 1
  • +
  • -

#6 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 1504
  • View blog
  • Posts: 3,216
  • Joined: 11-December 07

Re: Problem with Reading very long line using bufferedReader

Posted 07 November 2011 - 05:45 AM

I tried this which generates a file with a few line a million characters long. Then it reads them using your method and prints the line lengths. It works fine. Maybe there is something up with your file or maybe veitch is right that the console isn't displaying the lines correctly.

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;


public class BigLineReader {
	
	public static void main(String[] args) throws IOException {
		BigLineReader test = new BigLineReader();
		File f = test.createFile();
		test.analyseFile(f);
		
	}

	File createFile() throws IOException {
		File f = File.createTempFile("BigFileTest", ".tmp");
		f.deleteOnExit();
		String str = bigString(1000000);
		PrintWriter out = new PrintWriter(f);
		for(int i = 0; i < 10; i++) {
			out.println(str);
		}
		out.close();
		return f;
	}

	String bigString(int size) {
		StringBuilder b = new StringBuilder(size);
		for(int i = 0; i < size; i++) {
			b.append(i%10);
		}
		return b.toString();
	}

	void analyseFile(File f) throws IOException {
		FileInputStream inStream=new FileInputStream(f);
		DataInputStream inDataStream=new DataInputStream(inStream);
		BufferedReader br=new BufferedReader(new InputStreamReader(inDataStream));

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

		br.close();
		inDataStream.close();

	}

}



Also, your line is almost a million character long. How can you tell the difference by looking weather it's all there or if only most of it is there?
Was This Post Helpful? 2
  • +
  • -

#7 nicool  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 23
  • Joined: 12-August 11

Re: Problem with Reading very long line using bufferedReader

Posted 07 November 2011 - 11:33 AM

Sorry @all!! :-)
Problem is just because of i forgot to close FileWriter object.....
Now its working correctly....
Thanks for your Suggestions...

This post has been edited by nicool: 07 November 2011 - 11:34 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1