J2ME读取不到文件

Zemo 发布于 2014/04/11 00:38
阅读 153
收藏 0

【DevOps必读】产品经理与程序员之间如何破局?>>>

亲爱的,@红薯.我知道你做过J2ME...我对J2ME一点都不了解.

我想用J2ME写一个选择本地txt文件读取的功能

为什么在虚拟机上面就是读不到文件(没有真机)?

文件目录如下

下面是读取文件功能的代码:

package com.zemo.test;

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

import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.io.file.FileSystemRegistry;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.List;
import javax.microedition.midlet.MIDletStateChangeException;

public class FileBrowser extends List implements CommandListener{

	public static final Command SELECT_FILE_COMMAND = new Command("选择", Command.OK, 1);
	private String currDirName;
	private String currFile;
	private Image dirIcon;
	private Image fileIcon;
	private CommandListener commandListener;
	
	private static final String UP_DIRECTORY = "..";
	private static final String MEGA_ROOT = "/";
	private static final String SEP_STR = "/";
	private static final char SEP = '/';
	
	private Display display;
	private String selectedURL;
	private String filter = null;
	private String title;
	
	public FileBrowser(Display display) {
		super("文件浏览", IMPLICIT);
		currDirName = MEGA_ROOT;
		this.display = display;
		super.setCommandListener(this);
		setSelectCommand(SELECT_FILE_COMMAND);
		try {
			dirIcon = Image.createImage(this.getClass().getResourceAsStream("/file.png"));
		} catch (IOException e) {
			dirIcon = null;
			e.printStackTrace();
		}
		try {
			fileIcon = Image.createImage(this.getClass().getResourceAsStream("/book.png"));
		} catch (IOException e) {
			fileIcon = null;
			e.printStackTrace();
		}
		showDir();
	}

	/**
	 * 显示当前文件夹
	 */
	private void showDir() {
		new Thread(new Runnable() {
			public void run() {
				try {
					showCurrDir();
				} catch (SecurityException e) {
					Alert alert = new Alert("错误","您没有权限访问此文件或文件夹!",null,AlertType.ERROR);
					alert.setTimeout(2000);
					display.setCurrent(alert,FileBrowser.this);
				}catch (Exception e) {
					e.printStackTrace();
				}
			}
		}).start();
	}

	public void commandAction(Command c, Displayable d) {
		if (c.equals(SELECT_FILE_COMMAND)) {
			List currList = (List) d;
			currFile = currList.getString(currList.getSelectedIndex());
			new Thread(new Runnable() {
				
				public void run() {
					if (currFile.endsWith(SEP_STR) || currFile.equals(UP_DIRECTORY)) {
						openDir(currFile);
					}else {
						doDismiss();
					}
				}
			}).start();
		}else {
			if (commandListener != null) {
				commandListener.commandAction(c, d);
			}
		}
	}
	
	public void setTitle(String title){
		this.title = title;
		super.setTitle(title);
	}
	
	public void showCurrDir(){
		if (title == null) {
			super.setTitle(currDirName);
		}
		Enumeration enumeration = null;
		FileConnection currDir = null;
		
		deleteAll();
		if (MEGA_ROOT.equals(currDirName)) {
			append(UP_DIRECTORY, dirIcon);
			enumeration = FileSystemRegistry.listRoots();
		}else {
			try {
				currDir = (FileConnection) Connector.open("file:///"+currDirName);
				enumeration = currDir.list();
			} catch (IOException e) {
				e.printStackTrace();
			}
			append(UP_DIRECTORY, dirIcon);
		}
		if (enumeration == null) {
			try {
				currDir.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return;
		}
		while (enumeration.hasMoreElements()) {
			String fileName = (String)enumeration.nextElement();
			if (fileName.charAt(fileName.length() - 1) == SEP) {
				append(fileName, dirIcon);
			}else {
				if (filter == null || fileName.indexOf(filter) > -1) {
					append(fileName, fileIcon);
				}
			}
		}
		if (currDir != null) {
			try {
				currDir.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	private void openDir(String fileName){
		if (currDirName.equals(MEGA_ROOT)) {
			if (fileName.equals(UP_DIRECTORY)) {
				return;
			}
			currDirName = fileName;
		}else if(fileName.equals(UP_DIRECTORY)){
			int i = currDirName.lastIndexOf(SEP,currDirName.length() -2 );
			if (i != -1) {
				currDirName = currDirName.substring(0, i+1);
			}else {
				currDirName = MEGA_ROOT;
			}
		}else {
			currDirName = currDirName +fileName;
		}
		showDir();
	}
	
	public FileConnection getSelectedFile() throws IOException{
		FileConnection fileConnection = (FileConnection) Connector.open(selectedURL);
		return fileConnection;
	}

	public String getSelectedFileURL(){
		return selectedURL;
	}
	
	public void setFilter(String filter){
		this.filter = filter;
	}
	
	protected CommandListener getCommandListener() {
		return commandListener;
	}
	
	public void setCommandListener(CommandListener commandListener){
		this.commandListener = commandListener;
	}
	
	public void doDismiss(){
		selectedURL = "file:///"+currDirName + currFile;
		CommandListener listener = getCommandListener();
		if (listener != null) {
			listener.commandAction(SELECT_FILE_COMMAND, this);
		}
	}
}



加载中
返回顶部
顶部