import java.io.FileOutputStream; 
import java.io.ObjectOutputStream; 
import java.io.Serializable; 
public class test1 
{  private record rd; 
    private  ObjectOutputStream output; 
    
    public test1() 
    {try{output=new ObjectOutputStream(new FileOutputStream("e:\\file.txt")); 
        rd=new record(12,"ert",12f,23f); 
    }catch(Exception e){} 
    } 
    
private class record implements Serializable//记录类,用于写入 
{private int account; 
  private String name; 
  private float price; 
  private float payment;   public record(int account,String name,float price,float payment) 
  {this.account=account; 
  this.name=name; 
  this.payment=payment; 
  this.price=price; 
    
  } 
  
} public static void main(String[] args) 
{  test1 app=new test1(); 
try{ 
    app.output.writeObject(app.rd); 
    app.output.flush(); 
    } 
catch(Exception e){} 
} } 

解决方案 »

  1.   

    package demo.io;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.Serializable;public class ObjectIO { public static void main(String[] args) {
    try {
    //先把对象写入文件
    ObjectOutputStream output = new ObjectOutputStream(
    new FileOutputStream("e:\\file.txt"));
    output.writeObject(new record(12, "ert", 12f, 23f)); output.flush();
    output.close(); //在读进内存中
    ObjectInputStream fin = new ObjectInputStream(new FileInputStream(
    "e:\\file.txt"));
    Object o = fin.readObject();
    if (o instanceof record) {
    System.out.println(o);
    }
    fin.close();
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    } catch (ClassNotFoundException e) {
    e.printStackTrace();
    }
    }
    }class record implements Serializable//记录类,用于写入 
    {
    private static final long serialVersionUID = 1L;
    private int account;
    private String name;
    private float price;
    private float payment; public record(int account, String name, float price, float payment) {
    this.account = account;
    this.name = name;
    this.payment = payment;
    this.price = price; } @Override
    public String toString() {
    return this.account + this.name + this.payment + this.price;
    }
    }
      

  2.   


    package demo.io;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.Serializable;public class ObjectIO { public static void main(String[] args) {
    try {
    //先把对象写入文件
    ObjectOutputStream output = new ObjectOutputStream(
    new FileOutputStream("e:\\file.txt"));
    output.writeObject(new record(12, "ert", 12f, 23f)); output.flush();
    output.close(); //在读进内存中
    ObjectInputStream fin = new ObjectInputStream(new FileInputStream(
    "e:\\file.txt"));
    Object o = fin.readObject();
    if (o instanceof record) {
    System.out.println(o);
    }
    fin.close();
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    } catch (ClassNotFoundException e) {
    e.printStackTrace();
    }
    }
    }class record implements Serializable//记录类,用于写入 
    {
    private static final long serialVersionUID = 1L;
    private int account;
    private String name;
    private float price;
    private float payment; public record(int account, String name, float price, float payment) {
    this.account = account;
    this.name = name;
    this.payment = payment;
    this.price = price; } @Override
    public String toString() {
    return this.account + this.name + this.payment + this.price;
    }
    }
      

  3.   

    对不起,我是想在文件中出现记录内容,并非在屏幕上显示,比如当我打开e:\file.txt文件时,发现里面有我刚才程序中的记录
      

  4.   

    java的对象输入\输出流写的是二进制文件,不是普通的文本。是输入和输出配套使用的。