我遇到了一个问题。我编写了以下程序来对文本文件中的整数字符串进行排序,并在输出文件中输出排序后的整数列表。输入文件可以包含多行整数,我想一次将它们排序一行并将其打印到我的输出文件中。问题是程序从输入文本中读取所有行并在输出文件中打印出连续的排序数字行。如何在输出文件中逐行进行程序排序和打印排序列表?import java.util.*;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.io.PrintWriter;import java.util.Scanner;public class MergeSort { public static void main(String[] args) throws FileNotFoundException, IOException { // declare variable int data[]; int start; int end; // define the file path that we want to read String input_filename = "C:\\Users\\Documents\\NetBeansProjects\\MergeSort\\input.txt"; String output_file = "C:\\Users\\Documents\\NetBeansProjects\\MergeSort\\output.txt"; File file = new File(input_filename); File outfile = new File(output_file); PrintWriter outFile = new PrintWriter(outfile); Scanner fileScanner = new Scanner(new FileReader(file)); while(fileScanner.hasNextLine()){ ArrayList<Integer> list = new ArrayList<Integer>(); Scanner scanner_second = new Scanner(fileScanner.nextLine()); while (scanner_second.hasNextInt()) { list.add(scanner_second.nextInt()); } int[] intarray = list.stream().mapToInt(Integer::intValue).toArray(); non_recursive_mergeSort(intarray, 0, intarray.length - 1); for (int i = 0; i <= intarray.length - 1; i++) { outFile.print(intarray[i] + " "); } } outFile.close(); }
1 回答
墨色风雨
TA贡献1853条经验 获得超6个赞
你失踪了println。片段:
for (int i = 0; i <= intarray.length - 1; i++) {
outFile.print(intarray[i] + " ");
}
outFile.println(); // this is missing
}
outFile.close();
添加回答
举报
0/150
提交
取消