Results 1 to 9 of 9

Thread: Read in X characters of a file

  1. #1

    Thread Starter
    Hyperactive Member rockies1's Avatar
    Join Date
    Jul 1999
    Location
    Stuck at work
    Posts
    375

    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?
    Morgan
    [email protected] - Home
    [email protected] - Work
    Using VB6 SP6 but trying to learn VB2005EE

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  3. #3
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    It's one big line but how is the line broken up. Is it abc def ghi jkl mno pqr or abcdefgh ijklmnopqr?

  4. #4
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    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);
         } 
        } 
       }
      }

  5. #5
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111
    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.

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  7. #7

    Thread Starter
    Hyperactive Member rockies1's Avatar
    Join Date
    Jul 1999
    Location
    Stuck at work
    Posts
    375
    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.
    Morgan
    [email protected] - Home
    [email protected] - Work
    Using VB6 SP6 but trying to learn VB2005EE

  8. #8

    Thread Starter
    Hyperactive Member rockies1's Avatar
    Join Date
    Jul 1999
    Location
    Stuck at work
    Posts
    375
    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!
    Morgan
    [email protected] - Home
    [email protected] - Work
    Using VB6 SP6 but trying to learn VB2005EE

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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
  •  



Click Here to Expand Forum to Full Width