📄 filelistform.java
字号:
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.util.Enumeration;
import javax.microedition.io.file.*;
import java.io.*;
public class FileListForm extends Form implements CommandListener, ItemCommandListener, Runnable
{
private int isMusic = 0;
private FileForm fileForm = null;
private MainSelect mainSelect = null;
//备份还原相关变量
private int threadType = 0;
private String revertFile, musicFile;
private Enumeration fileEnum = null;
//private String fileSeparator = null;
private Command exit, back, enter, select, save;
private FileConnection fc = null;
private Flash flash = null;
private Thread getThread = null;
//每次调用getRootList()后,将其置true,表示当前处于根目录
private boolean isRooting = false, isFirstDir = false;
public FileListForm(int isMusic, Displayable dis)throws SecurityException
{
super("文件浏览器");
flash = new Flash();
flash.geting();
flash.startFlash();
//fileSeparator = System.getProperty("file.separator");
//System.out.print("FileSeparator为:" + fileSeparator);
this.isMusic = isMusic;
if(isMusic == 0)
fileForm = (FileForm)dis;
else
mainSelect = (MainSelect)dis;
if(isMusic == 2)
save = new Command("Save", Command.OK, 1);
exit = new Command("Exit", Command.EXIT, 1);
back = new Command("Back", Command.EXIT, 1);
enter = new Command("Enter", Command.OK, 1);
select = new Command("Select", Command.OK, 1);
addCommand(back);
setCommandListener(this);
//System.out.println("开始启动线程");
try
{
startGetThread(null, true, false);
}
catch(SecurityException se)
{
throw se;
}
//System.out.println("启动线程成功");
}
//自定义内部获取文件目录线程
private void startGetThread(final String dirName, final boolean isDir, final boolean isBack)throws SecurityException
{
getThread=new Thread()
{
public void run()
{
if(!isBack)
{
if(dirName == null && isDir)
{
try
{
getRootList();
}
catch(SecurityException se)
{
throw se;
}
}
else
getFileList(dirName, isDir);
}
else
{
getList(fc);
}
stopGetThread();
}
};
getThread.start();
}
private void stopGetThread()
{
Chess.dis.setCurrent(this);
flash.stopFlash();
}
private void getRootList()
{
deleteAll();
isRooting = true;
StringItem fileItem = null;
//System.out.println("开始获取Root集合");
Enumeration e = FileSystemRegistry.listRoots();
//System.out.println("获取Root集合完毕");
while (e.hasMoreElements())
{
String rootName = (String) e.nextElement();
//System.out.println("获取的根目录名字是:" + rootName);
//根目录与一般目录的区别
rootName = rootName + "/";
fileItem = new StringItem(null, rootName, Item.PLAIN);
fileItem.setDefaultCommand(enter);
fileItem.setItemCommandListener(this);
append(fileItem);
append("\n");
}
//System.out.println("遍历Root集合完毕");
}
private void getFileList(String dirName, boolean isRoot)
{
isRooting = false;
try
{
if(isRoot)
{
isFirstDir = true;
//System.out.println("开始打开您所选择的根目录:" + dirName);
fc = (FileConnection)Connector.open("file://localhost/" + dirName);
//System.out.println("打开您所选择的根目录完毕:"+ fc.getPath());
}
else
{
isFirstDir = false;
//System.out.println("开始打开您所选择的一般目录:" + dirName);
//System.out.println("重新定位前的目录是:" + fc.getPath());
fc.setFileConnection(dirName);
//System.out.println("重新定位后的目录是:" + fc.getPath());
//fc = (FileConnection)Connector.open("file://localhost" + fc.getPath() + dirName);
//System.out.println("附加后的目录是:" + fc.getPath());
}
}
catch(IOException ie)
{
//System.out.println("设置指向目录时出现异常");
}
getList(fc);
}
private void getList(FileConnection tempFC)
{
deleteAll();
StringItem fileItem = null;
Enumeration e = null;
try
{
//System.out.println("列出目录……");
e = fc.list();//这儿可再构造一个方法,专门用来获取文件列表,在方法里面对目录和文件类型做分类决定显示哪个。
//System.out.println("列出目录完毕");
}
catch(IOException ie)
{
//System.out.println("列出目录时出现异常");
}
while (e.hasMoreElements())
{
String fileName = (String) e.nextElement();
fileItem = new StringItem(null, fileName, Item.PLAIN);
String text = fileItem.getText();
if(text.charAt(text.length() - 1) == '/')
{
fileItem.setDefaultCommand(enter);
if(isMusic == 2)
fileItem.addCommand(save);
}
else
{
if(isMusic == 2)
{//寻找备份路径
fileItem.setDefaultCommand(save);
}
else
{//寻找备份文件
fileItem.setDefaultCommand(select);
}
}
fileItem.setItemCommandListener(this);
append(fileItem);
append("\n");
}
}
public void commandAction(Command com, Displayable dis)
{
if(com == back)
{
if(isRooting)
{
//System.out.println("当前处于根目录");
if(isMusic == 0)
Chess.dis.setCurrent(fileForm);
else
Chess.dis.setCurrent(mainSelect);
return;
}
if(isFirstDir)
{//当前处于一级目录中
//获取根目录
flash.startFlash();
try
{
startGetThread(null, true, false);
}
catch(SecurityException se)
{
}
}
else
{//当前处于非一级目录中
//获取当前目录下的文件
flash.startFlash();
try
{
fc.setFileConnection("..");
}
catch(IOException ie)
{
//System.out.println("指向上一级时出现IO异常");
//return;
}
catch(Exception e)
{
//System.out.println("指向上一级时出现异常");
try
{
startGetThread(null, true, false);
}
catch(SecurityException se)
{
}
return;
}
/*String tempPath = fc.getPath();
System.out.println(tempPath);
tempPath = tempPath.substring(0, tempPath.length() - 1);
System.out.println(tempPath);
int lastIndex = tempPath.lastIndexOf('/');
System.out.println(lastIndex);
//String fileName = tempPath.substring(lastIndex + 1, tempPath.length() - lastIndex - 1);
String fileName = tempPath.substring(0, lastIndex);
System.out.println(fileName);
try
{
fc = (FileConnection)Connector.open("file://localhost/" + fileName);
}
catch(IOException ie)
{
}
startGetThread(null, false, true);*/
try
{
startGetThread(null, false, true);
}
catch(SecurityException se)
{
}
}
}
}
public void run()
{//将当前路径作为备份文件的存放位置
//System.out.println("线程开始:"+fc.getURL());
if(threadType == 0)
{
mainSelect.setSavePath(fc.getURL());
//System.out.println("得到的备份路径是:" + fc.getURL());
}
else if(threadType == 1)
{
mainSelect.setRevertFile(fc.getURL() + "/" + revertFile);
//System.out.println("得到的还原文件路径是:" + fc.getURL() + revertFile);
}
else if(threadType == 2)
{
fileForm.addMusic(fc.getURL() + "/" + musicFile);
//System.out.println("得到的音乐文件路径是:" + fc.getURL() + musicFile);
Chess.dis.setCurrent(fileForm);
}
//System.out.println("线程结束");
}
public void commandAction(Command com, Item item)
{
if(com == enter)
{
flash.startFlash();
String text = ((StringItem)item).getText();
String tempDirName = text.substring(0, text.length() - 1);
if(tempDirName.charAt(tempDirName.length() - 1) == '/')
{//打开根目录
tempDirName = tempDirName.substring(0, text.length() - 1);
try
{
startGetThread(tempDirName, true, false);
}
catch(SecurityException se)
{
}
}
else
{//打开普通目录
try
{
startGetThread(tempDirName, false, false);
}
catch(SecurityException se)
{
}
}
return;
}
else if(com == select)
{//选择了文件
//System.out.println("当前文件的路径是:" + fc.getPath());
//System.out.println("当前文件的URL是:" + fc.getURL());
if(isMusic == 0)
{
threadType = 2;
musicFile = ((StringItem)item).getText();
new Thread(this).start();
}
else if(isMusic == 1)
{//开始用选择到的文件进行还原
threadType = 1;
revertFile = ((StringItem)item).getText();
new Thread(this).start();
}
}
else if(com == save)
{
threadType = 0;
new Thread(this).start();
}
try
{
fc.close();
}
catch(IOException ie)
{
//System.out.println("文件关闭失败");
}
}
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -