将排序数据打印到新文件

时间:2017-04-11 20:58:41

标签: java file sorting

我有3个带数据的文件(升序,降序,随机),我将文件中的数据传递给排序算法。我需要一个输出文件来显示算法实际上对它们进行了排序。我尝试过使用PrintWriterBufferedReader,但我无法弄清楚如何将已排序的数字打印到新文件中。谢谢!

PrintWriter pw = new PrintWriter(userFile);
   Scanner ascendingScanner = new Scanner(new File("AscendingData.txt"));
    while (ascendingScanner.hasNextInt()) {
        ascending[i++] = ascendingScanner.nextInt();
    }
    pw.print("Ascending");
    // QuickSort timing for ascending
    start = System.currentTimeMillis();
    qs.sort(ascending);
    stop = System.currentTimeMillis();
    sortTime = stop - start;
    pw.print("\t" + sortTime);

1 个答案:

答案 0 :(得分:0)

你几乎要在里面写点什么。但是你必须打电话给pw.close(),否则文件中就不会写任何内容。

public static void main(String[] args) throws FileNotFoundException {
    rintWriter pw = new PrintWriter("test.txt");
    int[] ascending = new int[]{8,2,4,6,7,3};//Just a sample
    pw.print(Arrays.toString(ascending));//will write [8, 2, 4, 6, 7, 3] in the file
    pw.close();//Dont forget this! Otherwise the PrintWriter will not print anything in the file!
}