1

这是我的文件处理类......我在任何其他类中都没有任何错误......程序也覆盖了文件中的数据......它没有附加

import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;
import java.io.EOFException;
import java.lang.ClassNotFoundException;
import java.util.NoSuchElementException;

public class FileHandling {

    FileHandling() {
    }

    public void addCustomer(Customer customerObject, String filePath) {

        ObjectOutputStream output;

        try {
            FileOutputStream fileOut = new FileOutputStream(filePath, true);
            output = new ObjectOutputStream(fileOut);
            output.writeObject(customerObject);
            output.close();
        }

        catch (IOException ioexception) {
            System.err.println("Error with file");
        }
    // end addCustomer function
    }

    public void retrieveCustomerData(String filePath) {

        Customer record = new Customer();

        ObjectInputStream input;

        try {
            FileInputStream fileOut = new FileInputStream(filePath);
            input = new ObjectInputStream(fileOut);

        }

        catch (IOException ioexception) {
            System.err.println("Error with file1");
            return;
        }

        try {
            while (true && input != null) {

                record = (Customer) input.readObject();

                System.out.println(record.getFirstName());
                System.out.println(record.getLastName());
                System.out.println(record.getEmailAddress());

            // end while loop   
            }

        // end try block    
        } catch (EOFException endOfFileException) {

        }

        catch (ClassNotFoundException ioexception) {
            System.err.println("Class not Found");
        }

        catch (IOException ioexception) {
            System.err.println("Error with file2");
            System.err.println(ioexception.getMessage());
        }

        try {
            if (input != null)
                input.close();
        }

        catch (IOException ioException) {
            System.err.println("Error closing file");
        }
    // end retrieveCustomerData function
    }

// end class fileHandling
}
4

0 回答 0