Reading & Writting Binary File

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • KWSW
    New Member
    • May 2007
    • 72

    Reading & Writting Binary File

    Got a question about reading and writing binary files.

    I know that to write a value(integer) to a binary file I can use the fileoutputstrea m to do it.
    Code:
    try 
    {
            // Create an output stream to the file.
            FileOutputStream file_output = new FileOutputStream (outFile);                                                              
    
            //for(int i = 0; i < cInt.length; i++)
    
            file_output.write(100);                
            file_output.write(200);                
            file_output.write(300);                
    
            // Close file when finished with it..
            file_output.close ();
    }
    But I cant seem to read back from the file these values using the fileinputstream

    Code:
    int bInt = 0;
    try 
    {
        // Wrap the FileInputStream with a DataInputStream            
        FileInputStream file_input = new FileInputStream (inFile);
        DataInputStream data_in    = new DataInputStream (file_input);
    
        while(true) 
        {
            try 
            {                    
                bInt = data_in.readInt();
            }
            catch (EOFException eof) 
            {
                //end of file
                break;
            }
    
            System.out.println("Test: "+bInt);
        }
    
        data_in.close ();
    } 
    catch(IOException e) 
    {
        System.out.println ( "IO Exception =: " + e );
        System.exit(0);
    }
    Any idea where I went wrong? Thanks in advance :)
  • KWSW
    New Member
    • May 2007
    • 72

    #2
    ok nvm... solved it :)

    Comment

    Working...