文件末尾在缓冲区后写入另一个文件

Write into another file after end of file is in buffer

我正在尝试写入两个 files.I 可以写入一个文件,但是在第一个文件末尾插入文件末尾后,我无法写入其他文件。程序终止而不对下一个文件进行任何输入。 请告诉我如何从缓冲区中删除文件结尾字符。 这样 addRecordToTransactionFile() 中的语句 input.hasNext() 就不会读取 eof 字符 their.

public class TestClass {
    public static BufferedWriter mainFile;
    public static BufferedWriter transactionFile;
    public static Scanner input = new Scanner(System.in);

    public static void openFile() {
        // Code to open file
    }

    public static void addRecordToMainFile() {
        try{
            System.out.println("\t\tEnter data to Master File");
            System.out.printf(" %s %s %s%n? ","Master file account number" , "Name" ,"Balance");
            while(input.hasNext()) {
                try {
                    mainFile.write(String.format("%-10d %-12s %.2f" , input.nextInt() , input.next() ,input.nextDouble()));
                } catch(IllegalStateException e) {
                    System.err.println("Please Enter a valid input.%nTry Again");
                    input.nextLine();
                } catch(NoSuchElementException e) {
                    System.err.println("Please Enter a valid input.%nTry Again");
                    input.nextLine();
                }
                System.out.print("? ");
            }

        }catch (IOException e) {
            System.err.println("File is Closed... Closing Application");
            System.exit(1);
        }
        input.nextLine();
    }


    public static void addRecordToTransactionFile() {
        try {
            System.out.println("\n\t\tEnter data to the Transaction File");
            System.out.printf("%s\t%s%n?" ,"Transaction file account number" ,"Transaction amount" );
            while(input.hasNext()) {
                try{
                    transactionFile.write(String.format("%-10d %.2d" , input.nextInt(),input.nextDouble()));

                }catch(NoSuchElementException e) {
                    System.err.println("Enter a valid input %nPlease try again");
                    input.nextLine();
                }
            }
        }catch (IOException e) {
            System.err.println("File is Closed... Closing Application");
            System.exit(1);
        }
    }

    public static void closeFile() {
        //Code to close file
    }

    public static void main(String[] args) {
        openFile();
        addRecordToMainFile();
        addRecordToTransactionFile();
        closeFile();
    }
}

输出:

        Enter data to Master File
 Master file account number Name Balance
? 100 Alan_Jones 232.32
? ^D

        Enter data to the Transaction File
Transaction file account number Transaction amount
?
Process finished with exit code 0

此程序在插入 eof 字符后不接受任何输入。

一旦 stdin 关闭(通过从键盘输入 ^D),它就不会再产生任何输入。因此,addRecordToMainFile 方法中的 input.hasNext() 将 运行 直到 stdin 耗尽。当 addRecordToTransactionFile 运行s 时,stdin 仍然关闭并且不会产生更多输入 ,因此该方法完全跳过 while 循环.

要将单个输入写入多个输出文件,我推荐以下两个选项之一:

  • 为每条输入记录使用一个标记 at 来指示交易的类型(它应该去哪里)。例如:

M 100 Alan_Jones 232.32 (goes to main file) T 100 Alan_Jones 232.32 (goes to transaction file)

  • 在输入文件的每个 部分 之间使用分隔符。例如:

MAIN 100 Alan_Jones 232.32 100 Alan_Jones 232.32 100 Alan_Jones 232.32 100 Alan_Jones 232.32 (all these go into the main file) TRANSACTION 100 Alan_Jones 232.32 100 Alan_Jones 232.32 100 Alan_Jones 232.32 100 Alan_Jones 232.32 (all these go into the transaction file)

您的输入读取逻辑必须变得更加复杂才能处理输入。请注意,第二个选项与您已经尝试过的方案非常相似,除了 "end of file" 字符 (^D) 已被替换为文件中不是 "special" 的不同标记到 UNIX 流系统:字符串 "TRANSACTION".

但是在流关闭后您无法从 stdin 读取,因此寻找 ^D / 等待 input.hasNext 到 return false 将不工作。