FileConnection

gesanri 2009-08-11 10:17:06
以下这个程序就是建立一个FileConnection,写一句话


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

public class FileConnection extends MIDlet implements CommandListener{
private Command exit,start;
private Display display;
private Form form;

public FileConnection() {
display = Display.getDisplay(this);
exit = new Command("Exit",Command.EXIT,1);
start = new Command("Start",Command.EXIT,1);
form = new Form("Write To File");
form.addCommand(exit);
form.addCommand(start);
form.setCommandListener(this);
}

public void startApp(){
display.setCurrent(form);
}

public void pauseApp(){
}

public void destroyApp(boolean arg0){
}

public void commandAction(Command command,Displayable displayable){
if(command == exit){
destroyApp(false);
notifyDestroyed();
}
else if(command == start){
try{
OutputConnection connection = (OutputConnection)Connector.open("file://c:/myfile.txt;append=true",Connector.WRITE);
OutputStream out = connection.openOutputStream();
PrintStream output = new PrintStream(out);
output.println("This is a test");
out.close();
connection.close();
Alert alert = new Alert("Completed","Data written",null,null);
alert.setTimeout(Alert.FOREVER);
alert.setType(AlertType.ERROR);
display.setCurrent(alert);
}catch(ConnectionNotFoundException error){
Alert alert = new Alert("Error","Cannot access file",null,null);
alert.setTimeout(Alert.FOREVER);
alert.setType(AlertType.ERROR);
display.setCurrent(alert);
}catch(IOException error){
Alert alert = new Alert("Error",error.toString(),null,null);
alert.setTimeout(Alert.FOREVER);
alert.setType(AlertType.ERROR);
display.setCurrent(alert);
}
}
}
}


报错误:
java.lang.IllegalArgumentException: Root is not specified
是不是我的open方法中的参数内容有问题?
...全文
330 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
gesanri 2009-08-12
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 pupppet 的回复:]
file://localhost/(root1或C etc....)
[/Quote]
改后没有报错误了,但是我搜索我的电脑也没有找到myfile.txt,如果生成了,应该在哪?
pupppet 2009-08-12
  • 打赏
  • 举报
回复
file://localhost/(root1或C etc....)
kf156 2009-08-12
  • 打赏
  • 举报
回复
OutputConnection connection = (OutputConnection)Connector.open("file://c:/myfile.txt;append=true",Connector.WRITE);
OutputStream out = connection.openOutputStream();
PrintStream output = new PrintStream(out);
output.println("This is a test");
out.close();
connection.close();


你这是要做什么....
建一个文件,写入数据,操作如下:

FileConnectin fc=(FileConnection)Connector.open("file:///root1/myfile.txt");
if(fc.exists())//若文件存在,先删除
fc.delete();
fc.create();//创建文件
OutputStream os = fc.openOutputStream();
os.write("This is a test".getBytes());//写入数据
os.close();
fc.close();
gesanri 2009-08-12
  • 打赏
  • 举报
回复
有谁知道啊,在wtk模拟器中根目录是什么啊
gesanri 2009-08-12
  • 打赏
  • 举报
回复
我用的是wtk模拟器,把c改成root1依然报这个错啊,我用s40模拟器也试过了,提示如下:
Smart card communication error 0x80100017
The specified reader is not currently available for use
Using Untrusted simulated domain
"Series 40 5th Edition SDK" Instance #6267000 Ready for Future Connections
Warning: Traffic View: Listing of TCP/UDP Sent traffic is set to off (see Monitor)
Warning: Traffic View: Listing of TCP/UDP Received traffic is set to off (see Monitor)
什么原因啊
kf156 2009-08-12
  • 打赏
  • 举报
回复
你是用什么模拟器,如果是WTK的话,版本多少?
程序运行完后,先别关模拟器,先搜索文件看看
askters 2009-08-12
  • 打赏
  • 举报
回复
一般文件夹系统在这个C:\Documents and Settings\aaa\j2mewtk\2.5.2\appdb\DefaultColorPhone\filesystem\root1这个目录下,你要把你的文件先放到这个目录下,才可以读取,
由于不同模拟器的存储位置不一样,可以使用Enumeration enumer = FileSystemRegistry.listRoots();获取模拟器的根目录,
while (enumer.hasMoreElements()) {
path = (String) enumer.nextElement; FileConnection conn = (FileConnection) Connector
.open("file:///".concat(path).concat("myfile.txt"));
}
wtk中path一般为root1,诺基亚为c和e,当然上面代码只是个例子,还要进行判断你的文件夹放在哪?
askters 2009-08-12
  • 打赏
  • 举报
回复
一般文件夹系统在这个C:\Documents and Settings\aaa\j2mewtk\2.5.2\appdb\DefaultColorPhone\filesystem\root1这个目录下,你要把你的文件先放到这个目录下,才可以读取,
由于不同模拟器的存储位置不一样,可以使用Enumeration enumer = FileSystemRegistry.listRoots();获取模拟器的根目录,
while (enumer.hasMoreElements()) {
path = (String) enumer.nextElement; FileConnection conn = (FileConnection) Connector
.open("file:///".concat(path).concat("myfile.txt"));
}
wtk中path一般为root1,诺基亚为c和e,当然上面代码只是个例子,还要进行判断你的文件夹放在哪?
gesanri 2009-08-12
  • 打赏
  • 举报
回复
我现在是搜索“我的电脑”都找不到,程序也不报错

import java.io.OutputStream;

import javax.microedition.io.file.FileConnection;
import javax.microedition.io.Connector;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.MIDlet;

public class fconnection extends MIDlet implements CommandListener{
private Command exit,start;
public static Display display;
private Form form;

public fconnection() {
display = Display.getDisplay(this);
exit = new Command("Exit",Command.EXIT,1);
start = new Command("Start",Command.EXIT,1);
form = new Form("Write To File");
form.addCommand(exit);
form.addCommand(start);
form.setCommandListener(this);
}

public void startApp(){
display.setCurrent(form);
}

public void pauseApp(){
}

public void destroyApp(boolean arg0){
}

public void commandAction(Command command,Displayable displayable){
if(command == exit){
destroyApp(false);
notifyDestroyed();
}
else if(command == start){
fileThread t = new fileThread();
t.start();
}
}
}

class fileThread extends Thread{
public void run(){
try{
FileConnection fc=(FileConnection)Connector.open("file:///root1/myfile.txt");
if(fc.exists())//若文件存在,先删除
fc.delete();
fc.create();//创建文件
OutputStream os = fc.openOutputStream();
os.write("This is a test".getBytes());//写入数据
os.close();
fc.close();
}catch(Exception error){
error.printStackTrace();
}
}
}


kf156 2009-08-12
  • 打赏
  • 举报
回复
WTK安装路径\appdb\DefaultColorPhone\filesystem\
一般在这目录
若找不到,直接在系统里搜索myfile.txt文件
gesanri 2009-08-12
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 kf156 的回复:]
OutputConnection connection = (OutputConnection)Connector.open("file://c:/myfile.txt;append=true",Connector.WRITE);
                OutputStream out = connection.openOutputStream();
                PrintStream output = new PrintStream(out);
                output.println("This is a test");
                out.close();
                connection.close();


你这是要做什么....
建一个文件,写入数据,操作如下:

FileConnectin fc=(FileConnection)Connector.open("file:///root1/myfile.txt");
if(fc.exists())//若文件存在,先删除
fc.delete();
fc.create();//创建文件
OutputStream os = fc.openOutputStream();
os.write("This is a test".getBytes());//写入数据
os.close();
fc.close();

[/Quote]
这样的话也是不报错,但并没有产生myfile.txt文件啊,应该去哪找?
pupppet 2009-08-11
  • 打赏
  • 举报
回复
打开路径有问题。。。。结合你模拟器看看,sun的就只有root1一个根目录,nokia的根目录就有C和E盘
gesanri 2009-08-11
  • 打赏
  • 举报
回复
忘了把connection的相关内容放进Thread了,不过这并不是错误的原因啊,程序依然有问题

13,100

社区成员

发帖
与我相关
我的任务
社区描述
Java J2ME
社区管理员
  • J2ME社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧