Results 1 to 3 of 3
  1. #1
    trishtren is offline Member
    Join Date
    Feb 2011
    Posts
    53
    Rep Power
    0

    Talking reading a from a binary file

    Hey, iv been trying to read a binary file in java but i seem to have problems reading all the numbers any help would be great, i have tested the code below using print lines to find what's wrong and it seems that although it tells me it has reached the end of the file its only giving me the very first value of the item saved in the file, so it seems to be some sort of fundamental looping problem :

    Java Code:
    //this is the function i am using to save 8 integer values to a file
    
    public void export_file() throws IOException
    	{
    		File file = null;
    		//create a new file
    		if (file == null)
    		{
    			file = new File("Project.mxi");
    		}
    		try
    		{
    			FileOutputStream f_out = new FileOutputStream (file);
    			DataOutputStream d_out = new DataOutputStream (f_out);
    			
    			for (int i=0;i <= 8; i++)
    			{
    				//only do it for the first item not 0
    				if (i > 0)
    				{
    				int x;
                                    //x = value from array list
    				x = listofvalues.get(i);
    				d_out.writeInt(x);
    				}
    			}
    			f_out.close();
    			//close the file from being read
    		}
    		catch (IOException e)
    		{	
    		}	
    	}
    
    public void read_file()
    	{
    		File file = null;
    		int x;
    		
    		boolean exists = (new File("Project.mxi")).exists();
    		if (exists) 
    		{
    			file = new File("Project.mxi");	
    		} 
    		else
    		{
    			System.out.println("File is corrupt or unreadable");	
    		}
    		
    	
    		
    		try
    		{
    			
    		FileInputStream f_in = new FileInputStream(file);
    		DataInputStream d_in = new DataInputStream (f_in);
    		
    		while(true)
    		{
    			try
    			{
    			x = d_in.readInt();
    			}
    			catch(EOFException eof)
    			{
    				System.out.println("end of file");
    				break;
    			}
    			
    			d_in.close();
    			
    		}
    		}
    		catch(IOException e)
    		{
    			
    		}	
    	}
    Any help would be fantastic :)

  2. #2
    Skiller is offline Member
    Join Date
    Jan 2011
    Posts
    67
    Rep Power
    0

    Default

    Looks to me like d_in.close(); should go inside the end of file catch otherwise you just close the file after the first value is read.
    Currently developing Cave Dwellers, a Dwarf Fortress/Minecraft style game for Android.

  3. #3
    trishtren is offline Member
    Join Date
    Feb 2011
    Posts
    53
    Rep Power
    0

    Default

    That did the trick, thanks! seems like a fairly retarded mistake on my part. I checked out your cave dwellers video, very nice congratulations on the z-buffering seems to be nicely executed.

Similar Threads

  1. Reading Binary File using java
    By pnbalaji in forum New To Java
    Replies: 16
    Last Post: 06-11-2010, 03:50 PM
  2. Replies: 3
    Last Post: 05-10-2009, 12:31 AM
  3. Reading a Field in a Binary File
    By janakiram.attuluri in forum Advanced Java
    Replies: 2
    Last Post: 01-09-2008, 11:47 AM
  4. Reading Binary File and Changing data
    By janakiram.attuluri in forum Advanced Java
    Replies: 1
    Last Post: 12-21-2007, 11:10 AM
  5. reading a binary file with a RAF
    By jkurth in forum Advanced Java
    Replies: 2
    Last Post: 12-20-2007, 08:30 AM

Posting Permissions

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