【问题标题】:Outputting number data into a .txt file in Java将数字数据输出到 Java 中的 .txt 文件中
【发布时间】:2015-09-22 17:24:16
【问题描述】:

对于我的编程任务是从文件中读取数据,计算平均值,运行总计,并列出原始数字。我遇到的问题是将数据输出到输出文件中。我不知道如何将控制台上的打印内容打印到输出文件。非常感谢任何帮助!

public static void main(String[] args) throws IOException {

  // Declare variables
    // Define your file names 
    final String INPUT_FILE  = ("Input.txt");
    final String OUTPUT_FILE = ("Output.txt");

  int numberOfNumbers = 0;   // Number of numbers in the input file
  double sum = 0;            // The sum of the numbers
  double average = 0;        // The average of the numbers read
  double oneNumber;          // An individual number read from the file
  double runningTotal = 0;   // the running total sum of the numbers


  // Access the input/output files
    File inputDataFile = new File(INPUT_FILE);
    Scanner inputFile  = new Scanner(inputDataFile);
    FileWriter outputDataFile = new FileWriter(OUTPUT_FILE);
    PrintWriter outputFile = new PrintWriter(outputDataFile);
    System.out.println("Reading  file " + INPUT_FILE + "\r\n" +
                       "Creating file " + OUTPUT_FILE);

    // Read the input file and sum the numbers. 

    while (inputFile.hasNext()) {
            numberOfNumbers++;
            oneNumber = inputFile.nextDouble();
            sum += oneNumber;                 // Calculate total sum 
            runningTotal += oneNumber;        // Calculate running total
            average = sum / numberOfNumbers;  // Calculate the average

        System.out.printf("%-10.2f\t %.2f\n", oneNumber, runningTotal);

            // 2. write the number and running total to the output file

        }  // End while

    // Add code here to:
    // 1. write the number of numbers, the sum and the average
  //    to the output file using DecimalFormat to format the sum and average
    inputFile.close();  // close the input file
    outputFile.close(); // close the output file

    System.out.println("The sum of the " + numberOfNumbers + 
                             " numbers is " + sum + "\n" +
                     " and the average is " + average);

    System.exit(0); 
  } // End main
} // End class

【问题讨论】:

    标签: java input output text-files


    【解决方案1】:

    将此添加到您的代码中:

    PrintWriter writer = new PrintWriter("the-file-name.txt", "UTF-8");
    writer.println("The first line");
    writer.println("The second line");
    writer.close();
    

    【讨论】:

      【解决方案2】:

      您已经创建的 PrintWriter 类就像 System.out 控制台输出一样工作。如果要向文件写入一行,请使用

      outputFile.println("text");
      

      如果您不希望在文本之后将换行符添加到文件中,请使用

      outputFile.print("text");
      

      它甚至还有一个 printf 函数。这是文档enter link description here

      【讨论】:

        【解决方案3】:

        printf之前的while循环中使用

        outputFile.printf("%-10.2f\t %.2f\n", oneNumber, runningTotal);
        

        System.exit(0)之前使用

            outputFile.println("The sum of the " + numberOfNumbers + " numbers is "
                    + sum + "\n" + " and the average is " + average);
            System.out.println("The sum of the " + numberOfNumbers + " numbers is "
                    + sum + "\n" + " and the average is " + average);
        
        
            inputFile.close(); // close the input file
            outputFile.close(); // close the output file
        

        【讨论】:

          【解决方案4】:
          File file = new File("output path");
          FileWriter fw = new FileWriter(file.getAbsoluteFile());
                      BufferedWriter bw = new BufferedWriter(fw);
          
          
                      bw.write(content);//This is the only thing that goes in the while loop!
          
                      bw.close();//do this at the end! of your program. 
          

          附:在你求助论坛之前尝试谷歌搜索这些东西。这样你会学到更多。 :)

          快乐编码

          【讨论】:

          • 是的,当我用谷歌搜索它时,我没有看到任何真正帮助我的东西,这就是我来这里的原因。我尝试了除了 printf() 之外的所有方法。你可能会说,我对编码非常陌生哈哈
          猜你喜欢
          • 2020-01-07
          • 1970-01-01
          • 1970-01-01
          • 2019-03-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-02-19
          • 1970-01-01
          相关资源
          最近更新 更多