第二个JAVA ME程序----批量添加后缀

作用:为指定文件夹的每个文件添加设定的后缀(jar好像不行,我也不知道为什么)
还不是很完美,这个程序就这样吧,下个程序再学习去了。

文件结构:
Replace类--------主类
AddSuffix类------添加后缀
Config类---------设置文件夹,后缀
RMS类------------保存设置
ConfigThread类---没有用到,就不贴了


//Replace类
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class Replace extends MIDlet implements CommandListener {
final private String help = "如果不设置并且以前没设置过,就默认为E:/UCDownloaded/Z/里的文件添加.jar,否则以你设置后的为准。";
final private String about = "作者:kyda\n版本:v1.1\nAll rights reserved.\n如果你有什么好的建议请站内信\n我的ID:jimieaaa";
// final static int dirnum = 1, sufnum = 2;

private Display display;
private Command exitCommand, actCommand, configCommand, helpCommand, aboutCommand;
private Form fm;
private Alert helpAlert, aboutAlert;
static public String sDir, sSuf;
private RMS rms;
private AddSuffix addSuffix;
private Config config;

public Replace() {
rms = new RMS();
sDir = rms.getConfig(1);
sSuf = rms.getConfig(2);
display = Display.getDisplay(this);
fm = new Form("批量添加后缀");

exitCommand = new Command("退出", Command.EXIT, 2);
actCommand = new Command("替换", Command.OK, 2);
configCommand = new Command("设置", Command.ITEM, 2);
helpCommand = new Command("帮助", Command.ITEM, 2);
aboutCommand = new Command("关于", Command.ITEM, 2);

fm.addCommand(exitCommand);
fm.addCommand(actCommand);
fm.addCommand(configCommand);
fm.addCommand(helpCommand);
fm.addCommand(aboutCommand);

}

public void destroyApp(boolean arg0) throws MIDletStateChangeException {

}

public void pauseApp() {

}

public void startApp() throws MIDletStateChangeException {
//fm.append("sDir = " + sDir + "\nsSuf = " + sSuf + "\n");
fm.append("这是批量添加设定后缀的程序。\n\n");
//fm.append("还有点小bug,设置写好后按确定,然后再进入设置,这次按取消。这样就可以正常操作了。\n");
fm.append("请选择操作:\n");
display.setCurrent(fm);
fm.setCommandListener(this);

}

public void commandAction(Command cmd, Displayable arg1) {
if (cmd == exitCommand) {
try {
rms.closeRMS();
destroyApp(false);
} catch (MIDletStateChangeException e) {
e.printStackTrace();
}
notifyDestroyed();
} else if (cmd == actCommand) {
addSuffix = new AddSuffix(sDir, sSuf);
addSuffix.Replace(fm);
} else if (cmd == configCommand) {
//fm.append("sDir = " + sDir + "\nsSuf = " + sSuf + "\n");
config = new Config(display, fm, sDir, sSuf);
// configThread.start();
// try {
// configThread.join();
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// sDir = rms.getConfig(1);
// sSuf = rms.getConfig(2);
} else if (cmd == helpCommand) {
helpAlert = new Alert("帮助", help, null, AlertType.INFO);
display.setCurrent(helpAlert);
} else if (cmd == aboutCommand) {
aboutAlert = new Alert("关于", about, null, AlertType.CONFIRMATION);
display.setCurrent(aboutAlert);
}

}
}



//AddSuffix类
import java.io.*;
import java.util.*;
import javax.microedition.io.*;
import javax.microedition.io.file.*;
import javax.microedition.lcdui.Form;

public class AddSuffix {
private FileConnection fc, oldfile;
private Enumeration enums = null;
private String str, sName, sDir = "file:///", sSuf = ".", newfile = "";
private int mode;

public AddSuffix(String dir, String suf) {
sDir += dir;
sSuf += suf;
mode = 1;
}

public AddSuffix(String dir, String name, String suf) {
sDir += dir;
sName = name;
sSuf += suf;
mode = 2;
}

public void Replace(Form fm) {
fm.append("正在打开中...\n");
try {
fc = (FileConnection) Connector.open(sDir, Connector.READ);

if (!fc.exists()) {
fm.append("no folder!\n");
return;
}
enums = fc.list();

while (enums.hasMoreElements()) {
str = enums.nextElement().toString();
if (mode == 1)
sName = str;
try {
oldfile = (FileConnection) Connector.open(sDir + str, Connector.READ_WRITE);
newfile = sName + sSuf;
try {
oldfile.rename(newfile);
} catch (IOException e) {
fm.append(str + " 重命名失败!!!\n");
oldfile.close();
continue;
}
oldfile.close();
fm.append("重命名 " + str + " 成功!\n");
} catch (IOException e) {
fm.append("文件" + str + "打开失败!\n");
continue;
}
}
fc.close();
fm.append("添加后缀完成!\n");
} catch (IOException e) {
fm.append("文件夹打开失败!\n");
e.printStackTrace();
}
}
}



//Config类
import javax.microedition.lcdui.*;

public class Config implements CommandListener {
private Display display;
private TextField textFieldDir, textFieldSuf;
private Form configForm, fm;
private Command okCommand, cancelCommand;
private RMS rms;
private boolean isok = false;

public Config() {

}

public Config(Display display, Form fm, String dir, String suf) {

configForm = new Form("设置");
textFieldDir = new TextField("设置文件夹:", dir, 100, TextField.ANY);
configForm.append(textFieldDir);
textFieldSuf = new TextField("要添加的后缀:", suf, 10, TextField.ANY);
configForm.append(textFieldSuf);
configForm.append("\n\n注意:文件夹后要加'/'\n 后缀不要加'.'\n");

okCommand = new Command("确定", Command.OK, 2);
cancelCommand = new Command("取消", Command.CANCEL, 2);
configForm.addCommand(okCommand);
configForm.addCommand(cancelCommand);

this.display = display;
this.fm = fm;

configForm.setCommandListener(this);
display.setCurrent(configForm);
}

public boolean isOK() {
return isok;
}

public void commandAction(Command cmd, Displayable arg1) {
if (cmd == okCommand) {
rms = new RMS();
rms.setConfig(1, textFieldDir.getString());
rms.setConfig(2, textFieldSuf.getString());
//这里一直不能实现阻塞,只能用static的了,再学习去了
Replace.sDir = rms.getConfig(1);
Replace.sSuf = rms.getConfig(2);
rms.closeRMS();
isok = true;
display.setCurrent(fm);
} else if (cmd == cancelCommand) {
isok = true;
display.setCurrent(fm);
}
}

}



//RMS类
import javax.microedition.rms.*;

public class RMS {
final private String fDir = "E:/UCDownloaded/Z/";
final private String fSuf = "jar";
private RecordStore rms;
private String str = "";
private byte[] record = null;

public RMS() {
try {
rms = RecordStore.openRecordStore("Config", true);
if (rms.getNumRecords() == 0) {
record = fDir.getBytes();
rms.addRecord(record, 0, record.length);
record = fSuf.getBytes();
rms.addRecord(record, 0, record.length);
}
} catch (RecordStoreFullException e) {
e.printStackTrace();
} catch (RecordStoreNotFoundException e) {
e.printStackTrace();
} catch (RecordStoreException e) {
e.printStackTrace();
}
}

public String getConfig(int i) {
try {
record = rms.getRecord(i);
str = new String(record);
} catch (RecordStoreNotOpenException e) {
e.printStackTrace();
} catch (InvalidRecordIDException e) {
e.printStackTrace();
} catch (RecordStoreException e) {
e.printStackTrace();
}
return str;
}

public void setConfig(int i, String s) {
record = s.getBytes();
try {
rms.setRecord(i, record, 0, record.length);
} catch (RecordStoreNotOpenException e) {
e.printStackTrace();
} catch (InvalidRecordIDException e) {
e.printStackTrace();
} catch (RecordStoreFullException e) {
e.printStackTrace();
} catch (RecordStoreException e) {
e.printStackTrace();
}
}

public void closeRMS() {
try {
rms.closeRecordStore();
} catch (RecordStoreNotOpenException e) {
e.printStackTrace();
} catch (RecordStoreException e) {
e.printStackTrace();
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值