首页 > 解决方案 > 多次打印到文件缓冲阅读器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();
    }
}

标签: java

解决方案


推荐阅读