PrintSteam完整输出仅创建最后一个数据集

问题描述 投票:0回答:1

我的程序会编译并写入一个.txt文件,但是它只会写入最后一个数据集大小1000。我正在尝试将其所有十个大小都写入一个.txt文件。如果我不使用PrintSteam方法,则完整的输出将包括所有十个数据集。我将如何使十个人全部写入.txt文件?下面是主要代码和BenchmarkSorts类中写入文件的部分。

import java.io.IOException;
import java.io.FileNotFoundException;

public class SortMain {

    public static void main(String[] args) throws Exception, IOException, FileNotFoundException {
        int[] sizes = {100, 200, 300, 400, 500, 600, 700, 800, 900, 1000};
        new BenchmarkSorts(sizes);
    }
}
    try {
        // Creating a File object that represents the disk file. 
        PrintStream o = new PrintStream(new File("BenchmarkSort.txt")); 

        // Store current System.out before assigning a new value 
        PrintStream console = System.out; 

        // Assign o to output stream 
        System.setOut(o); 

        // Produces output
        System.out.println("Data Set Size (n): " + arraySize +
        "\n\tIterative Selection Sort Results: \t\t\t\t\tRecursive Selection Sort Results:" +
        "\n\tAverage Critical Operation Count: " + Math.round(iterativeAverageCount) +
        "\t\t\tAverage Critical Operation Count: " + Math.round(recursiveAverageCount) +
        "\n\tStandard Deviation of Count: " + Math.round(iterativeSDCount) +
        "\t\t\t\t\tStandard Deviation of Count: " + Math.round(recursiveSDCount) +
        "\n\tAverage Execution Time: " + Math.round(iterativeAverageTime) +
        "\t\t\t\t\t\tAverage Execution Time: " + Math.round(recursiveAverageTime) +
        "\n\tStandard Deviation of Time: " + Math.round(iterativeSDTime) +
        "\t\t\t\t\t\tStandard Deviation of Time: " + Math.round(recursiveSDTime));

         // Use stored value for output stream 
        System.setOut(console); 
        System.out.println("Output File is now BenchmarkSort.txt");
    } 
    catch(FileNotFoundException ex) {

    }
java file-writing
1个回答
0
投票

实际上,这已覆盖每个文件。尝试按日期指定唯一的文件名,然后检查newDate()。getTime();

© www.soinside.com 2019 - 2024. All rights reserved.