关于写入跟输出的两个问题

关于写入跟输出的两个问题

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.

正在回答

登陆购买课程后可参与讨论,去登陆

2回答

        同学你好,1、在写入txt文件时,文件会自动过滤\n\r等符号哦,输出时使用readObject()方法也就读取不到不是对象的\n哦!所以这里还是使用System.out.println()来换行,更简便哦!

        2、使用循环的方法来输出对象,同学可以参考如下代码哦,当输出到文件末尾后,再输出会报出EOFException已经读取到文件尾部,例如:

http://img1.sycdn.imooc.com//climg/5d38334100018f3605460255.jpg

        3、对象流不能引入缓冲流哦,因为BufferedWriter的构造方法中的参数只能是Writer以及其子类的流哦,但是ObjectOutputStream不属于writer下的子类哦!

http://img1.sycdn.imooc.com//climg/5d3834fc000165e405410150.jpg

http://img1.sycdn.imooc.com//climg/5d3835e40001e71b09700250.jpg

          如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~

提问者 XO鸡丝 2019-07-24 17:33:30

还有个疑问,如何引入缓存流。在对象的写入跟读出的时候。

  • 同学你好,这里老师在上面的回答中回复了同学这个问题哦! 继续加油,祝:学习愉快~
    2019-07-24 18:40:37
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师