?? filebrowser.java
字號:
/* * * Copyright ? 2007 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. */package example.fc;import java.io.IOException;import java.io.InputStream;import java.util.Calendar;import java.util.Date;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.Choice;import javax.microedition.lcdui.ChoiceGroup;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.StringItem;import javax.microedition.lcdui.TextBox;import javax.microedition.lcdui.TextField;import javax.microedition.midlet.MIDlet;/** * Demonstration MIDlet for File Connection API. This MIDlet implements simple * file browser for the filesystem available to the J2ME applications. * */public class FileBrowser extends MIDlet implements CommandListener { private static final String[] attrList = { "Read", "Write", "Hidden" }; private static final String[] typeList = { "Regular File", "Directory" }; private static final String[] monthList = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; /* special string denotes upper directory */ private static final String UP_DIRECTORY = ".."; /* special string that denotes upper directory accessible by this browser. * this virtual directory contains all roots. */ private static final String MEGA_ROOT = "/"; /* separator string as defined by FC specification */ private static final String SEP_STR = "/"; /* separator character as defined by FC specification */ private static final char SEP = '/'; private String currDirName; private Command view = new Command("View", Command.ITEM, 1); private Command creat = new Command("New", Command.ITEM, 2); //add delete file functionality private Command delete = new Command("Delete", Command.ITEM, 3); private Command creatOK = new Command("OK", Command.OK, 1); private Command prop = new Command("Properties", Command.ITEM, 2); private Command back = new Command("Back", Command.BACK, 2); private Command exit = new Command("Exit", Command.EXIT, 3); private TextField nameInput; // Input field for new file name private ChoiceGroup typeInput; // Input field for file type (regular/dir) private Image dirIcon; private Image fileIcon; private Image[] iconList; public FileBrowser() { currDirName = MEGA_ROOT; try { dirIcon = Image.createImage("/icons/dir.png"); } catch (IOException e) { dirIcon = null; } try { fileIcon = Image.createImage("/icons/file.png"); } catch (IOException e) { fileIcon = null; } iconList = new Image[] { fileIcon, dirIcon }; } public void startApp() { try { showCurrDir(); } catch (SecurityException e) { Alert alert = new Alert("Error", "You are not authorized to access the restricted API", null, AlertType.ERROR); alert.setTimeout(Alert.FOREVER); Form form = new Form("Cannot access FileConnection"); form.append(new StringItem(null, "You cannot run this MIDlet with the current permissions. " + "Sign the MIDlet suite, or run it in a different security domain")); form.addCommand(exit); form.setCommandListener(this); Display.getDisplay(this).setCurrent(alert, form); } catch (Exception e) { e.printStackTrace(); } } public void pauseApp() { } public void destroyApp(boolean cond) { notifyDestroyed(); } public void commandAction(Command c, Displayable d) { if (c == view) { List curr = (List)d; final String currFile = curr.getString(curr.getSelectedIndex()); new Thread(new Runnable() { public void run() { if (currFile.endsWith(SEP_STR) || currFile.equals(UP_DIRECTORY)) { traverseDirectory(currFile); } else { // Show file contents showFile(currFile); } } }).start(); } else if (c == prop) { List curr = (List)d; String currFile = curr.getString(curr.getSelectedIndex()); showProperties(currFile); } else if (c == creat) { createFile(); } else if (c == creatOK) { String newName = nameInput.getString(); if ((newName == null) || newName.equals("")) { Alert alert = new Alert("Error!", "File Name is empty. Please provide file name", null, AlertType.ERROR); alert.setTimeout(Alert.FOREVER); Display.getDisplay(this).setCurrent(alert); } else { // Create file in a separate thread and disable all commands // except for "exit" executeCreateFile(newName, typeInput.getSelectedIndex() != 0); Display.getDisplay(this).getCurrent().removeCommand(creatOK); Display.getDisplay(this).getCurrent().removeCommand(back); } } else if (c == back) { showCurrDir(); } else if (c == exit) { destroyApp(false); } else if (c == delete) { List curr = (List)d; String currFile = curr.getString(curr.getSelectedIndex()); executeDelete(currFile); } } void delete(String currFile) { if (!currFile.equals(UP_DIRECTORY)) { if (currFile.endsWith(SEP_STR)) { checkDeleteFolder(currFile); } else { deleteFile(currFile); showCurrDir(); } } else { Alert cantDeleteFolder = new Alert("Error!", "Can not delete The up-directory (..) " + "symbol! not a real folder", null, AlertType.ERROR); cantDeleteFolder.setTimeout(Alert.FOREVER); Display.getDisplay(this).setCurrent(cantDeleteFolder); } } private void executeDelete(String currFile) { final String file = currFile; new Thread(new Runnable() { public void run() { delete(file); } }).start(); } private void checkDeleteFolder(String folderName) { try { FileConnection fcdir = (FileConnection)Connector.open("file://localhost/" + currDirName + folderName); Enumeration content = fcdir.list("*", true); //only empty directory can be deleted if (!content.hasMoreElements()) { fcdir.delete(); showCurrDir(); } else { Alert cantDeleteFolder = new Alert("Error!", "Can not delete The non-empty folder: " + folderName, null, AlertType.ERROR); cantDeleteFolder.setTimeout(Alert.FOREVER); Display.getDisplay(this).setCurrent(cantDeleteFolder); } } catch (IOException ioe) { System.out.println(currDirName + folderName); ioe.printStackTrace(); } } //Starts creatFile with another Thread private void executeCreateFile(final String name, final boolean val) { new Thread(new Runnable() { public void run() { createFile(name, val); } }).start(); } /** * Show file list in the current directory . */ void showCurrDir() { Enumeration e; FileConnection currDir = null; List browser;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -