J2ME文件管理(文件/文件夹的访问)

代码来自书本

package com.zeph.j2me.fo;

import java.io.IOException;
import java.io.InputStream;
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.Form;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.List;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class FileOperateMIDlet extends MIDlet implements CommandListener {

	private String currDirName;
	
	private final static String UP_DIRECTORY = "..";
	
	private final static String ROOT = "/";
	
	private final static String SEP_STR = "/";
	
	private final static char SEP = '/';
	
	private final static String SYSVERSION = "microedition.io.file.FileConnection.version";
	
	private Image fileIcon = null;
	
	private Image folderIcon = null;
	
	private Form form = new Form("File Browse");
	
	private Display display = null;
	
	private Command viewCommand = new Command("View", Command.ITEM, 1);
	
	private Command backCommand = new Command("Back", Command.ITEM, 1);
	
	private Command exitCommand = new Command("Exit", Command.EXIT, 3);
	

	public FileOperateMIDlet() {
		
		currDirName = ROOT;
		
		display = Display.getDisplay(this);
		
	}

	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {

	}

	protected void pauseApp() {

	}

	protected void startApp() throws MIDletStateChangeException {
		
		String FileConnectionVersion = System.getProperty(SYSVERSION);
		
		if (FileConnectionVersion != null) {
			
			form.append("Loading......");
			
			form.addCommand(exitCommand);
			
			form.setCommandListener(this);
			
			display.setCurrent(form);
			
			try {
				
				fileIcon = Image.createImage("/file_icon.png");
				
				folderIcon = Image.createImage("/folder_icon.png");
				
			} catch (IOException e) {
				
				e.printStackTrace();
				
			}
			
			showCurrDir();
			
			
		} else {
			
			Alert alert = new Alert("Error!",
					"FileConnectionAPI not available", null, AlertType.ERROR);
			
			alert.setTimeout(Alert.FOREVER);
			
			display.setCurrent(alert, form);
			
		}
	}

	public void showCurrDir() {
		
		Enumeration enumeration;
		
		FileConnection currDir = null;
		
		List browser;
		
		try {
			
			if (ROOT.equals(currDirName)) {
				
				enumeration = FileSystemRegistry.listRoots();
				
				browser = new List(currDirName, List.IMPLICIT);
				
			} else {
				
				currDir = (FileConnection) Connector.open("file://localhost/"
						+ currDirName);
				
				enumeration = currDir.list();
				
				browser = new List(currDirName, List.IMPLICIT);
				
				browser.append(UP_DIRECTORY, null);
				
			}
			while (enumeration.hasMoreElements()) {
				
				String fileName = (String) enumeration.nextElement();
				
				if (fileName.charAt(fileName.length() - 1) == SEP) {
					
					browser.append(fileName, folderIcon);
					
				} else {
					
					browser.append(fileName, fileIcon);
					
				}
				
			}
			
			browser.setSelectCommand(viewCommand);
			
			browser.setCommandListener(this);
			
			if (currDir != null) {
				
				currDir.close();
				
			}
			
			Display.getDisplay(this).setCurrent(browser);
			
		} catch (IOException e) {
			
			e.printStackTrace();
			
		}
	}

	public void traverseDirectory(String fileName) {
		
		if (currDirName.equals(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 = ROOT;
				
			}
			
		} else {
			
			currDirName = currDirName + fileName;
			
		}
		
		showCurrDir();
		
	}

	public void showFile(String fileName) {
		
		try {
			
			FileConnection fc = (FileConnection) Connector
					.open("file://localhost/" + currDirName + fileName);
			
			if (!fc.exists()) {
				
				Alert alert = new Alert("Error!", "The file doesn't exist!",
						null, AlertType.ERROR);
				
				alert.setTimeout(3000);
				
				display.setCurrent(alert, form);
				
			}
			
			InputStream fis = fc.openInputStream();
			
			byte[] b = new byte[1024];
			
			int length = fis.read(b, 0, 1024);
			
			fis.close();
			
			fc.close();
			
			TextBox tb = new TextBox("View File:" + fileName, null, 1024,
					TextField.ANY | TextField.UNEDITABLE);
			
			tb.addCommand(backCommand);
			
			tb.addCommand(exitCommand);
			
			tb.setCommandListener(this);
			
			if (length > 0) {
				
				tb.setString(new String(b, 0, length));
				
			}
			
			Display.getDisplay(this).setCurrent(tb);
			
		} catch (IOException e) {
			
			e.printStackTrace();
			
		}
		
	}

	public void commandAction(Command c, Displayable d) {
		
		if (c == viewCommand) {
			
			List curr = (List) d;
			
			final String currFile = curr.getString(curr.getSelectedIndex());
			
			new Thread() {
				
				public void run() {
					
					if (currFile.endsWith(SEP_STR)
							|| currFile.equals(UP_DIRECTORY)) {
						
						traverseDirectory(currFile);
						
					} else {
						
						showFile(currFile);
						
					}
					
				}
				
			}.start();
			
		} else if (c == backCommand) {
			
			showCurrDir();
			
		} else if (c == exitCommand) {
			
			notifyDestroyed();
			
		}
		
	}
	
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值