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,
Re: Problem with Reading very long line using bufferedReader
Try using a scanner instead:
Code Java:
Scanner scanner = new Scanner(new File(inFile));
while(scanner.hasNextLine())
{
System.out.println(scanner.nextLine());
}
Re: Problem with Reading very long line using bufferedReader
Quote:
It's failed to read whole line.
How much of the line does it read?
Does it read the same amount every time?
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...
Re: Problem with Reading very long line using bufferedReader
This test program seems to write and read 913434 bytes with no problem.
Code :
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?
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....
Re: Problem with Reading very long line using bufferedReader
Thanks for your suggestions