Java 将排序后的数据打印到新文件
我有3个包含数据的文件(升序、降序、随机),我将数据从文件中传递到排序算法中。我需要一个输出文件来显示算法实际上对它们进行了排序。我曾尝试使用Java 将排序后的数据打印到新文件,java,file,sorting,Java,File,Sorting,我有3个包含数据的文件(升序、降序、随机),我将数据从文件中传递到排序算法中。我需要一个输出文件来显示算法实际上对它们进行了排序。我曾尝试使用PrintWriter和BufferedReader但我不知道如何将排序后的数字打印到新文件中。谢谢 PrintWriter pw = new PrintWriter(userFile); Scanner ascendingScanner = new Scanner(new File("AscendingData.txt")); while
PrintWriter
和BufferedReader
但我不知道如何将排序后的数字打印到新文件中。谢谢
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);
你差点要在里面写点什么。但是您必须调用
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!
}
什么是
qs
?qs
只是在调用快速排序。您不能写入文件还是数字没有排序?我不知道如何写入文件。我需要将排序后的数据写入一个文件,如您正在从文件读取的quickSortAscendingOutput.txt
,好吗。您确定升序[]数组包含正确的数据吗?-->如果“是”,您可以使用大量示例逐行编写其他文件: