我正在尝试将对象数组保存/加载到普通的.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){
}
}
}
这是Room类(如果需要):
public class Room implements Serializable {
private String mainName;
int guestsInRoom;
public Room() {
mainName = "e";
System.out.println("made a room ");
}
public void setName(String aName) {
mainName = aName;
}
public String getName() {
return mainName;
}
}
答案 0 :(得分:0)
使用ObjectInputStream和强制转换从文件加载数组。并且您需要在写入结束时(在关闭之前)刷新流。
答案 1 :(得分:0)
编写ObjectOutputStream的结果应该是机器可读的,而不是人类可读的。要重新阅读,请使用:
FileInputStream fis = new FileInputStream("C:\\Users\\Ganz\\Documents\\NetBeansProjects\\HotelObjects\\HotelObject.dat");
ObjectInputStream ois = new ObjectInputStream(fis);
Hotel hotel = (Hotel) ois.readObject();
答案 2 :(得分:0)
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
}
如果您觉得默认的序列化表单不适合您的需要,那么您可以创建自己的。如果是这样,请查看如何创建自定义序列化表单。