我遇到了一个问题。我编写了以下程序来对文本文件中的整数字符串进行排序,并在输出文件中输出整数的排序列表。输入文件可以包含多行整数,我想一次对它们进行排序,然后在输出文件中打印出来。
问题是程序从输入文本中读取所有行,并在输出文件中打印出连续的已排序数字行。如何在输出文件中使程序排序并逐行打印排序的列表?
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();
}
答案 0 :(得分:1)
您缺少println
。片段:
for (int i = 0; i <= intarray.length - 1; i++) {
outFile.print(intarray[i] + " ");
}
outFile.println(); // this is missing
}
outFile.close();