weixin_38368325 2017-04-18 04:41 采纳率: 0%
浏览 2029

java io 写不进数据,读取一直是null

 @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();
        }
  • 写回答

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向钉钉机器人发送可以放大的图片?