Thread: Java Binary File IO

  1. #1
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094

    Java Binary File IO

    I am doing some File reading... and I am having some problems with a bit of it...

    I was having some weird output and it took me a long time to figure it out, it turns out Java doesn't like reading 1000 000 , 1000 0010 , 1001 1101 , and 1001 1111 ... I haven't check all possibilities.

    I take the whole file, and read it into a char[] using :
    Code:
            public BufferReader(File lFile) {
    		if (lFile.exists() && lFile.isFile() && lFile.canRead()) {
    			try {
    				iBuffer = new char[(int) lFile.length()];
    				Reader lIn = new FileReader(lFile);
    				lIn.read(iBuffer);
    				lIn.close();
    			}
    			catch ( Exception pEx ) {
    				iBuffer = null;
    				pEx.printStackTrace();
    			}
    		}
    	}
    It reads it all in and says that it is all good, but when I take a cell that should contain one of the above values, I end up with a value that is >255 (which should be possible I would think)
    Code:
    (int)iBuffer[pPos]
    should read it out as an int and throughout 99% of the file it is fine, but there are 3 bytes that end up with values > 255 and they contain the above values.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Reader and char doesn't agree with binary. Use an InputStream and byte.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Closing a binary file before it ends
    By cedrik23 in forum C Programming
    Replies: 8
    Last Post: 12-11-2007, 01:26 PM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Replies: 3
    Last Post: 03-04-2005, 02:46 PM