欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JSR75 FileConnection Optional Package 使用方法

程序员文章站 2024-03-24 11:25:10
...

package forrest.fcop;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;

import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class MyFCOP extends MIDlet {
    private void visitByFileConnection(String url) {
        // 首先你必须检查选定的设备是否支持FileConnection
        if(System.getProperty("microedition.io.file.FileConnection.version") != null ) { // file.separator
            FileConnection fconn = null;
            try {
                fconn = (FileConnection) Connector.open(url, Connector.READ_WRITE);
                // Always check whether the file or directory exists.
                if (!fconn.exists()) {
                	fconn.mkdir();
                	((FileConnection) Connector.open("file:///root1/navi/a", Connector.READ_WRITE)).mkdir(); // 创建navi/a文件夹
                	((FileConnection) Connector.open("file:///root1/navi/b", Connector.READ_WRITE)).mkdir(); // 创建navi/b文件夹
                }
                if (fconn.isDirectory()) {
                    Enumeration names = fconn.list();
                    while (names.hasMoreElements()) {
                        String name = (String) names.nextElement();
                        System.out.println("navi下有 " + name + " Dictionary");
                    }
                } else {
                    System.out.println("Not a Directory");
                }
                
                url += "/log.txt";
                fconn = (FileConnection) Connector.open(url, Connector.READ_WRITE);
                if (!fconn.exists()) { // 创建txt文件,并写入文字
                    fconn.create();
                    if (!fconn.canWrite()) fconn.setWritable(true);
                    OutputStream out = fconn.openOutputStream();
                    out.write("欢迎访问".getBytes("utf-8"));
                    out.write("\n".getBytes());
                    out.write("贴心网".getBytes());
                    out.flush();
                    out.close();
                } else {
                	if (!fconn.canRead()) fconn.setReadable(true);
                	InputStream in = fconn.openInputStream();
                	byte[] b = new byte[1024];
                	in.read(b);
                	System.out.println("写出的文件为:" + new String(b, "utf-8").trim());
                	fconn.delete();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            } finally {
            	if (fconn != null) {
            		try {
    					fconn.close();
    				} catch (IOException e) {
    					e.printStackTrace();
    				}
    				fconn = null;
            	}
            }
        } else {
            System.out.println("FCOP not available");
        }
    }
    protected void startApp() throws MIDletStateChangeException {
    	String url = "file:///root1/navi";
        visitByFileConnection(url);
    }
    protected void pauseApp() {
    }
    protected void destroyApp(boolean unconditional) throws MIDletStateChangeException {
    }
}