-2

RandomAccessFile を使用してファイル内のデータを変更するためのこのコードがあります。
しかし、それは機能しません。

      Scanner sc = new Scanner(System.in);
   System.out.println("enter search id");
    String search = sc.next().toLowerCase();

    try {
    file = new RandomAccessFile(new File("HallFile.txt"), "rw");
    long FileSize = file.length();
    file.seek(0);
    long numberOfRecords = FileSize / RECORD;
    for (int j = 0; j < numberOfRecords; j++) {


            hall_id= file.readInt();
            floorNo= file.readInt();
            area= file.readInt();
            capacity= file.readInt();
            rent= file.readInt();
            dimonssion= file.readInt();

            char temp = '\0';

            for(int i=0; i< 20; i++){
                temp = file.readChar();
                if(temp == '\0') continue;
                hall_type += temp;
            }
            for(int i=0; i< 20; i++){
                temp = file.readChar();
                if(temp == '\0') continue;
                seat_style += temp;
            }
            for(int i=0; i< 20; i++){
                temp = file.readChar();
                if(temp == '\0') continue;
                facilities += temp;

           Integer m = hall_id;
         if(search.equals(m.toString()))
         {

       file.seek(RECORD*j+144);

       System.out.println("New Floor NO: ");
       floorNo= sc.nextInt();
       file.write(floorNo);
       System.out.println("Hall area: ");
       area= sc.nextInt();
       file.write(area); 
       System.out.println("Hall capacity: ");
       capacity= sc.nextInt();
       file.write(capacity); 
       System.out.println("Hall rent per day: ");
       rent= sc.nextInt();
       file.write(rent);
       System.out.println("Hall Type: ");
       hall_type= sc.next().toLowerCase();
       file.writeUTF(hall_type); 
       System.out.println("Hall Style: ");
       seat_style= sc.next().toLowerCase();
       file.writeUTF(seat_style); 
       System.out.println("Hall Facilities: ");
       facilities= sc.next().toLowerCase();
       file.writeUTF(facilities); 
       System.out.println("Hall Dimonssion: ");
       dimonssion= sc.nextInt();
       file.write(dimonssion); 
       System.out.println("Saved successfully.");

                }
            }
            file.close();

        }} catch (IOException e) {
            e.getStackTrace();
                        System.out.println("error");
 }
4

1 に答える 1

0

一連の変数にデータを読み込んで、必要に応じて更新してみてください (それぞれの現在の値をユーザーに促し、Enter を使用して新しい値を要求することができます)。その後、更新された値を再度書き込みます。

書き込みたい値とファイル内のレコードインデックスがわかっている場合は、そこを探して直接上書きすることもできます。

しかし、通常、「変更」とは、何を変更しているのかを知ることを意味します。

于 2013-07-26T02:30:03.483 に答える