【问题标题】:FileInputStream & FileOutputStream. Cannot get the information inside the file文件输入流和文件输出流。无法获取文件内的信息
【发布时间】:2014-02-18 11:23:16
【问题描述】:
  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新手。非常感谢。如果您要求检查,我可以将我的完整代码发送给您。谢谢。

【问题讨论】:

  • 定义“无法工作”和“无法读取里面的文件”。

标签: java java-io


【解决方案1】:

这是一个应该可以工作的例子:

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(...))”。通常不需要有两个不同的变量,而是一个指向外部流的变量。当您关闭外部流时,它会关闭链中的所有流。
    • 一般来说,您的代码看起来是正确的。只需确保为输入和输出文件指定相同的路径,并确保在读取后检查 ArrayList 的正确实例 :)

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-16
    • 2021-11-19
    • 2019-04-10
    • 1970-01-01
    • 2012-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多