Results 1 to 3 of 3
  1. #1
    bbonya is offline Member
    Join Date
    May 2008
    Posts
    2
    Rep Power
    0

    Question How to using read file in FileConnection

    Hi all,

    please guide how to using FileConnection in j2me??
    I try open and read to file. But don't execute.

    An exception happened at InputStream in = conn.openInputStream();

    Exception messgae is ..

    java.io.IOException:

    Java Code:
    import java.io.*;
    import javax.microedition.io.*;
    import javax.microedition.midlet.*;
    import javax.microedition.io.file.*;
    
    public class FileTest extends MIDlet {
    
    	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
    	}
    	 
    	 protected void pauseApp() {
    		 
    	 }
     
    	 FileConnection conn = null;
    
    	String url = "file:///root1/test.txt";
    	
    	protected void startApp() throws MIDletStateChangeException {
    		try{
    			System.out.println("Start app");
    			conn = (FileConnection)Connector.open(url, Connector.READ);
    		}
    		catch(Exception e)
    		{
    			System.out.println("=== Open error ===");
    			System.out.println(e);
    		}
    
    		try{
    			
    			System.out.println(conn);
    
    			InputStream in = conn.openInputStream();
    
    			byte[] b = new byte[1024];
    			in.read(b,0,10);
    			
    			System.out.println(b);
    		
    		}
    		catch(Exception e)	{
    			System.out.println("=== Read error ===");
    			System.out.println(e);
    		}
    		
    		try{
    			conn.close();
    		}
    
    		catch(Exception e)
    		{
    			System.out.println("=== Close error ===");
    			System.out.println(e);
    		}
    
    	}
    }

  2. #2
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    7

    Default read from LOCAL ? - or testing

    Everything I could find worked off the tower. ( iow - seems to assume we are going to the internet ) If it is just a local testing and get-going question, then we might do it like this:
    Java Code:
    FileInputStream file = new FileInputStream ("test.txt");//
    DataInputStream stream = new DataInputStream(file) ;//
    Or something similar.

    Further questions welcome, it is hard to tell if this is the question it sounds like. Additionally, information may be obtained by trapping the errant code in an exception block, then posting the message it gives.

  3. #3
    bbonya is offline Member
    Join Date
    May 2008
    Posts
    2
    Rep Power
    0

    Default

    I find out a solution.

    Java Code:
    Enumeration e = FileSystemRegistry.listRoots();
    
    String root = (String)e.nextElement();
    String filename = "file:///" + root + filename;
    
    FineConnection fc = (FileConnection)Connector.open(filename, Connector.READ);
    
    InputStream is = (InputStream)fc.openInputStream();
    Thanks for the reply.

Similar Threads

  1. help to read .pst file through java
    By umadas in forum Advanced Java
    Replies: 2
    Last Post: 12-19-2010, 04:32 AM
  2. Replies: 3
    Last Post: 06-22-2008, 07:25 PM
  3. How to read a text file from a Java Archive File
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 09:13 AM
  4. Read file
    By tajinvillage in forum Threads and Synchronization
    Replies: 0
    Last Post: 01-29-2008, 09:10 AM
  5. read txt file
    By sureshsri1981 in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 08-05-2007, 03:49 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
  •