Click to See Complete Forum and Search --> : Read in X characters of a file
rockies1
Nov 10th, 2004, 02:06 PM
I have a file that I need to process. Some of them are as large as several MB.
The file is a single line of data, each record being 450 bytes.
I've read files using this:
BufferedReader in = new BufferedReader(new FileReader( strTheFile ));
String strReadFromFile = null;
strReadFromFile = in.readLine ();
but that reads the entire line, in this case several MB of data.
How can I read in just the 450 bytes I need at a time?
CornedBee
Nov 10th, 2004, 04:03 PM
Use the read(char[]) method.
Dillinger4
Nov 10th, 2004, 06:21 PM
It's one big line but how is the line broken up. Is it abc def ghi jkl mno pqr or abcdefgh ijklmnopqr?
Dillinger4
Nov 10th, 2004, 06:55 PM
This will read the whole line but broken into xSize depending on how big you make each record. I think this is good because each record can vary in size if you want.
import java.io.*;
import java.util.*;
public class X{
public static void main(String[] args){
File f;
BufferedReader buff = null;
StringTokenizer st;
try{
f = new File("C:\\test.txt");
buff = new BufferedReader(new FileReader(f));
String data = buff.readLine();
st = new StringTokenizer(data);
while(st.hasMoreTokens()){
System.out.println(st.nextToken());
}
}catch(IOException io){System.err.println(io);}
finally{
try{
if(buff != null){
buff.close();
}
}catch(IOException io){
System.err.println(io);
}
}
}
}
System_Error
Nov 10th, 2004, 07:24 PM
I thought the read(byte[]) method would work but not sure.
public int read(byte[] buffer,int offset,int length) throws IOException
CornedBee
Nov 11th, 2004, 02:05 AM
If each record is 450 characters long (NOTE: If you really mean bytes and the whole thing is binary data, you need to use InputStream, not Reader), then this code will read a block:
final BLOCKSIZE = 450;
char[] buffer = new char[BLOCKSIZE];
in.read(buffer);
// Skip whitespace
in.read();
rockies1
Nov 11th, 2004, 08:47 AM
Originally posted by Dilenger4
It's one big line but how is the line broken up. Is it abc def ghi jkl mno pqr or abcdefgh ijklmnopqr?
The line contains many records with many fields that are of different lengths, fixed width-not delimeted.
rockies1
Nov 11th, 2004, 08:52 AM
I ended up using this:
BufferedReader in = new BufferedReader(new FileReader( strTheFile ));
StringBuffer sbufReadFromFile = new StringBuffer(in.readLine ());
if ( sbufReadFromFile == null )
{
//End of the file
break;
}
else
{
//Process the line
//System.out.println(strReadFromFile);
processLine(sbufReadFromFile);
}
in.close();
public static void processLine(StringBuffer strDataIn)
{
//Each record is 450 bytes long...
StringBuffer strTemp = strDataIn;
int intNumRecords = strTemp.length()/450;
for(int x=0; x<intNumRecords; x++)
{
String strRecord = strTemp.substring(x*450,(x+1)*450);
System.out.println(x + ": " + strRecord);
}
}//End processLine
It still reads the entire file in, but I was not sure how to read the first 450 then the next 450 then the 3rd 450 and so on...
Thanks for all the help!
CornedBee
Nov 11th, 2004, 09:56 AM
As long as the width is fixed, my approach works. If not, you can still read character by character. The BufferedReader ought to ensure that it is still efficient. Also, the Scanner class that's new in Java5 ought to do exactly what you want.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.