@SuppressWarnings("unchecked")
public void reader() throws IOException, ClassNotFoundException {
file = new File("src/com/lzf/www/dao/1.txt");
if (!file.exists()) {
file.createNewFile();
}
ois = new ObjectInputStream(new FileInputStream(file));
list = (List<Employee>) ois.readObject(); // 给list赋值
ois.close();
}
/**
* 写文件
* @throws IOException
* @throws ClassNotFoundException
*/
public void writer() throws IOException, ClassNotFoundException {
file = new File("src/com/lzf/www/dao/1.txt");
if (!file.exists()) {
file.createNewFile();
}
oos = new ObjectOutputStream(new FileOutputStream(file));
for(Employee e:list){
oos.writeObject(e.toString()+"\r\n"); // 将list值写进文件里去
}
oos.flush();
oos.close();
}
java io 写不进数据,读取一直是null
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
5条回答 默认 最新
- 夕阳雨晴 2017-04-18 04:46关注
file = new File("src/com/lzf/www/dao/1.txt");
if (!file.exists()) {
file.createNewFile();
}读取不到文件就自己创建,创建的内容怎么可能不为空?一般是需要确定file不为空,读取到文件,如果找不到文件,直接抛异常即可。
解决 无用评论 打赏 举报
悬赏问题
- ¥15 程序不包含适用于入口点的静态Main方法
- ¥15 素材场景中光线烘焙后灯光失效
- ¥15 请教一下各位,为什么我这个没有实现模拟点击
- ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
- ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
- ¥20 有关区间dp的问题求解
- ¥15 多电路系统共用电源的串扰问题
- ¥15 slam rangenet++配置
- ¥15 有没有研究水声通信方面的帮我改俩matlab代码
- ¥15 ubuntu子系统密码忘记