import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.Serializable;class Customer implements Serializable
{
    String custName;
    String custPass;
}public class FileTest implements ActionListener
{
    JFrame frame;
    JPanel pane;
    JLabel labelName;
    JLabel labelPass;
    JTextField textName;
    JPasswordField textPass;
    JButton buttonWrite;
    JButton buttonRead;
    ObjectOutputStream output;

    public FileTest()
    {
frame = new JFrame();
frame.setTitle("File Test");
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

pane = new JPanel();
labelName = new JLabel("Name");
labelPass = new JLabel("Password");
textName = new JTextField(15);
textPass = new JPasswordField(15);
buttonWrite = new JButton("Write");
buttonRead = new JButton("Read");

pane.add(labelName);
pane.add(textName);
pane.add(labelPass);
pane.add(textPass);
pane.add(buttonWrite);
buttonWrite.addActionListener(this);
pane.add(buttonRead);
buttonRead.addActionListener(this);

frame.setContentPane(pane);
frame.setVisible(true);
         } public void actionPerformed(ActionEvent evt)
{
    Object obj = evt.getSource();
    if (obj == buttonWrite)
    {
String custName = textName.getText();
String custPass = new String(textPass.getPassword());
try
             {
         Customer cust = new Customer(custName, custPass);
    File file = new File("Customer.txt");
    FileOutputStream out = new FileOutputStream(file, true);
                      ObjectOutputStream object = new ObjectoutputStream(out);
    object.writeObject(cust);
    out.close();
    object.flush();
    object.close();
    }
    catch (Exception e)
    {
System.out.println("Write Exception");
    }
}

else if (obj == buttonRead)
{
    try
    {
    Customer cust;
    FileInputStream file = new FileInputStream("Customer.txt");
    ObjectInputStream input = new ObjectInputStream(file);
    while((cust = (Customer)input.readObject()) != null)
    {
cust = (Customer)input.readObject();
System.out.println("Name :" + cust.custName);
System.out.println("Password :" + cust.custPass);
    }
    file.close();             
                      input.close();                       
                      }
    catch (Exception e)
    {
System.out.println("Reader Exception");
    }
}
}

    public static void main(String[] args)
    {
new FileTest();
    }
}
代码在这里
Exception没有写好不是偷懒是想让代码简洁
哪位大哥帮我看看
实现循环读????

解决方案 »

  1.   

    1、
     class Customer implements Serializable{
    Customer(String str1,String str2){
    this.custName=str1;
    this.custPass=str2;
    }
        String custName;
        String custPass;
     }
    2、ObjectOutputStream(OutputStream out)
      

  2.   

    其实我有个偷懒的办法,你的类是不是只有name和password,那么使用properties文件读取,采用userName=password格式,哈哈,简单,只是不能扩充,呵呵
      

  3.   

    不是这样的
    网上不是说在ObjectOutputStream被new
    写入的对象就会加入一个文件头
    第二次这样就会又写入一个文件头
    当ObjectInputStream读了第一个文件头,读出对象
    再遇到文件头就会报异常
      

  4.   

    我想知道怎么跳过这个
    实现循环读
    我做了一下改动就是启动ObjectOutputStream后不在循环里面加入Close();
    而是在程序结束的时候调用
    这个可以存进很多个对象
    也能读很多
    但是有一个问题
    我现在在做一个项目
    要是下次服务器重启的话那么在后面追加又出现问题
      

  5.   

    和系统没有关系
    我试了一个偷懒的办法就是在new ObjectOutputStream后写入所有的文件
    然后Close()
    写入所有的文件以后一共只new 了一次和使用一个Close()
    所以用ObjectInputStream来读只有一个文件头
    下次读进Vector后删除文件再创建一个新的文件写入Vector继续追加就可以了
    在1.4.1里面有一个方法就是改写写文件头的方法要是写了文件头就用reset()追加
    但是我们学校的电脑垃圾
    jdk还是1.3.7的我用这个方法会出现Exception或者没有这个函数
    到时候会扣分
    希望大哥们能告诉我一些更好的方法
      

  6.   

    用一个Vector存入文件再读出来
    载入时对文件进行删除
    再新建文件存入新的Vector
      

  7.   

    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 Main { /**
     * @param args
     * @throws IOException 
     * @throws ClassNotFoundException 
     */
    public static void main(String[] args) throws IOException, ClassNotFoundException { write("c:\\a.dat");
    read("c:\\a.dat");
    } public static void read(String filename) throws IOException, ClassNotFoundException{

    File f=new  File(filename);
    FileInputStream fin=new FileInputStream(f);
    ObjectInputStream in=new ObjectInputStream(fin);
    Object o;
    boolean next=true;
    while(next){
    try{
    Student s=(Student)in.readObject();
    System.out.println(s.getName()+"   "+s.getScore()+"    "+s.getId());
    }catch(Exception e){
    next=false;
    }
    }
    System.out.println("read  end!");
    in.close();
    }
    public static void write(String filename) throws IOException{

    File f=new  File(filename);
    FileOutputStream fout=new FileOutputStream(f);
    ObjectOutputStream out=new ObjectOutputStream(fout);
    Student s1=new Student("leiqian",100,22);
    Student s2=new Student("leijia", 98, 21);
    Student s3=new Student("x_man", 65, 1);
    out.writeObject(s1);
    out.writeObject(s2);
    out.writeObject(s3);
    out.close();
    System.out.println("write  end!");
    }

    }