如何将对象写入/替换到文本文件-Java

如何将对象写入/替换到文本文件-Java,java,arrays,object,filereader,Java,Arrays,Object,Filereader,如何替换文本文件中的对象并将其保存到文本文件 首先,我要添加单独的房间 for (int i = 0; i < lines.size() - 1; i++){ String[] words = lines.get(i).split(" "); var room = new Room(); room.roomNum = Integer.parseInt(words[0]); room.roomType = (words[1]);

如何替换文本文件中的对象并将其保存到文本文件

首先,我要添加单独的房间

for (int i = 0; i < lines.size() - 1; i++){

    String[] words = lines.get(i).split(" ");
    var room = new Room();
    room.roomNum = Integer.parseInt(words[0]);
    room.roomType = (words[1]);
    room.roomPrice = Double.parseDouble(words[2]);
    room.hasBalcony = Boolean.parseBoolean(words[3]);
    room.hasLounge = Boolean.parseBoolean(words[4]);
    room.eMail = (words[5]);
    rooms.add(room);                   
}
for(int i=0;i
然后我在文本文件中搜索特定的房间,并将selectedRoom.eMail替换为ReserveMail

System.out.println("\n-- ROOM RESERVATION --");
System.out.println("Please enter the room number you wish to reserve"); 

Room selectedRoom = null;
var searchRoomNum = input.nextInt();

for(int i = 0; i < rooms.size(); i++){                 
    if(rooms.get(i).roomNum == searchRoomNum){                    
        selectedRoom = rooms.get(i);     
    }
}

if(selectedRoom.eMail.contentEquals("free")) {  
    System.out.println("Please Enter your email");
        var reserveEmail = input.next();
            selectedRoom.eMail = reserveEmail;
            System.out.println("Thanks, this room has been reserved");
            return;
}
System.out.println(“\n——房间预订——”;
System.out.println(“请输入您希望预订的房间号”);
房间selectedRoom=null;
var searchRoomNum=input.nextInt();
对于(inti=0;i
我需要知道如何将此更改保存到它来自的文本文件中,而不覆盖整个文件


根据文件的大小,只需将其加载到内存中,解析到房间对象中,然后覆盖更改,可能会更容易

如果我的代码有错误,我很抱歉,但我在手机上。它应该给你一个通用的算法让它运行起来

导入扫描仪

import java.util.Scanner;
一般更新过程

// Load to memory
Scanner scn = new Scanner(new File(“C:\Users\me\Desktop\test.txt”));

// Create new list of rooms
List<Room> roomList = new ArrayList<Room>();

// Begin parsing
while (scn.HasNextLine()) {
    String currentLine = scn.nextLine();
    String[] roomAttrs = currentLine.split(“ ”);
    Room currentRoom = new Room();
    currentRoom.roomNum = Integer.parseInt(roomAttrs[0]);
    currentRoom.roomType = roomAttrs[1];
    currentRoom.roomPrice = Double.parseDouble(roomAttrs[2]);
    currentRoom.hasBalcony = Boolean.parseBoolean(roomAttrs[3]);
    currentRoom.hasLounge = Boolean.parseBoolean(roomAttrs[4]);
    currentRoom.eMail = roomAttrs[5];

    // make your changes to the room here

    roomList.add(currentRoom);
}

// reuse code for writing to text here, either delete file or clear it
//加载到内存
Scanner scn=new Scanner(新文件(“C:\Users\me\Desktop\test.txt”);
//创建新的房间列表
List roomList=新建ArrayList();
//开始解析
while(scn.HasNextLine()){
字符串currentLine=scn.nextLine();
字符串[]roomAttrs=currentLine.split(“”);
房间当前房间=新房间();
currentRoom.roomNum=Integer.parseInt(roomAttrs[0]);
currentRoom.roomType=roomAttrs[1];
currentRoom.roomPrice=Double.parseDouble(roomAttrs[2]);
currentRoom.hasBaround=Boolean.parseBoolean(roomAttrs[3]);
currentRoom.hasLounge=Boolean.parseBoolean(roomAttrs[4]);
currentRoom.eMail=roomAttrs[5];
//在这里对房间进行更改
房间列表。添加(当前房间);
}
//在此处重复代码以写入文本,可以删除文件,也可以清除文件