Results 1 to 2 of 2
  1. #1
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    CA, USA
    Posts
    264
    Rep Power
    6

    Default 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.

  2. #2
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    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

  1. Problem reading from socket using read(bytes[])
    By sm123 in forum New To Java
    Replies: 1
    Last Post: 04-21-2010, 06:49 PM
  2. Reading and Writing to XML - Help Please!
    By JonnySnip3r in forum New To Java
    Replies: 4
    Last Post: 01-17-2010, 10:55 PM
  3. Reading/Writing to file
    By Doctor Cactus in forum New To Java
    Replies: 2
    Last Post: 10-28-2008, 02:05 PM
  4. Replies: 3
    Last Post: 08-24-2008, 01:51 PM
  5. Reading bytes from InputStream
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-25-2007, 07:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •