源码:
/*************************************************************/
/*软件名:手机文件管理器 */
/*程序名:biyan.java */
/*作者:牛志强 */
/*日期:2008.5 */
/*************************************************************/
import java.util.*;
import java.io.*;
import javax.microedition.io.*;
import javax.microedition.io.file.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class biyan extends MIDlet implements CommandListener{
private Form f;
private Display display;
private String currdirname;
private TextField inputpath;
private TextField enterpass;
private TextField againpass;
private Ticker tickerMsg=new Ticker("加密");
private Ticker ticker=new Ticker("解密");
private static final int chunk_size=1024;
private Command view =new Command("浏览",Command.ITEM,1);
private Command creat=new Command("新建",Command.ITEM,2);
private Command delete=new Command("删除",Command.ITEM,3);
private Command creatok=new Command("OK",Command.OK,1);
private Command prop=new Command("属性",Command.ITEM,2);
private Command back=new Command("后退",Command.BACK,2);
private Command exit =new Command("退出",Command.EXIT,3);
private Command jiami=new Command("加密",Command.ITEM,4);
private Command jiemi=new Command("解密",Command.ITEM,5);
private Command jiamiok=new Command("OK",Command.OK,1);
private Command jiemiok=new Command("OK",Command.OK,1);
private TextField nameinput;
private ChoiceGroup typeinput;
private final static String[] attrlist={"读","写","隐藏"};
private final static String[] typelist={"文件","目录"};
private final static String up_directory="..";
private final static String mega_root="/";
private String sep_str="/";
private char sep='/';
public biyan() {
currdirname=mega_root;
// TODO 自动生成构造函数存根
}
public void destroyApp(boolean arg0){
// TODO 自动生成方法存根
}
public void pauseApp() {
// TODO 自动生成方法存根
}
public void startApp() {
new Thread(new Runnable(){
public void run(){
jiemian();
try{Thread.sleep(3000);
showCurrDir();
}catch(SecurityException e){
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
}
}).start();
// TODO 自动生成方法存根
}
public void jiemian(){
display=Display.getDisplay(this);
Image img;
try{
img=Image.createImage("/1.png");
}catch(java.io.IOException e){
img=null;
}
Alert al=new Alert("东信手机文件管理器~","作者:牛志强/n指导老师:张辉",img,null);
al.setTimeout(3000);
display.setCurrent(al);
}
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{
int location=currfile.indexOf(".");
String extendname=currfile.substring(location+1);
if(extendname.endsWith("jpg")||extendname.endsWith("jpeg"))
showImage(currfile);
else
showFile(currfile);
}
}
}).start();
}else if(c==prop){
List curr=(List)d;
final String currfile=curr.getString(curr.getSelectedIndex());
new Thread(new Runnable(){
public void run(){
showProperties(currfile);}
}).start();
}else if(c==jiami){
jiami();
}else if(c==jiamiok){
new Thread(new Runnable(){
public void run(){
jiamiok();}
}).start();
}else if(c==jiemi){
jiemi();
}else if(c==jiemiok){
new Thread(new Runnable(){
public void run(){
jiemiok();}
}).start();
}else if(c==creat){
createFile();
}else if(c==creatok){
String newname=nameinput.getString();
if(newname==null||newname.equals("")){
Alert alert=new Alert("错误!",
"文件名不能为空",
null,
AlertType.ERROR);
alert.setTimeout(Alert.FOREVER);
Display.getDisplay(this).setCurrent(alert);
}else{
executeCreateFile(newname,typeinput.getSelectedIndex()!=0);
Display.getDisplay(this).getCurrent().removeCommand(creatok);
Display.getDisplay(this).getCurrent().removeCommand(back);
}
}else if(c==back){
new Thread(new Runnable(){
public void run(){
showCurrDir();}
}).start();
}else if(c==exit){
notifyDestroyed();
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)){
checkDelteFolder(currfile);
}else{
deleteFile(currfile);
showCurrDir();
}
}else{
Alert cantDeleteFolder=new Alert("Error!","无法删除上级目录",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 checkDelteFolder(String foldername){
try{
FileConnection fcdir=(FileConnection)Connector.open("file:///"+currdirname+foldername);
Enumeration content=fcdir.list("*",true);
if(!content.hasMoreElements()){
fcdir.delete();
showCurrDir();
}else{
Alert cantDeleteFolder=new Alert("错误!","不能删除非空目录:"+foldername,null,AlertType.ERROR);
cantDeleteFolder.setTimeout(Alert.FOREVER);
Display.getDisplay(this).setCurrent(cantDeleteFolder);
}
}catch(IOException ioe){
System.out.print(currdirname+foldername);
ioe.printStackTrace();
}
}
private void executeCreateFile(final String name,final boolean val){
new Thread(new Runnable(){
public void run(){
createFile(name,val);
}
}).start();
}
void showCurrDir(){/*主要方法*/
Enumeration e;
FileConnection currdir=null;
List browser;
try{
if(mega_root.equals(currdirname)){
e=FileSystemRegistry.listRoots();
browser=new List(currdirname,List.IMPLICIT);
}else{
currdir=(FileConnection)Connector.open("file:///"+currdirname);
e=currdir.list("*",true);
browser=new List(currdirname,List.IMPLICIT);
browser.append(up_directory, null);
}
while(e.hasMoreElements()){
String filename=(String)e.nextElement();
if(filename.charAt(filename.length()-1)==sep){
browser.append(filename, null);
}else{
browser.append(filename, null);
}
}
browser.setSelectCommand(view);
if(!mega_root.equals(currdirname)){
browser.addCommand(prop);
browser.addCommand(creat);
browser.addCommand(delete);
browser.addCommand(jiami);
browser.addCommand(jiemi);
}
browser.addCommand(exit);
browser.setCommandListener(this);
if(currdir!=null){
currdir.close();
}
Display.getDisplay(this).setCurrent(browser);
}catch(IOException ioe){
ioe.printStackTrace();
}
}
void traverseDirectory(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;
}
showCurrDir();
}
void showFile(String filename){
try{
FileConnection fc=(FileConnection)
Connector.open("file:///"+currdirname+filename);
if(!fc.exists()){
throw new IOException("文件不存在");
}
InputStream fis=fc.openInputStream();
byte[] b=new byte[1024];
int length=fis.read(b,0,1024);
fis.close();
fc.close();
TextBox viewer=new TextBox("查看文件:"+filename,null,1024,TextField.ANY|TextField.UNEDITABLE);
viewer.addCommand(back);
viewer.addCommand(exit);
viewer.setCommandListener(this);
if(length>0){
viewer.setString(new String(b,0,length));
}
Display.getDisplay(this).setCurrent(viewer);
}catch(Exception e){
Alert alert=new Alert("错误!","在目录"+currdirname+"中不能访问文件"+filename+"/意外:"+e.getMessage(),null,AlertType.ERROR);
alert.setTimeout(Alert.FOREVER);
Display.getDisplay(this).setCurrent(alert);
}
}
void deleteFile(String filename){
try{
FileConnection fc=(FileConnection) Connector.open("file:///"+currdirname+filename);
fc.delete();
}catch(Exception e){
Alert alert=new Alert("错误!","在目录"+currdirname+"中无法删除文件"+filename+"/n意外"+e.getMessage(),null,AlertType.ERROR);
alert.setTimeout(Alert.FOREVER);
Display.getDisplay(this).setCurrent(alert);
}
}
void showProperties(String filename){
try{
if(filename.equals(up_directory)){
return;
}
FileConnection fc=(FileConnection)Connector.open("file:///"+currdirname+filename);
if(!fc.exists()){
throw new IOException("文件不存在");
}
Form props=new Form("文件属性"+filename);
ChoiceGroup attrs=new ChoiceGroup("属性:",Choice.MULTIPLE,attrlist,null);
attrs.setSelectedFlags(new boolean[]{fc.canRead(),fc.canWrite(),fc.isHidden()});
props.append(new StringItem("位置:",currdirname));
props.append(new StringItem("类型:",fc.isDirectory()?"目录":"文件"));
props.append(attrs);
props.addCommand(back);
props.addCommand(exit);
props.setCommandListener(this);
fc.close();
Display.getDisplay(this).setCurrent(props);
}catch(Exception e){
Alert alert=new Alert("错误!","在目录"+currdirname+"中不能访问文件"+filename+"/n意外:"+e.getMessage(),null,AlertType.ERROR);
alert.setTimeout(Alert.FOREVER);
Display.getDisplay(this).setCurrent(alert);
}
}
void createFile(){
Form creator=new Form("文件创建");
nameinput=new TextField("输入文件名",null,256,TextField.ANY);
typeinput=new ChoiceGroup("输入文件类型",Choice.EXCLUSIVE,typelist,null);
creator.append(nameinput);
creator.append(typeinput);
creator.addCommand(creatok);
creator.addCommand(back);
creator.addCommand(exit);
creator.setCommandListener(this);
Display.getDisplay(this).setCurrent(creator);
}
void createFile(String newname,boolean isDirectory){
try{
FileConnection fc=(FileConnection) Connector.open("file:///"+currdirname+newname);
if(isDirectory){
fc.mkdir();
}else{
fc.create();
}
showCurrDir();
}catch(Exception e){
String s="不能创建文件'"+newname+ "'";
if(e.getMessage()!=null&&e.getMessage().length()>0){
s+="/n"+e;
}
Alert alert=new Alert("错误!",s,null,AlertType.ERROR);
alert.setTimeout(Alert.FOREVER);
Display.getDisplay(this).setCurrent(alert);
Display.getDisplay(this).getCurrent().addCommand(creatok);
Display.getDisplay(this).getCurrent().addCommand(back);
}
}
public void showImage(String imgname){
try{
FileConnection fileconn=(FileConnection)
Connector.open("file:///"+currdirname+imgname,Connector.READ);
if(!fileconn.exists()){
throw new IOException("文件不存在");
}
InputStream fis=fileconn.openInputStream();
long overallsize=fileconn.fileSize();
int length=0;
byte[] imagedata=new byte[0];
while(length<overallsize){
byte[] data=new byte[chunk_size];
int readamount=fis.read(data,0,chunk_size);
byte[] newimagedata=new byte[imagedata.length+chunk_size];
System.arraycopy(imagedata, 0, newimagedata, 0, length);
System.arraycopy(data, 0, newimagedata, length, readamount);
imagedata=newimagedata;
length+=readamount;
}
fis.close();
fileconn.close();
Image currentimage=Image.createImage(imagedata,0,length);
Form f=new Form(imgname);
f.append(currentimage);
f.addCommand(back);
f.addCommand(exit);
f.setCommandListener(this);
Display.getDisplay(this).setCurrent(f);
}catch(IOException e){
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
}
void jiami(){
f=new Form("加密");
inputpath=new TextField("请输入文件名",null,120,TextField.ANY);
enterpass = new TextField("enterpass", null, 120, TextField.ANY | TextField.PASSWORD);
againpass = new TextField("againpass", null, 120, TextField.ANY | TextField.PASSWORD);
f.append(inputpath);
f.append(enterpass);
f.append(againpass);
f.addCommand(jiamiok);
f.addCommand(back);
f.setTicker(tickerMsg);
f.setCommandListener(this);
Display.getDisplay(this).setCurrent(f);
}
void jiamiok(){
tickerMsg.setString("简单加密程序");
InputStream fileR=null;
OutputStream fileW=null;
String passfile=inputpath.getString();
if(passfile!=null&&!"".equals(passfile)){
if(enterpass.getString().equals(againpass.getString())){
int pas=getPass(enterpass.getString());
try {
fileR=Connector.openInputStream("file:///"+currdirname+passfile);
fileW=Connector.openOutputStream("file:///"+currdirname+passfile);
int b=fileR.read();
int xorB=0;
while(b!=-1){
xorB= b ^ pas;
fileW.write(xorB);
b=fileR.read();
}
tickerMsg.setString("加密成功 你的密码为 "+enterpass.getString()+" ,请记住");
} catch (Exception ex) {
tickerMsg.setString("加密失败~ 有错误"+ex.toString());
ex.printStackTrace();
} finally{
try{fileR.close();}catch(Exception e){}
try{fileW.close();}catch(Exception e){}
}
}else{
tickerMsg.setString("两次输入的密码不一样");
}
}else{
tickerMsg.setString("文件路径不能为空");
}
}
void jiemi(){
f=new Form("解密");
inputpath=new TextField("请输入文件名",null,120,TextField.ANY);
enterpass = new TextField("enterpass", null, 120, TextField.ANY | TextField.PASSWORD);
againpass = new TextField("againpass", null, 120, TextField.ANY | TextField.PASSWORD);
f.append(inputpath);
f.append(enterpass);
f.append(againpass);
f.addCommand(jiemiok);
f.addCommand(back);
f.setTicker(ticker);
f.setCommandListener(this);
Display.getDisplay(this).setCurrent(f);
}
void jiemiok(){
ticker.setString("简单解密程序");
InputStream fileR=null;
OutputStream fileW=null;
String passfile=inputpath.getString();
if(passfile!=null&&!"".equals(passfile)){
if(enterpass.getString().equals(againpass.getString())){
int pas=getPass(enterpass.getString());
try {
fileR=Connector.openInputStream("file:///"+currdirname+passfile);
fileW=Connector.openOutputStream("file:///"+currdirname+passfile);
int b=fileR.read();
int xorB=0;
while(b!=-1){
xorB= b ^ pas;
fileW.write(xorB);
b=fileR.read();
}
ticker.setString("解密成功!~ ");
} catch (Exception ex) {
ticker.setString("解密失败~ 有错误"+ex.toString());
ex.printStackTrace();
} finally{
try{fileR.close();}catch(Exception e){}
try{fileW.close();}catch(Exception e){}
}
}else{
ticker.setString("两次输入的密码不一样");
}
}else{
ticker.setString("文件路径不能为空");
}
}
public int getPass(String pass){
byte[] passes=pass.getBytes();
int result=0;
for(int len=0;len<passes.length;len++){
result+=passes[len];
}
return result;
}
}