|
-
Nov 10th, 2004, 03:06 PM
#1
Thread Starter
Hyperactive Member
Read in X characters of a file
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:
Code:
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?
-
Nov 10th, 2004, 05:03 PM
#2
Use the read(char[]) method.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 10th, 2004, 07:21 PM
#3
Dazed Member
It's one big line but how is the line broken up. Is it abc def ghi jkl mno pqr or abcdefgh ijklmnopqr?
-
Nov 10th, 2004, 07:55 PM
#4
Dazed Member
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.
Code:
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);
}
}
}
}
-
Nov 10th, 2004, 08:24 PM
#5
Frenzied Member
I thought the read(byte[]) method would work but not sure.
Code:
public int read(byte[] buffer,int offset,int length) throws IOException
Last edited by System_Error; Nov 10th, 2004 at 08:28 PM.
-
Nov 11th, 2004, 03:05 AM
#6
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:
Code:
final BLOCKSIZE = 450;
char[] buffer = new char[BLOCKSIZE];
in.read(buffer);
// Skip whitespace
in.read();
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 11th, 2004, 09:47 AM
#7
Thread Starter
Hyperactive Member
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.
-
Nov 11th, 2004, 09:52 AM
#8
Thread Starter
Hyperactive Member
I ended up using this:
Code:
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();
Code:
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!
-
Nov 11th, 2004, 10:56 AM
#9
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|