乱码是怎么回事
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | import java.io.Serializable; public class Product implements Serializable{ private String ID; //产品ID private String name; //产品名称 private String categories; //产品属性 private double price; //产品价格 public Product(String ID,String name,String categories, double price){ this .setID(ID); this .setName(name); this .setCategories(categories); this .setPrice(price); } public String getID() { return ID; } public void setID(String iD) { 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( double price) { this .price = price; } public String toString(){ return "产品ID:" + this .getID()+ "/n" + "产品名称:" + this .getName()+ "/n" + "产品属性:" + this .getCategories()+ "/n" + "产品价格:" + this .getPrice(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | import java.io.BufferedOutputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.OutputStreamWriter; public class ProductTest { public static void main(String[] args) { // TODO Auto-generated method stub Product product1= new Product( "123" , "iphone" , "telephone" , 4888.0 ); Product product2= new Product( "234" , "ipad" , "computer" , 5088.0 ); Product product3= new Product( "345" , "macbook" , "computer" , 10688.0 ); Product product4= new Product( "256" , "iwatch" , "watch" , 4799.0 ); System.out.println( "apple系列产品信息:" ); try { FileOutputStream fos= new FileOutputStream( "product.txt" ); ObjectOutputStream oos= new ObjectOutputStream(fos); oos.writeObject(product1); oos.writeObject(product2); oos.writeObject(product3); oos.writeObject(product4); oos.flush(); FileInputStream fis= new FileInputStream( "product.txt" ); ObjectInputStream ois= new ObjectInputStream(fis); System.out.println((Product)ois.readObject()); fos.close(); oos.close(); fis.close(); ois.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e){ e.printStackTrace(); } } } |
3
收起
正在回答 回答被采纳积分+1
1回答
1. Java 零基础入门
- 参与学习 人
- 提交作业 3802 份
- 解答问题 11489 个
本阶段带你迈入Java世界,学习Java必备基础知识,基础语法、面向对象思想以及常用工具类的使用。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧