1.获取文件创建位置
public String getPath() {
StringBuffer path = new StringBuffer();
path.append(“file:///”);
Enumeration e = FileSystemRegistry.listRoots();
String root = (String) e.nextElement();
while(e.hasMoreElements()) {
e.nextElement();
}
path.append(root);
path.append(“yourFolderName”);
return path.toString();
}
2.调用(FileConnection)Connector.open(url)创建文件,具体如下
写文件:
FileConnection fc = (FileConnection)Connector.open(ref);
if(!fc.exists()) {
fc.create();
}
DataOutputStream dos = new
DataOutputStream(fc.openDataOutputStream());
如果写整数,则调用dos.writeInt(int)
如果写字符串,则调用dos.writeUTF(String)
读文件:
FileConnection fc = (FileConnection) Connector.open(ref);
if(fc.exists() && fc.canRead())
{
DataInputStream dis = new
DataInputStream(fc.openDataInputStream());
如果读字符串,则调用dis.readUTF()
如果读整数,则调用dis.readInt()
}
FileConnection fc;
try {
= null;
e = FileSystemRegistry.listRoots();
(e.hasMoreElements()) {
elem = e.nextElement().toString();
}
System.out.println(“::”+elem);
(FileConnection) Connector.open(“file://localhost/” +
elem+”a.txt”);
(!fc.exists()) {
fc.create();
OutputStream is = fc.openOutputStream();
is.write(“abc”.getBytes(), 0,
“abc”.length());
is.flush();
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}