2019-02-18 回答
1、可以使用简单的数据库,如sql server, mysql ,等(这个需要在java jdk中添加链接数据库的jar包,或者将对应的jar包加到你的java project中);
2、可以考虑使用java的简单存储,如下:
import java.awt.event.actionevent;
import java.awt.event.actionlistener;
import java.io.file;
import java.io.randomaccessfile;
import javax.swing.jbutton;
import javax.swing.jframe;
import javax.swing.joptionpane;
public class s extends jframe implements actionlistener {
private static final long serialversionuid = 1l;
private jbutton jb = null;
private int i = 0;
public void init() {
jb = new jbutton("保存");
jb.addactionlistener(this);
}
public s() {
init();
this.setlayout(null);
this.setsize(800, 600);
this.setvisible(true);
this.setdefaultcloseoperation(dispose_on_close);
this.add(jb).setbounds(100, 200, 120, 60);
}
// path为保存文件路径,content为输入框内容
public void save(string path, string content) {
file file = new file(path);
if (!file.exists()) {
file.mkdir();
}
if (file.isdirectory()) {
file.delete();
return ;
}
randomaccessfile raf = null;
try {
raf = new randomaccessfile(file, "rw");
raf.write(content.getbytes("gbk")); //将内容写入到固定位置的文件
raf.close();
} catch (exception e) {
e.printstacktrace();
}
}
@override
public void actionperformed(actionevent e) {
// todo auto-generated method stub
if (e.getsource() == jb) {
string content = joptionpane.showinputdialog("");
if (content != null) {
i++;
string path = "d:\\c" + i + ".txt";
save(path.trim(), content); // 保存到你要保存的位置
} else {
//这里还可以做其他操作
joptionpane.showconfirmdialog(getparent(), "请输入需要保存的内容!");
}
}
}
@suppresswarnings("unused")
public static void main(string[] args) {
// todo auto-generated method stub
s s = new s();
}
}