Please help in reading file
3 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Huang_Qingyan
Posted On:   Wednesday, January 23, 2002 08:28 AM

Hi, Im been trying to read a text file from my local drive using J2ME and I failed after a few days and so can you guys please kindly help me out? My codes goes like this: ---------------------------------------- package InputData; import java.io.*; import java.util.*; import javax.microedition.io.*; import javax.microedition.midlet.*; import javax.microedition.lcdui.*; public class InputData extends MIDlet implements CommandListener{ private Command exitCommand, nextCommand; private Display display; private Form screen; private StringItem displayTitle; public InputD   More>>

Hi,


Im been trying to read a text file from my local drive using J2ME and I failed after a few days and so can you guys please kindly help me out?
My codes goes like this:



----------------------------------------



package InputData;



import java.io.*;
import java.util.*;
import javax.microedition.io.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;



public class InputData extends MIDlet implements CommandListener{
private Command exitCommand, nextCommand;
private Display display;
private Form screen;
private StringItem displayTitle;



public InputData(){
display = Display.getDisplay(this);
exitCommand = new Command("Exit", Command.EXIT, 2 );
nextCommand = new Command("Next", Command.OK, 1);



screen = new Form("Data display here...");//Title on top of midlet
displayTitle = new StringItem("", "Default text");



//Default text shown
screen.append(displayTitle);



screen.addCommand(exitCommand);
screen.addCommand(nextCommand);
screen.setCommandListener(this);
}



public void startApp() throws MIDletStateChangeException{
//display.setCurrent(screen);
readData();
}



public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}

public void commandAction(Command c, Displayable s){
if(c == exitCommand){
destroyApp(false);
notifyDestroyed();
}
}



public void readData(){
try {
String fileName = "file:C:/test.txt";
InputConnection conn = (InputConnection)Connector.open(fileName, Connector.READ);
InputStream is = conn.openInputStream();
StringBuffer sb = new StringBuffer();
int chr, i = 0;
conn.close();
// Read until the end of the stream
while ((chr = is.read()) != -1)
sb.append((char) chr);
is.close();
}



catch (Exception e){
System.out.println("Error occurs while reading file");
}
}

}



Please kindly give me your advice on this.

Thanks,
Franco
   <<Less

Re: Please help in reading file

Posted By:   Shane_Isbell  
Posted On:   Monday, February 11, 2002 07:22 AM

The CLDC is not required to support the file protocol. Try ibm's VAME. They have a library, denoted as com.ibm.oti.connection.file,
which you can use to open a file.

Re: Please help in reading file

Posted By:   lei_jun  
Posted On:   Wednesday, January 23, 2002 10:24 PM

OK,I'll try to explain the securiy domain.

You may know this fact: for security reasons, a java applet can't access the local files of the machine it's running on. A midlet and a applet are alike. Midlets should NOT access local files( on the mobile phone). But for these trusted midlets, it's another story. They can create a RandomAccessStream to access local files.

The question is:
What's the difference between trusted midlets and untrusted ones?

In midp, there is a class called SecurityDomain. Every trusted midlet must have a SecurityDomain object to do
risky actions.

Take RandomAccessStream as a example, this class's constructor method will ask the caller: Are you authorized to create me? i.e. The caller must be given a security domain by MIDP. But in MIDP RI, the security domain can only be given to some internal MIDlets and classes. So a third part midlet won't have security domain -- thus, it can't access local files.

If you wanna access the files on PC, I think socket may be a solution.Midlet can send the file name to PC through socket, a application running on PC receives the name and sends the content of file through ServerSocket, then the midlet can read the content from socket.
BTW: Are you chinese? You can mail to
tiger_lei@263.net.

Re: Please help in reading file

Posted By:   lei_jun  
Posted On:   Wednesday, January 23, 2002 07:26 PM

First, you can access this file through http. That is, start a http server and put the file on the proper position.
About local file's access,
I think the only way to access local(on the MIDP device) files is to modify the source codes of midp and build a new j2me.
1. You can't open a file like "Connector.open("file://c:/test.txt"). That will throw ConnectionNotFoundException. Right? I found MIDP seemed not process file:// properly, A bug , maybe.

2. You may use RandomAccessStream to access a local file, but your midlet suites must have the Actions.DEVICE_CORE_FUNCTION security domain.
About | Sitemap | Contact