怎么样在程序结束后还保存数据!

driftofking 2008-02-25 07:24:02
就是用JCreator,做一个简单的程序,在程序结束后保存一个字符串(例如保存String类型s这个字符串),然后再次执行此程序时,s的初始值就为上次执行此程序时s的结果。
...全文
193 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
duzhonghua 2008-02-26
  • 打赏
  • 举报
回复
我的是放入文件当中的...
package me;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
class ConfigFile{
public static final String Conf="d:/dzh.txt";
}
class CreateFile {
private File myFile = null;

public CreateFile() {

myFile = new File(ConfigFile.Conf);

}

public File getMyFile() {
return myFile;
}

public void setMyFile(File myFile) {
this.myFile = myFile;
}

}

public class FileRead {
public static void main(String[] args) {
CreateFile create = new CreateFile(); // 实例化createFile
FileInputStream ps = null;
PrintWriter pw = null;
BufferedReader buf=null;
String temp = null;
String result="";
// 先从文件读信息
try {
ps = new FileInputStream(create.getMyFile());
buf=new BufferedReader(new InputStreamReader(ps));

temp = buf.readLine();
while(temp!=null)
{
result+=temp;
temp=buf.readLine();

}

} catch (Exception e) {
e.printStackTrace();
} finally {
try {
buf.close();
ps.close();
} catch (Exception ex) {
}

}

System.out.println("初始化的字符串为: " + result);



// 再将信息写到文件中
try {
buf = new BufferedReader(new InputStreamReader(
System.in));
temp = buf.readLine();
pw = new PrintWriter(create.getMyFile());
pw.write(temp);
pw.flush();
} catch (Exception e2) {
e2.printStackTrace();
}finally{
try {
buf.close();
pw.close();
} catch (Exception ex2) {
}
}

}
}


自己感觉有点乱,请高手指点!!!!!!
求摸小手 2008-02-26
  • 打赏
  • 举报
回复
数据库。小点的access
forestandriver23 2008-02-25
  • 打赏
  • 举报
回复
需要使用外部存储,即持久化到外存中去。程序结束后,内存数据清空,数据无法保留了。
诗海 2008-02-25
  • 打赏
  • 举报
回复
把这个数据写文件,或者写数据库

62,629

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧