【问题标题】:Merging two text files and sorting it to a third file合并两个文本文件并将其排序到第三个文件
【发布时间】:2016-10-13 10:40:19
【问题描述】:

我正在尝试制作一个程序,它将两个不同的文本文件合并到按升序排序的第三个文件中。这是这两个文件中的内容

文件 1:12 23 34 45 56 67 69 123 133

文件 2:4 5 10 20 35 44 100 130 150 160 180

这是我目前的代码:

    try 
    {

        FileReader file1=new FileReader("D://School//text1.txt");   
        Scanner scan = new Scanner(new File("D://School//text1.txt"));
        ArrayList<Integer> values = new ArrayList<Integer>(); 
        Collections.sort(values);      //sorting the values 
        while(scan.hasNextInt()) values.add(scan.nextInt());

        FileReader file2=new FileReader("D://School//text2.txt");
        scan = new Scanner(new File("D://School//text2.txt")); 
        values = new ArrayList<>();
        Collections.sort(values);       //sorting the values. 
        while(scan.hasNextInt()) values.add(scan.nextInt());

        BufferedReader br1 = new BufferedReader (file1);
        BufferedReader br2 = new BufferedReader(file2);

        String temp1 = "";
        String temp2 = "";

        while(br1.readLine() !=null)
        {
            temp1=br1.readLine()+temp1;
        }
        while(br2.readLine()!=null)
        {
            temp2=br2.readLine()+temp2;
        }
        String temp = temp1 + temp2; 



        FileWriter fw=new FileWriter("D://School//text3.txt"); 
        char buffer[]=new char[temp.length()];
        temp.getChars(0,temp.length(),buffer,0);                        
        fw.write(buffer);
        file1.close();
        file2.close();
        fw.close();
    } 

    catch (IOException e) 
    {
        e.printStackTrace();
    }

到目前为止,该程序将能够在第三个文件中读取和写入正确的内容,但是,它没有按升序对其进行排序。

这是程序将打印到第三个文件的内容

12 23 34 45 56 67 69 123 133 4 5 10 20 35 44 100 130 150 160 180

我希望它像这样将其排序到第三个文件中

4 5 10 12 20 23 34 35 44 45 56 67 69 100 123 130 133 150 160 180

提前致谢:)

【问题讨论】:

  • 它不会打印到第三个文件,你说它会打印到它。我执行你的代码,第三个文件包含nullnull。或者当我将所有数字换行时,它将包含null123674523null16013044205
  • 这让我想到一个问题:这些数字是全部在一行上,还是每个都在不同的行上?
  • 将两个数据编号放在文本文件的第二行,它应该正确打印到第三行。此外,它打印到第三个文件中的数字都将在一行上。

标签: java arrays sorting merge


【解决方案1】:

我建议你使用 TreeSet,我在你的代码中发现了一些问题。

TreeSet<Integer> treeSet = new TreeSet<Integer>();
while(scan.hasNextInt()) treeSet.add(scan.nextInt());
scan = new Scanner(new File("D://School//text2.txt")); 
//Do not new Collection!!!!!
while(scan.hasNextInt()) treeSet.add(scan.nextInt());

你可以为每个集合

for (Integer i : treeSet) {
        System.out.println(i);
}

如果你有 jdk8,你可以试试这个。

    ArrayList<Integer> ints = new ArrayList<>();
    Files.readAllLines(Paths.get("D://School//text1.txt"), StandardCharsets.UTF_8).forEach(x -> Arrays.asList(x.split(" ")).stream().forEach(y -> ints.add(Integer.parseInt(y))));
    Files.readAllLines(Paths.get("D://School//text2.txt"), StandardCharsets.UTF_8).forEach(x -> Arrays.asList(x.split(" ")).stream().forEach(y -> ints.add(Integer.parseInt(y))));
    Collections.sort(ints);
    StringBuilder sb = new StringBuilder();
    ints.stream().map(x -> x + " ").forEach(sb::append);
    Files.write(Paths.get("D://School//text3.txt"),sb.toString().getBytes(),StandardOpenOption.WRITE,StandardOpenOption.CREATE);

【讨论】:

  • 注意:如果文本文件中有重复项,那么使用 TreeSet 会丢失此信息。
  • 非常好的 lambda 解决方案!不幸的是,当 text1/text2 文件的第一行为空时(如 OP 在对问题的评论中所述),它会引发 NumberFormatException 。但是tbh,我真的想知道那个空行是分配的一部分还是OP的解决方法,以使数据适合错误的代码:P +1 :)
  • 是的,空行是一种解决方法。我还在学习java,谢谢大家的帮助:)
  • Files.readAllLines(Paths.get(""), StandardCharsets.UTF_8).stream().filter(z -> !z.equals("")).forEach(x ->Arrays .asList(x.split(" ")).stream().forEach(y -> ints.add(Integer.parseInt(y))));可以通过过滤器解决
【解决方案2】:

当您调用Collections.sort(values); 时,您正在排序一个空的ArrayList

将所有值添加到ArrayList 后,您需要调用Collections.sort(values);

您可以运行这些示例进行测试:

示例 1

List<Integer> values = new ArrayList<Integer>();

// sort the list first
Collections.sort(values);

values.add(5);
values.add(3);
values.add(8);
values.add(1);

System.out.println(values);

输出:[5、3、8、1]

示例 2

List<Integer> values = new ArrayList<Integer>();

values.add(5);
values.add(3);
values.add(8);
values.add(1);

// sort the list now
Collections.sort(values);

System.out.println(values);

输出:[1、3、5、8]

【讨论】:

  • 光靠这个不能解决问题,这段代码还有更多问题。
  • 我是否可以直接从文本文件中添加值,而不必在源代码中单独添加每个数字?
  • 好吧,您必须像现在使用 scan.hasNextInt() 那样解析它们
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-19
  • 1970-01-01
  • 2018-11-08
  • 1970-01-01
相关资源
最近更新 更多