【问题标题】:Write into another file after end of file is in buffer文件末尾在缓冲区后写入另一个文件
【发布时间】:2018-12-29 01:53:22
【问题描述】:

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

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 字符后没有接受任何输入。

【问题讨论】:

    标签: java file eof


    【解决方案1】:

    一旦stdin 关闭(通过从键盘输入^D),它将不再产生任何输入。所以,addRecordToMainFile 方法中的input.hasNext() 将一直运行到stdin 用完为止。当addRecordToTransactionFile 运行时,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)

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

    但是在流关闭后你不能从stdin读取,所以寻找^D/等待input.hasNext返回false是行不通的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-26
      • 1970-01-01
      • 1970-01-01
      • 2023-03-29
      • 1970-01-01
      相关资源
      最近更新 更多