关于写入跟输出的两个问题
package com.demo.weather; import java.io.Serializable; public class Product implements Serializable{ private int ID; private String name; private String categories; private double price; public Product() {} public Product(int ID,String name,String categories,double price) { this.ID = ID; this.name = name; this.categories = categories; this.price = price; } public int getID() { return ID; } public void setID(int ID) { this.ID = ID; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCategories() { return categories; } public void setCategories(String categories) { this.categories = categories; } public double getPrice() { return price; } public void setPrice() { this.price = price; } public String toString() { return "产品ID:"+ ID + "\n产品名称:" + name + "\n产品属性:" + categories + "\n产品价格:" + price + "元"; } }
package com.demo.weather; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; public class ProductTest { public static void main(String[] args) { Product iphone = new Product(123,"iphone","telephone",4888.0); Product ipad = new Product(123,"ipad","computer",5088.0); Product macbook = new Product(345,"macbook","computer",10688.0); Product iwatch = new Product(256,"iwatch","watch",4799.0); File file = new File("File\\product.txt"); if(!file.exists()) { try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } try { FileOutputStream fos = new FileOutputStream(file); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(iphone); oos.writeObject(ipad); oos.writeObject(macbook); oos.writeObject(iwatch); oos.flush(); fos.close(); oos.close(); FileInputStream fis = new FileInputStream(file); ObjectInputStream ois = new ObjectInputStream(fis); try { System.out.println((Product)ois.readObject()); System.out.println((Product)ois.readObject()); System.out.println((Product)ois.readObject()); System.out.println((Product)ois.readObject()); fis.close(); ois.close(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
问题一:如何在写入时进行换行?
我使用的是 writeObject() 进行写入,但是如果使用writeChars("\n") 写入的话,输出也是不换行的。。只能在输出的时候加 System.out.println() 吗?
问题二:我使用(Product)ois.readObject() 进行四次输出,有没有办法使用循环的方法来输出。。。
没找到哪个方法像前面的课一样用 read() 来判断是否 等于-1
或是 readLine() 来判断是否 等于 null.
12
收起
正在回答
2回答
同学你好,1、在写入txt文件时,文件会自动过滤\n\r等符号哦,输出时使用readObject()方法也就读取不到不是对象的\n哦!所以这里还是使用System.out.println()来换行,更简便哦!
2、使用循环的方法来输出对象,同学可以参考如下代码哦,当输出到文件末尾后,再输出会报出EOFException已经读取到文件尾部,例如:
3、对象流不能引入缓冲流哦,因为BufferedWriter的构造方法中的参数只能是Writer以及其子类的流哦,但是ObjectOutputStream不属于writer下的子类哦!
如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~
1. Java 零基础入门
- 参与学习 人
- 提交作业 3802 份
- 解答问题 11489 个
本阶段带你迈入Java世界,学习Java必备基础知识,基础语法、面向对象思想以及常用工具类的使用。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星