Thread: Java: File I/O

  1. #1
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696

    Java: File I/O

    The following program will throw an exception at the indicated section. Don't know how to fix it
    Code:
    iimport java.io.*;
    
    public class MainTwo
    {
        private RandomAccessFile ioFile;
        
        public MainTwo( String file ) 
        {
            try
            {
                ioFile = new RandomAccessFile( file, "rw" );
            }
            catch ( FileNotFoundException e )
            {
                System.err.println ( "Mmm?" );
            }
        }
        
        public void write( String str, int length ) throws IOException
        {
            StringBuffer buffer = new StringBuffer( str );
            buffer.setLength( length );
            
            ioFile.writeChars( buffer.toString() );
           
        }
        
        public String read( int length ) throws IOException
        {
             char arr[] = new char[ length ];
             char temp;
             
             for ( int count = 0; count < length; count++ )
             {
                temp = ioFile.readChar();
                arr[ count ] = temp;
             }   
             
             return new String( arr ).replace( '\0', ' ' );
        }
        
        public void kill()
        {
            try
            {
                if ( ioFile != null )
                    ioFile.close();
            }
            catch ( IOException e )
            {
                System.err.println( "Great?" );
            }
        }
        
        public static void main( String args[] )
        {
            MainTwo app;
            String msg;
            String anotherMsg;
      
            app = new MainTwo( "Data.bin" );
                  
            msg = "Got Java?";
            try
            {
                app.write( msg, msg.length() );
            }
            catch ( IOException e )
            {
                System.err.println( "What the?" );
            }
            
            try
            {
                anotherMsg = app.read( msg.length() );
            }
            catch ( IOException e )
            {
                System.err.println( "Ouch" );
            }
            
            app.kill();
        }
    }
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  2. #2
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    your getting an exception because the file pointer is at the end of the file after writing (ie. there is nothing to read).

    btw: do you have to read one byte at a time for some reason? there are easier ways to do such things.

    see: http://java.sun.com/j2se/1.4.2/docs/...ccessFile.html

  3. #3
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    kewl, thnx
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File I/O Assertion Failure in VS2008
    By clegs in forum C Programming
    Replies: 5
    Last Post: 12-25-2008, 04:47 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  4. 2 questions surrounding an I/O file
    By Guti14 in forum C Programming
    Replies: 2
    Last Post: 08-30-2004, 11:21 PM
  5. advice on file i/o
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-29-2001, 05:56 AM