从文件导入整数,将它们相乘,然后导出到新文件

Import integers from a file, multiply them, then export to a new file

我正在做编程原理 1 的家庭作业,我正在努力弄清楚它。

作业状态:

Write a program to create a new file (e.g. named scaled.txt) if it does not exist (If the file exists, terminate your program without doing anything.). Multiply all the numbers from an existing file with integer numbers (e.g. original.txt) by 10 and save all the new numbers in the new file (e.g. scaled.txt).

例如,如果现有文件 (original.txt) 是:

26
12
4
89
54
65
12
65
74
3

那么新文件(scaled.txt)应该是:

260
120
40
890
540
650
120
650
740
30

这是我到目前为止写的:

        //Bronson Lane 11/28/15
        //This program reads a file, multiplies the data in the file by           10, then exports that new data
        //to a new file
        package hw12;
        import java.io.File;
        import java.io.PrintWriter;
        import java.util.Scanner;
        public class HW12Part2 {
           public static void main(String[] args) throws Exception{
              File file1 = new   File("/Users/bronsonlane/Documents/workspace/hw12/original.rtf");
              if (!file1.exists()) {
                 System.out.println("File does not exist");
                 System.exit(0);
              }
              int newNum = 0;
              Scanner input = new Scanner(file1);
              while (input.hasNextInt()) {
                 int num = input.nextInt();
                 newNum = num * 10;
              }

              PrintWriter output;
              File file2 = new File("/Users/bronsonlane/Documents/workspace/hw12/scaled.rtf");
              if (file2.exists()) {
                 System.exit(0);
              }
              output = new PrintWriter(file2);
              while (input.hasNextInt()) {
                    int num = input.nextInt();
                    newNum = num * 10;
                    output.println(newNum);
              }
              output.close();
        }
    }

我现在的问题是控制台正在输出:

File does not exist

不管文件在那里。

我认为这是将其打印到新文件的最佳方式,但显然不是,因为它根本不起作用。

我知道我缺少一些完成程序的关键要素,但我就是想不出来。

*编辑 1:更新代码和新问题 *编辑 2:呸!我愚蠢地放错了文件路径。一切正常,程序按预期运行。

您在 while 块中声明了 newNum,即,您将 int newNum... 放在 while 块的大括号内。这意味着该变量仅在该块内有意义。在该块外声明变量,因此它具有整个方法的范围,例如 int newNum = 0; 然后在 while 块中使用它(不带 int),然后它稍后可用。

我注意到,在查看它时,您也只有一个写入新文件的语句。也许你只是走到这一步还没有写...

您可以在阅读中打开文件,在阅读文件第y行的同时,您可以直接将更新的行输出到新文件中。请参阅下面的代码示例,该示例打开一个包含整数的文件并更新整数,然后将它们添加到另一个文件中:

public static void main(String[] args) throws FileNotFoundException {
        //open file containing integers
        File file = new File("C:\test_java\readnumbers.txt"); 

        //instruct Scanner to read from file source
        Scanner scanner = new Scanner(file); 

        //create file object for output
        File outFile = new File("C:\test_java\incremented_numbers.txt");

        //print writer with source as file 
        PrintWriter writer = new PrintWriter(outFile); 

        //for each line in input file 
        while(scanner.hasNext()) {
            //get the integer in the line 
            int number = scanner.nextInt(); 
            number *= 10; 

            //write the updated integer in the output file
            writer.println(number);
        }

        //close both streams
        scanner.close();
        writer.close();
    }

对您的答案进行以下更改,

PrintWriter output;
File file2 = new File("scores.txt");
if (file2.exists()) {
                System.exit(0);
}
output = new PrintWriter(file2);
 while (input.hasNextLine()) {
                int num = input.nextInt();
                int newNum = num * 10;
               output.print(newNum);
            }