简体   繁体   English

使用方法(Java)按字母顺序将文件内容排序到新文件中

[英]Sort contents of file in alphabetical order into a new file using methods (Java)

So I am trying write some code to find a way to bubble sort an existing text file that I have collected the information in a form of an array and it looks like this:所以我正在尝试编写一些代码来找到一种对现有文本文件进行冒泡排序的方法,我以数组的形式收集了信息,它看起来像这样:

 04/26/16 Sega 3D Classics Collection 07/14/16 Batman: Arkham Underworld 06/24/16 Tokyo Mirage Sessions #FE

Essentially I want them to be in alphabetical order and it should make a brand new file that looks like this:本质上,我希望它们按字母顺序排列,它应该创建一个如下所示的全新文件:

 Batman: Arkham Underworld Sega 3D Classics Collection Tokyo Mirage Sessions #FE

The problem is I do not know how to remove the date of the string that it is originally part of.问题是我不知道如何删除它最初属于的字符串的日期。 I think there should be some kind of function like indexOf() like in javascript.我认为应该有某种 function,比如 javascript 中的 indexOf()。 I would assume that after the extraction of the names.我会假设在提取名称之后。 Then the sorting would be the same as the one I have done for the sorting of dates.然后排序将与我为日期排序所做的排序相同。

This is the code I have done:这是我所做的代码:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class Main{
  public static void main (String[]args) throws IOException{

    File file = new File("C:/Users/cyrus/Downloads/Lab 11/releasedates.txt");

    String []arr = input(file);

    output(file,arr);

    outputSort1(file, arr);
    

  }

  public static String[]input (File file) throws FileNotFoundException{
    String[]arr = new String[3];
    Scanner sc = new Scanner(file);

    for(int i = 0; i < arr.length; i++){
      arr[i] = sc.nextLine();

    }
    return arr;
  }

  public static void output(File file, String[] info) throws IOException{

    FileWriter writer = new FileWriter("C:/Users/cyrus/Downloads/Lab 11/fileName.txt");
    for(String aString:info){
      writer.write(aString);
    }
    writer.close();
  }

  public static void sortByMonth(String[]info){

    String temp;

    for (int j = 0; j < info.length; j++) {
      for (int i = j + 1; i < info.length; i++) {
 
  if (info[i].compareTo(info[j]) < 0) {
    temp = info[j];
    info[j] = info[i];
    info[i] = temp;
        }
     }
    }
  }

  public static void outputSort1(File file,String[] info) throws IOException{
    sortByMonth(info);
    FileWriter writer = new FileWriter("C:/Users/cyrus/Downloads/Lab 11/fileNameSorted1.txt");
    for(String aString:info){
        writer.write(aString);
    }
    writer.close();
}

public static void sortByName(String[]info){
//this isn't really finished, I am unsure on how to extract JUST the name of the games
}
}

There are several ways to sort the lines.有几种方法可以对行进行排序。 suppose you put all the lines in a list and sort that list, before the sort, you also remove the date.假设您将所有行放在一个列表中并对该列表进行排序,在排序之前,您还删除了日期。 Using Java Stream 8使用 Java Stream 8

List<String> lines = Arrays.asList("04/26/16  Sega 3D Classics Collection",
                "07/14/16  Batman: Arkham Underworld",
                "06/24/16  Tokyo Mirage Sessions #FE");
        
        List<String> newLines = lines.parallelStream()
                .map(line-> line.substring(line.indexOf(" ")))
                .sorted((line1, line2)->line1.compareTo(line2))
                .collect(Collectors.toList());
newLines.forEach(System.out::println);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用方法(Java)按字母顺序对文件内容进行排序 - Alphabetically sort contents of file using methods (Java) 按字母顺序对文本文件中的数据进行排序-Java - Sort data in a text file in alphabetical order - Java 如何按字母顺序对文件进行排序? - How to sort a file in alphabetical order? 如何使用Java在文件之间按字母顺序对姓氏进行排序? - How do I use Java to sort surnames in alphabetical order from file to file? 使用Java中的比较器对文件内容进行排序 - To sort the contents of a file using comparator in java 使用方法(Java)使用从另一个文件中获取的字符串数组的内容创建新文件 - Create new file with the contents of a string array taken from another file using methods (Java) JAVA:如何按字母顺序对从文件读取并输出到控制台的字符串进行排序? - JAVA: How to sort strings read from file and output to console in alphabetical order? 从文本文件中读取一行并将其内容拆分并排序,然后以新的排序顺序将其放入文件中 - Reading a line from a text file and splitting its contents and sort them and place them in the file with the new sorting order 以升序和降序对文件内容进行排序 - sort the file contents in ascending and descending order JAVA在新文件中按升序对行进行排序并显示 - JAVA Sort lines in ascending order in new file and display it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM