11011110110011011001100110011001000110011001100111010001011

April 2, 2008

connections with cldc

Filed under: Programming
import java.io.*;
import javax.microedition.io.*;
	
public class TestInput {
	
    public static void main( String args[] ) {
        try {
            String uri = \"file:c:/autoexec.bat\";
            InputConnection conn = (InputConnection)
                     Connector.open( uri,
                        Connector.READ );
            InputStream in = conn.openInputStream();
            int ch;
	
            conn.close();  // doesn’t close input stream!
            while( ( ch = in.read() ) != -1 ){
                System.out.print( (char) ch );
            }
	
            in.close();
        }
        catch( ConnectionNotFoundException e ){
            System.out.println( “File could not be
                                         found!” );
        }
        catch( IOException e ){
        System.out.println( e.toString() );
        }
	
        System.exit( 0 );
    }
}

Notice that the program can safely close the connection after it obtains the input stream!!!
The input stream isn’t closed !! And the output stream either.