java - 多次打印到文件缓冲阅读器java
问题描述
我正在开发一个电影院座位应用程序,但我想在选择时将座位号更新为 xx,然后将其打印到文件中,但每次运行它时,它都会将一个全新的数组打印到文件中,而不是更新。
public void display() throws Exception, FileNotFoundException, IOException, ClassNotFoundException {
String grid [][]= {
{"00","01","02","03","04","05","06","07","08","09"},
{"10","11","12","13","14","15","16","17","18","19"},
{"20","21","22","23","24","25","26","27","28","29"}
};
// ObjectInputStream in = new ObjectInputStream(new FileInputStream("grid.txt"));
//grid[] array = (grid[]) in.readObject();
//in.close();
int row1 = 0;
int col1 = 0;
int seatnum;
//int colnum;
// grid = new int[cinema.rowno()][cinema.colno()];
Cinema.getcinema();
Cinema.getcineplex();
Scanner sc1 = new Scanner(new BufferedReader(new FileReader(".\\src\\lido1.txt")));
while(sc1.hasNextLine()) {
for (int i=0; i<grid.length; i++) {
String[] line = sc1.nextLine().trim().split(" ");
for (int j=0; j<line.length; j++) {
grid[i][j] = line[j];
}
}
}
System.out.println(Arrays.deepToString(grid));
System.out.print(" you have choosen " +Cinema.getcineplex() );
System.out.println();
System.out.print(" HAll " + Cinema.getcinema());
System.out.println();
System.out.printf("=========================================");
System.out.println();
for (row1 = 0; row1 < 3 ; row1++) {
for (col1 = 0; col1 < 10; col1++) {
System.out.printf( "["+grid[row1][col1]+"]");
}
System.out.println();
}
Scanner sc = new Scanner(System.in);
System.out.println("Enter Seat number: ");
seatnum = sc.nextInt();
System.out.print(seatnum);
for (row1 = 0; row1 < 3 ; row1++) {
for (col1 = 0; col1 < 10; col1++) {
//if ( Integer.toString(seatnum) == grid[row1][col1] ) {
// String seatnums = Integer.toString(seatnum);
String str1 = grid[row1][col1];
String str2 = Integer.toString(seatnum);
if (str1.equals(str2)) {
grid[row1][col1] = "xx";
System.out.printf("you have choosen " + str2 );
PrintWriter writer = null;
try {
writer = new PrintWriter(".\\src\\lido1.txt");
for (int i = 0; i<grid.length; i++){
for(int j = 0; j<grid[i].length; j++) {
writer.print(grid[i][j] + ",");
}
writer.println();
}
}
catch (FileNotFoundException e) {
System.out.println("Error: " + e.getMessage());
}
finally {
try{
if (writer!=null) writer.close();
} catch (Exception e) {
System.out.println("Could not close writer");
}
}
}
}
}
System.out.println();
for (row1 = 0; row1 < 3 ; row1++) {
for (col1 = 0; col1 < 10; col1++) {
System.out.printf( "["+grid[row1][col1]+"]");
}
System.out.println();
}
}
解决方案
推荐阅读
- google-api-js-client - 什么是 GAPI 的授权类型(Google API)
- ios - iOS 15.0 中的 UIActivityViewController 问题,选择共享文本选项后导航栏出现问题
- php - 如何打印 PDO CRUD 语句和 bindParams 的值 -PHP PDO
- c# - Unity 中的自定义 OAuth 链接到 Parse Server
- python - 分段颜色图的自定义颜色图边界
- mongodb - mongodb 根据数组 1 中的索引从数组 2 中获取值
- python - Python 请求在我的笔记本电脑上运行缓慢,但在我的 macbook 上运行很快
- vba - 如何检查 2 个表 (MSAccess) 之间的相同值
- r - R Rcpp 测试在某些平台上挂在 CRAN 上
- r - 如何使用 mapvalues 函数来更改特定列内的值?(使用 R)