@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 微信会员卡接入微信支付商户号收款
- ¥15 如何获取烟草零售终端数据
- ¥15 数学建模招标中位数问题
- ¥15 phython路径名过长报错 不知道什么问题
- ¥15 深度学习中模型转换该怎么实现
- ¥15 HLs设计手写数字识别程序编译通不过
- ¥15 Stata外部命令安装问题求帮助!
- ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
- ¥15 TYPCE母转母,插入认方向
- ¥15 如何用python向钉钉机器人发送可以放大的图片?