Results 1 to 2 of 2
Thread: Reading/Writing Integer Bytes
- 03-30-2011, 06:04 PM #1
Senior Member
- Join Date
- Jan 2009
- Location
- NJ, USA
- Posts
- 183
- Rep Power
- 5
Reading/Writing Integer Bytes
Hi, a piece of my recent program includes generating map "chunks" and saving each chunk as a .chunk file. In the past, I typically stored such data as text in the file, but it has come to a point where I need to optimize the file size by writing the individual integer bytes to a file rather than writing/reading strings then parsing them. Not really sure how to do this though... any help appreciated, thanks.
Edit: Actually, better yet, just reading/writing individual bytes would be sufficient.
This is what I tried to do... but it just prints the bytes as strings to the file, and tries to read it as text (see error message).
Exception in thread "main" java.util.InputMismatchException: Value out of range. Value:"0123456789" Radix:10
at java.util.Scanner.nextByte(Unknown Source)
at java.util.Scanner.nextByte(Unknown Source)
at MapEditor.IOTest.loadBytes(IOTest.java:44)
at MapEditor.IOTest.main(IOTest.java:21)
Java Code:package MapEditor; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.PrintWriter; import java.util.Scanner; public class IOTest { private static final String sep = File.separator; public static void main(String[] args) { byte[] someBytes = new byte[10]; for(byte i = 0; i < 10; i++) { someBytes[i] = i; } saveBytes(someBytes); someBytes = loadBytes(); for(int i = 0; i < 10; i++) { System.out.println(someBytes[i]); } } private static byte[] loadBytes() { String fileName = "data" + sep + "test.dat"; Scanner reader; File file = new File(fileName); try { reader = new Scanner(file); } catch(FileNotFoundException ex) { return null; } byte[] bytes = new byte[(int)file.length()]; for(int i = 0; i < bytes.length; i++) { bytes[i] = reader.nextByte(); } return bytes; } private static boolean saveBytes(byte[] bytes) { String fileName = "data" + sep + "test.dat"; File file = new File(fileName); PrintWriter writer = null; try { writer = new PrintWriter(file); } catch(FileNotFoundException ex1) { try { file.createNewFile(); writer = new PrintWriter(file); } catch(IOException ex2) { System.err.println("Could not create new file (" + fileName + ") Error: " + ex2); return false; } } for(int i = 0; i < bytes.length; i++) { writer.print(bytes[i]); } writer.flush(); writer.close(); return true; } }Last edited by AndrewM16921; 03-30-2011 at 06:34 PM.
- 04-03-2011, 11:56 PM #2
A few remarks:
- What is the range of the integers you’re trying to store in bytes? Is the "map" a picture, are you saving pixels? An integer is 32 bits, a byte is 8 bits.
If you cast a byte to an integer (what you are implicitly doing if you use it as a counter in a for-loop) it wraps to negative –128 (range byte: -128 to 127). So it’s a bad idea to use a byte as a counter using a realistic (>10) range.
- You use Scanner. I’m a very old-fashioned programmer not using it very much, but I think to know that it is used for delimited text, not a bytestream.
- You use PrintWriter. It produces human-readable textfiles, That human can’t figure out where integers larger then 10 stop and start again without delimiters (Sorry, I suppose you figured out the last two points already but used it for the example)
- If I was going to help you, my knowledge would come from Thinking in Java from Bruce Eckel. The 3rd Edition is free on the internet (Bruce Eckel's MindView, Inc: Free Electronic Book: Thinking in Java, 3rd Edition), chapter 12 is on IO.)
I guess you need a Data in/outputstream, wrapped by buffered- and filestreams.
Similar Threads
-
Problem reading from socket using read(bytes[])
By sm123 in forum New To JavaReplies: 1Last Post: 04-21-2010, 06:49 PM -
Reading and Writing to XML - Help Please!
By JonnySnip3r in forum New To JavaReplies: 4Last Post: 01-17-2010, 10:55 PM -
Reading/Writing to file
By Doctor Cactus in forum New To JavaReplies: 2Last Post: 10-28-2008, 02:05 PM -
Writing integer pixel array(Range:0-255) into .txt file
By Mazharul in forum Java 2DReplies: 3Last Post: 08-24-2008, 01:51 PM -
Reading bytes from InputStream
By Java Tip in forum Java TipReplies: 0Last Post: 11-25-2007, 07:51 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks