File I/O: Reading from one file and writing to another (Java)

前端 未结 5 1198
闹比i
闹比i 2021-01-05 02:36

I\'m currently working on a lab in my cpe class and we have to create a simple program that scans in strings from a .txt file and prints them to a different .txt file. So fa

5条回答
  •  感动是毒
    2021-01-05 03:06

    May be you are just forget the flush()

           try {
                File input = new File("input");
                File output = new File("output");
                Scanner sc = new Scanner(input);
                PrintWriter printer = new PrintWriter(output);
                while (sc.hasNextLine()) {
                    String s = sc.nextLine();
                    printer.write(s);
                }
                **printer.flush();**
            }
            catch (FileNotFoundException e) {
                System.err.println("File not found. Please scan in new file.");
            }
    

提交回复
热议问题