📄 txtfilelist.java
字号:
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
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;
public class TxtFileList extends List implements CommandListener{
private static final long fileSize = 358400;
/**
* 可以显示的文件类型
*/
private static final String[] fileType ={".txt",".TXT"};
/**
* 向上文件夹名
*/
public static final String UP_DIRECTORY = "..";
/**
* 根目录名
*/
public static final String MEGA_ROOT = "/";
/**
* 分隔符
*/
public static final char SEP = '/';
/**
* 分隔符的字符串形式
*/
public static final String SEP_STR = "/";
private static Command CMD_back = new Command("返回",Command.BACK,1);
private static Command CMD_open = new Command("打开",Command.SCREEN,2);
private static Command CMD_delete = new Command("删除",Command.SCREEN,2);
private static Command CMD_Yes = new Command("是",Command.OK,2);
private static Command CMD_No = new Command("否",Command.CANCEL,1);
private static Command CMD_partition = new Command("分割文件",Command.SCREEN,2);
private static Command CMD_YesForPartition = new Command("是",Command.OK,2);
/**
* 当前目录
*/
private String curDir;
/**
* 当前关联的MIDlet
*/
private TxtReader bookReader;
//各个ICON
private Image InfoIcon = null;
private Image ErrorIcon = null;
private Image FileIcon = null;
private Image FolderIcon = null;
//内部线程类,用于运行Open方法
private class openThread extends Thread{
private TxtFileList TFL;
public openThread(TxtFileList l){
TFL = l;
}
public void run() {
TFL.open(TFL.getString(TFL.getSelectedIndex()));
}
}
private class deleteThread extends Thread{
private TxtFileList TFL;
public deleteThread(TxtFileList l){
TFL = l;
}
public void run(){
TFL.deleteFile(TFL.getString(TFL.getSelectedIndex()));
}
}
/**构造一个TxtFileList
* @param title 标题
* @param listType 列表类型,同List
* @param MID 关联的MIDlet
*/
public TxtFileList(TxtReader MID){
super("File List",List.IMPLICIT);
curDir = MEGA_ROOT;
bookReader = MID;
try{
ErrorIcon = Image.createImage("/icon/Error.png");
InfoIcon = Image.createImage("/icon/Info.png");
FileIcon = Image.createImage("/icon/File.png");
FolderIcon = Image.createImage("/icon/Folder.png");
} catch (Exception e){
e.printStackTrace();
}
this.addCommand(CMD_back);
this.addCommand(CMD_open);
this.addCommand(CMD_delete);
this.addCommand(CMD_partition);
this.setSelectCommand(CMD_open);
this.setCommandListener(this);
}
/**
* 获得当前目录的所有文件和文件夹
*/
private boolean getCurDirList() {
Enumeration e = null;
FileConnection currDir = null;//当前目录
try {
/*当前目录是根目录*/
if (MEGA_ROOT.equals(curDir)) {
e = FileSystemRegistry.listRoots();
}
else {
/*当前目录不是根目录*/
currDir = (FileConnection)Connector.open("file://localhost/" + curDir);
e = currDir.list();//获得目录列表
}
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (SecurityException se){
return false;
}
if(0 != this.size()){
this.deleteAll();
}
//当不是根目录时
if(!MEGA_ROOT.equals(curDir)){
this.append(UP_DIRECTORY, FolderIcon);//添加向上目录
}
List temp = new List("temp",List.IMPLICIT); //临时列表存放文件
while (e.hasMoreElements()) {
String fileName = (String)e.nextElement();
//若是文件夹则添加到当前列表
if (fileName.charAt(fileName.length() - 1) == SEP) {
this.append(fileName, FolderIcon);
}
//否则为文件,符合条件则添加到临时列表
else {
String appendName = fileName.substring(fileName.length() - 4);
for(int i = 0 ; i < fileType.length ; i++){
if(appendName.equals(fileType[i])){
temp.append(fileName, null);
break;
}
}
}
}
for(int i = 0 ; i < temp.size();i++){
this.append(temp.getString(i), FileIcon) ;
}
temp.deleteAll();
temp = null;
e = null;
return true;
}
/**转入目标目录,获得目录中的所有文件和文件夹,并显示
* @param dirName 目录名
*/
private void traverseDir(String dirName) {
String lastDir = curDir;
if (curDir.equals(MEGA_ROOT)) {
if (dirName.equals(UP_DIRECTORY)) {
return;
}
curDir = dirName;
} else if (dirName.equals(UP_DIRECTORY)) {
int i = curDir.lastIndexOf(SEP, curDir.length() - 2);
if (i != -1) {
curDir = curDir.substring(0, i + 1);
}
else {
curDir = MEGA_ROOT;
}
}
else {
curDir = curDir + dirName;
}
if(!getCurDirList())
curDir = lastDir;
Display.getDisplay(bookReader).setCurrent(this);
}
/**打开目标文件
* @param fileName 目标文件名
*/
private void openFile(String fileName){
String path = "file://localhost/" + curDir + fileName;
long size = 0;
try{
FileConnection fc = (FileConnection)Connector.open(path);
size = fc.fileSize();
} catch(IOException e){
Display.getDisplay(bookReader).setCurrent( new Alert("错误","文件不存在!",ErrorIcon,AlertType.ERROR),this);
e.printStackTrace();
} catch(SecurityException se){
return;
}
//到这里已经可以确定文件存在
if(size > TxtFileList.fileSize){
Alert partitionAlert = new Alert("提示","该文件过大,请分割文件后打开,是否要分割这个文件?",InfoIcon,AlertType.INFO);
partitionAlert.addCommand(CMD_No);
partitionAlert.addCommand(CMD_YesForPartition);
partitionAlert.setCommandListener(this);
Display.getDisplay(bookReader).setCurrent(partitionAlert,this);
}else {
HistoryRecordList hrl = (HistoryRecordList)bookReader.getDisplayable(TxtReader.HISTORYRECORDLIST);
hrl.addFile(fileName,path);
//目录重置
curDir = MEGA_ROOT;
this.getCurDirList();
ReadingBoard d = (ReadingBoard)bookReader.getDisplayable(TxtReader.READINGBORD);
//每次打开文件时对空间进行回收
System.gc();
//这个为判断是否许可读取文件,不许可则保持在当前列表
if (d.selectFile(path, false)){
Display.getDisplay(bookReader).setCurrent(d);
}
}
}
/**响应CMD_open Command命令的方法
* @param fileName 文件名或文件夹名
*/
private void open(String fileName){
//若是文件夹
if(fileName.charAt(fileName.length() - 1) == TxtFileList.SEP || fileName.equals(TxtFileList.UP_DIRECTORY)){
this.traverseDir(fileName);
}else{
this.openFile(fileName);
}
}
/**
* 返回主界面
*/
/**删除指定文件
* @param fileName 指定文件名
*/
private void deleteFile(String fileName){
if(fileName.endsWith(SEP_STR) || fileName.equals(UP_DIRECTORY)){
Alert errAlert = new Alert("错误","不能删除文件夹!",ErrorIcon,AlertType.ERROR);
Display.getDisplay(bookReader).setCurrent(errAlert,this);
}else {
try{
FileConnection fc = (FileConnection)Connector.open("file://localhost/" + curDir + fileName);
fc.delete();
getCurDirList();
Display.getDisplay(bookReader).setCurrent(this);
}catch(Exception e){
Alert errAlert =
new Alert("错误","不能删除 " + "在" + curDir + "目录下的文件"+ fileName +
"\nException: " + e.getMessage(), ErrorIcon, AlertType.ERROR);
errAlert.setTimeout(Alert.FOREVER);
Display.getDisplay(bookReader).setCurrent(errAlert);
}
}
}
private void partitionFile(String path){
FileConnection fc = null;
boolean needPartition = false;
InputStream is = null;
try{
fc = (FileConnection)Connector.open(path);
if(fc.isDirectory())
Display.getDisplay(bookReader).setCurrent(new Alert("错误","请选择文件!",ErrorIcon,AlertType.ERROR),this);
else if(fc.fileSize() > TxtFileList.fileSize)
needPartition = true;
is = fc.openInputStream();
} catch(Exception e){
System.out.println("Error! here" + e.getMessage());//
e.printStackTrace();
}
if(needPartition){
FileConnection fcTemp = null;
String subPath = path.substring(0, path.length() - 4);
String type = ".txt";
//数据
char[] data = new char[(int)TxtFileList.fileSize / 2];
//读入的长度
int length = 0;
try{
InputStreamReader isr = new InputStreamReader(is);
OutputStream os = null;
OutputStreamWriter osr = null;
for(int i = 0;;i++){
length = isr.read(data);
if (length==-1){
break;
}
fcTemp = (FileConnection)Connector.open(subPath + i + type);
if(fcTemp.exists()){
fcTemp.close();
i++;
continue;
}
fcTemp.create();
os = fcTemp.openOutputStream();
osr = new OutputStreamWriter(os);
if (length==(int)TxtFileList.fileSize / 2)
osr.write(data);
else
osr.write(data, 0, length + 10);
os.close();
fcTemp.close();
data = new char[(int)TxtFileList.fileSize / 2];
System.gc();
}
os.close();
is.close();
fc.close();
} catch(Exception e){
e.printStackTrace();
}
} else{
Display.getDisplay(bookReader).setCurrent(new Alert("提示","该文件不需要分割!",InfoIcon,AlertType.INFO), this);
}
}
private class partitionThread extends Thread{
private TxtFileList TFL;
public partitionThread(TxtFileList l){
TFL = l;
}
public void run(){
String path = "file://localhost/" + curDir + TFL.getString(TFL.getSelectedIndex());
System.out.println(path);//
//if(path.charAt(path.length() - 1) != TxtFileList.SEP){
TFL.partitionFile(path);
TFL.getCurDirList();
Display.getDisplay(bookReader).setCurrent(TFL);
//}
}
}
public void showList(){
if(getCurDirList())
Display.getDisplay(bookReader).setCurrent(this);
else
Display.getDisplay(bookReader).setCurrent(bookReader.getDisplayable(TxtReader.MAINMANU));
}
private class backThread extends Thread{
private TxtFileList TFL;
public backThread(TxtFileList l){
TFL = l;
}
public void run(){
TFL.curDir = TxtFileList.MEGA_ROOT;
if(TFL.getCurDirList())
Display.getDisplay(bookReader).setCurrent(bookReader.getDisplayable(TxtReader.MAINMANU));
else
Display.getDisplay(bookReader).setCurrent(TFL);
}
}
public void commandAction(Command c, Displayable d) {
if(c == CMD_back){
new backThread(this).start();
}else if(c == CMD_open){
new openThread(this).start();
}else if(c == CMD_delete){
Alert warningAlert = new Alert("确认","删除这个文件?",InfoIcon,AlertType.INFO);
warningAlert.addCommand(CMD_No);
warningAlert.addCommand(CMD_Yes);
warningAlert.setCommandListener(this);
Display.getDisplay(bookReader).setCurrent(warningAlert);
}else if(c == CMD_Yes){
new deleteThread(this).start();
}else if(c == CMD_partition){
new partitionThread(this).start();
}else if(c == CMD_YesForPartition){
new partitionThread(this).start();
}else {
Display.getDisplay(bookReader).setCurrent(this);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -