如何将文本文件读入waitingRoom数组?我还需要能够使用此文件来添加和删除乘客吗?谁能帮忙x
package trainstation;
import java.util.Scanner;
public class TrainStation
{
static int WAITING_ROOM_CAPACITY = 30;
private static Passenger[] waitingRoom = new Passenger[WAITING_ROOM_CAPACITY];
private static PassengerQueue trainQueue = new PassengerQueue();
public static void main(String[] args)
{
File myObj = new File("filename.txt");
Scanner myReader = new Scanner(myObj);
while (myReader.hasNextLine()) {
String data = myReader.nextLine(); // The data of the txt file will depends on the format you wanted.
System.out.println(data); // Or you can append data to an array ...
}
或者如果您的txt文件数据格式是这样的话
名称|姓氏| 1个名称1 |姓1 | 2
然后,您必须用分隔符将txt文件中的每一行数据分开。
File myObj = new File("filename.txt");
Scanner myReader = new Scanner(myObj);
while (myReader.hasNextLine()) {
String[] data = (myReader.nextLine()).split(" | "); // It is already an array
System.out.println(data); // Or you can append data to an array ...
}