FileInputStream& FileOutputStream中。无法获取文件内的信息

时间:2014-02-18 11:23:16

标签: java java-io

  import java.io.FileInputStream;
  import java.io.FileOutputStream;

    try {
        FileInputStream inFile_Customer = new FileInputStream("database\\Customers.bin");
        ObjectInputStream ois_Customer = new ObjectInputStream(inFile_Customer);
        customerList = (ArrayList<Customer>)ois_Customer.readObject();

        FileInputStream inFile_Package = new FileInputStream("database\\Packages.bin");
        ObjectInputStream ois_Package = new ObjectInputStream(inFile_Package);
        packageList = (ArrayList<Packages>)ois_Package.readObject();

        FileInputStream inFile_Order = new FileInputStream("database\\Orders.bin");
        ObjectInputStream ois_Order = new ObjectInputStream(inFile_Order);
        orderList = (ArrayList<Order>)ois_Order.readObject();

        FileInputStream inFile_Invoice = new FileInputStream("database\\Invoices.bin");
        ObjectInputStream ois_Invoice = new ObjectInputStream(inFile_Invoice);
        invoiceList = (ArrayList<Invoice>)ois_Invoice.readObject();

    } catch (ClassNotFoundException |IOException | ClassCastException e) {
        e.printStackTrace();
    }


    public void WriteCustomer(){
     try {
        FileOutputStream fos = new FileOutputStream("database\\Customers.bin");
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(customerList);
        oos.close();
     } catch(Exception ex) {
        ex.printStackTrace();
     }
    }

    public void WritePackage(){
     try {
         FileOutputStream fos = new FileOutputStream("database\\Packages.bin");
         ObjectOutputStream oos = new ObjectOutputStream(fos);
         oos.writeObject(packageList);
         oos.close();
     } catch(Exception ex) {
        ex.printStackTrace();
     }
     }

     public void WriteOrder(){
       try {
         FileOutputStream fos = new FileOutputStream("database\\Orders.bin");
         ObjectOutputStream oos = new ObjectOutputStream(fos);
         oos.writeObject(orderList);
         oos.close();
       } catch(Exception ex) {
         ex.printStackTrace();
       }
     }

     public void WriteInvoice(){
       try {
         FileOutputStream fos = new FileOutputStream("database\\Invoices.bin");
         ObjectOutputStream oos = new ObjectOutputStream(fos);
         oos.writeObject(invoiceList);
         oos.close();
       } catch(Exception ex) {
        ex.printStackTrace();
       }
     }

这是我的编码。它没有显示任何错误,但它无法正常工作。信息写在文件中。但它只是无法读取里面的文件。请帮我。我是java的新手。非常感谢。如果您要求检查,我可以将您的完整编码发送给您。感谢。

1 个答案:

答案 0 :(得分:0)

这是一个应该有效的例子:

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;

public class Test {

    public static class Customer implements Serializable {
        private static final long serialVersionUID = 1L;        

        private String name;

        public Customer() {
        }       

        public Customer(String name) {
            this.name = name;
        }       

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        @Override
        public String toString() {
            return "Customer{" + "name=" + name + '}';
        }                
    }

    private ArrayList<Customer> customerList = new ArrayList<>();

    public void writeCustomers(String path) {
        ObjectOutputStream oos_Customer = null;
        try {
            oos_Customer = new ObjectOutputStream(new FileOutputStream(path));
            oos_Customer.writeObject(customerList);
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if (oos_Customer != null) { // old-style safe closing when done
                try {
                    oos_Customer.close();
                } catch (IOException e) {                    
                }
            }
        }
    }

    public void readCustomers(String path) {
        try (ObjectInputStream ois_Customer = new ObjectInputStream(new FileInputStream(path))) { // new-style safe closing when done

            customerList = (ArrayList<Customer>) ois_Customer.readObject();

        } catch (ClassNotFoundException | IOException | ClassCastException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {

        final Test t = new Test();
        t.customerList.add(new Customer("A"));
        t.customerList.add(new Customer("B"));

        System.out.println("Initial customer list: " + t.customerList);

        t.writeCustomers("Customers.bin");

        t.customerList.clear();

        t.readCustomers("Customers.bin");

        System.out.println("Read customer list: " + t.customerList);
    }
}

一些补充说明:

  • 确保您的类实现java.io.Serializable并将serialVersionUID设置为任何常量long值。如果您更改班级,然后阅读旧文件
  • ,这将对您有所帮助
  • 使用包装器模式“new SomeStream(new SomeOtherStream(...))”。通常不需要有2个不同的变量而是指向外部流的变量。关闭外部流时,它会关闭链中的所有流。
    • 通常,您的代码看起来是正确的。只需确保为输入和输出文件指定相同的路径,并确保在读取后检查ArrayList的正确实例:)

如果它仍然不适合您,请发布整个代码。