Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中保存/加载对象数组_Java_Arrays_Object - Fatal编程技术网

在Java中保存/加载对象数组

在Java中保存/加载对象数组,java,arrays,object,Java,Arrays,Object,我正在尝试将对象数组保存/加载到一个普通的.txt文件中。我对序列化知之甚少,但我认为我已经正确地使用它将对象数组写入.txt文件。单独打开时,.txt文件完全不可读(普通英语)。我是否正确地将其写入文件?我该如何把它读入程序 public class HotelObjects { /** * @param args the command line arguments */ public static void main(String[] args) { String comma

我正在尝试将对象数组保存/加载到一个普通的.txt文件中。我对序列化知之甚少,但我认为我已经正确地使用它将对象数组写入.txt文件。单独打开时,.txt文件完全不可读(普通英语)。我是否正确地将其写入文件?我该如何把它读入程序

public class HotelObjects {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    String command;

    Scanner input = new Scanner(System.in);

    Room[] myHotel = new Room[10];
    for (int x = 0; x < myHotel.length; x++) {
    myHotel[x] = new Room();
    }

    String roomName;
    int roomNum = 0;

    while (roomNum < 11) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter command : ");
        command = input.next();
        command = command.toLowerCase();

        if (command.charAt(0) == 'v') {
            viewCustomers(myHotel);
        }

        if (command.charAt(0) == 'a') {
            addCustomers(myHotel);
        }

        if (command.charAt(0) == 'e') {
            emptyRooms(myHotel);
        }

        if (command.charAt(0) == 's') {
            storeData(myHotel);
        }

    }
}

private static void viewCustomers(Room hotelRef []) {
    for (int x = 0; x < 10; x++) {
            System.out.println("room " + x + " occupied by " + hotelRef[x].getName());
        }
}

private static void addCustomers(Room myHotel[]) {
    String roomName;
    int roomNum;
    System.out.println("Enter room number (0-10) or 11 to stop:");
    Scanner input = new Scanner(System.in);
        roomNum = input.nextInt();
        System.out.println("Enter name for room " + roomNum + " :");
        roomName = input.next();
        myHotel[roomNum].setName(roomName);
}

private static void emptyRooms(Room[] myHotel) {
    for (int x = 0; x < 10; x++ )
        if (myHotel[x].getName().equals("e"))System.out.println("room " + x + " is empty");
}

private static void storeData(Room [] myHotel) {
    try{
        FileOutputStream fos= new FileOutputStream("C:\\Users\\Ganz\\Documents\\NetBeansProjects\\HotelObjects\\HotelObject.dat");
        ObjectOutputStream oos= new ObjectOutputStream(fos);
        oos.writeObject(myHotel);
        oos.close();
        fos.close();
    }catch(IOException ioe){
    }
  }
}

使用ObjectInputStream和强制转换从文件加载回数组。您需要在写入流结束时(关闭它之前)刷新流。

写入ObjectOutputStream的结果应该是机器可读的,而不是人类可读的。要将其读回,请使用:

FileInputStream fis = new FileInputStream("C:\\Users\\Ganz\\Documents\\NetBeansProjects\\HotelObjects\\HotelObject.dat");
ObjectInputStream ois = new ObjectInputStream(fis);
Hotel hotel = (Hotel) ois.readObject();

ObjectOutputStream
的输出是机器可读的,因此不一定是人类可读的。当使用可关闭的资源时,您应该在Java 1.7之前的try块中使用它们,并在try的finally块中关闭资源,或者如果使用1.7+try with resources语句,则关闭资源。这是为了确保即使在引发异常的情况下也关闭资源

String path = "C:\\Users\\Ganz\\Documents\\NetBeansProjects\\HotelObjects\\HotelObject.dat";

try (FileInputStream fileInput = new FileInputStream(path); ObjectInputStream inputStream= new ObjectInputStream(fileInput)) {

    // Cast the Object returned from the stream to a Hotel object
    Hotel hotel = (Hotel) inputStream.readObject(); 

} catch (FileNotFoundException ex) {
   // TODO: Exception handling here   
} catch (IOException ex) {
   // TODO: Exception handling here     
}

如果您觉得默认的序列化表单不适合您的需要,那么您可以创建自己的表单。如果是,请查看如何创建自定义序列化表单。

当我使用该表单(在单独的a过程中)时,它可能会在建议代码的最后一行中导致两个错误,即“找不到符号”使用新代码更新问题,并在运行时进行回溯。
String path = "C:\\Users\\Ganz\\Documents\\NetBeansProjects\\HotelObjects\\HotelObject.dat";

try (FileInputStream fileInput = new FileInputStream(path); ObjectInputStream inputStream= new ObjectInputStream(fileInput)) {

    // Cast the Object returned from the stream to a Hotel object
    Hotel hotel = (Hotel) inputStream.readObject(); 

} catch (FileNotFoundException ex) {
   // TODO: Exception handling here   
} catch (IOException ex) {
   // TODO: Exception handling here     
}