从文本文件中读取两列,并用java对这两列进行排序

从文本文件中读取两列,并用java对这两列进行排序,java,file,text,Java,File,Text,我的工作是我应该阅读这个输入文件,删除空行并对这两列进行排序 下面是我的java代码,它试图读取文件并打印其值: 在这里,我将整数存储在不同的数组中 代码是: 18 14 19 15 20 16 21 17 22 18 23 19 24 20 25 20 25 21 47 44 48 44 48 45 49 44 49 43 49 42 50 42 50 43 51 43 53 40 53 39 53 38 54 38 54 39 import java.io.*; 导入java.lang

我的工作是我应该阅读这个输入文件,删除空行并对这两列进行排序 下面是我的java代码,它试图读取文件并打印其值: 在这里,我将整数存储在不同的数组中 代码是:

18 14
19 15
20 16
21 17
22 18
23 19
24 20
25 20
25 21

47 44
48 44
48 45
49 44

49 43
49 42
50 42
50 43
51 43
53 40
53 39
53 38
54 38
54 39
import java.io.*;
导入java.lang.*;
导入java.util.*;
公开课阅读
{
公共静态void main(字符串[]args)引发异常
{
System.out.println(“输入文件名”);
DataInputStream dis=新的DataInputStream(System.in);
字符串dir1=dis.readLine();
文件填充=新文件(dir1);
System.out.println(“输入输出文件名”);
DataInputStream dis2=新的DataInputStream(System.in);
字符串dir3=dis.readLine();
字符串path=“E:/photos”;
字符串newpath=path+“/”+dir3;
文件输出文件=新文件(新路径);
int newcount=0,newcount1=0;
FileReader fr=新的FileReader(填充);
BufferedReader fr11=新的BufferedReader(fr);
FileWriter fw=新的FileWriter(输出文件);
BufferedWriter bufferFileWriter=新的BufferedWriter(fw);
扫描仪输入=新扫描仪(填充);
字符串[]outputArray1=新字符串[31];
字符串[]outputarry2=新字符串[31];
int i=0;
while(input.hasNextLine())
{
String line=input.nextLine();
if(line.length()>0)
{
String[]columns=line.split(“”);
System.out.println(“我的第一列:+列[0]);
System.out.println(“我的第二列:“+列[1]);
outputArray1[i]=列[0];
outputArray2[i]=列[1];
i++;
}
}
字符串[][]临时=新字符串[2][];
温度[0]=输出阵列1;
温度[1]=输出阵列2;
对于(int k=0;k
您可以使用这种方法,创建一个
列表
,其中项目是 包含2列值的类型。(x1和x2)

然后将
比较器写入(项目o)
,比较两个项目的x1值 比较中呈现给它的项对象,如果它给出了一个确定的 回答,回答

公共类项实现可比较{
私有整数int1;
私有整数int2;
@凌驾
公共整数比较(o项){
返回int1>(o.int1);
}
}

希望这能有所帮助。

以下内容可能会让您对​​您可以做什么。此示例使用Java 7的一些功能。排序是
C1 ASC,C2 DESC
,类似于SQL
ORDER BY
,其中
C1
是第一列,
C2
是第二列

public class Item implements Comparable<Item> {
    private Integer int1;
    private Integer int2;

    @Override
    public int compareTo(Item o) {
        return int1 > (o.int1);
    }
}

我的问题的答案是:

18 14
19 15
20 16
21 17
22 18
23 19
24 20
25 21
25 20
47 44
48 45
48 44
49 44
49 43
49 42
50 43
50 42
51 43
53 40
53 39
53 38
54 39
54 38

你到底想在那里排序什么?你甚至还没有创建一个包含所有输入的数组?顺便说一句,为什么你在那里使用
DataInputStream
,而不是
Scanner
?如何
sort-nout.file
?…并且永远不要使用DataInputStream来读取文本。这里我使用DataInputStream来读取文件名\但是我有相同的键和不同的值,所以我没有使用哈希映射。我的实际输入是:18141915212012122231924220220252525252525252525252525252525252525252525252525252525252525252525252525252525252525252525252525252525252525252525252525252525252525252525252525252525252525252525252525252525252525252525252525252525252525252525252525252525252525252525252o显示不正确的结果
public static class Row {
    public Row(String c1, String c2) {
        this.c1 = c1;
        this.c2 = c2;
    }
    String c1;
    String c2;
}

public static void main(String[] args) throws Exception {
    JFileChooser fileChooser = new JFileChooser();
    int option = fileChooser.showOpenDialog(null);
    if (option == JFileChooser.APPROVE_OPTION) {
        Path inputPath = fileChooser.getSelectedFile().toPath();
        option = fileChooser.showSaveDialog(null);
        if (option == JFileChooser.APPROVE_OPTION) {
            Path outputPath = fileChooser.getSelectedFile().toPath();
            sortAndSave(inputPath, outputPath);
        }
    }
}

public static void sortAndSave(Path inputPath, Path outputPath)
        throws IOException {
    List<String> lines = Files.readAllLines(inputPath,
            StandardCharsets.UTF_8);
    List<Row> rows = new ArrayList<>();
    for (String line : lines) {
        String[] array = line.split("\\s+");
        rows.add(new Row(array[0], array[1]));
    }
    Collections.sort(rows, new Comparator<Row>() {
        public int compare(Row r1, Row r2) {
            // C1 ASC, C2 DESC
            if (r1.c1.compareTo(r2.c1) == 0) {
                return r2.c2.compareTo(r1.c2);
            }
            return r1.c1.compareTo(r2.c1);
        }
    });
    try (BufferedWriter writer = Files.newBufferedWriter(outputPath,
            StandardCharsets.UTF_8);
            PrintWriter out = new PrintWriter(writer);) {
        for (Row row : rows) {
            out.println(row.c1 + " " + row.c2);
        }
        out.flush();
    }
}
18 14
19 15
20 16
21 17
22 18
23 19
24 20
25 21
25 20
47 44
48 45
48 44
49 44
49 43
49 42
50 43
50 42
51 43
53 40
53 39
53 38
54 39
54 38
import java.io.*;
import java.lang.*;
import java.util.*;

public class readnew
 {
        public static void main(String[] args) throws Exception 
        {
        System.out.println("Enter file name");
        DataInputStream dis=new DataInputStream(System.in);
        String dir1=dis.readLine();
        File infile = new File(dir1);
        System.out.println("Enter output file name");
        DataInputStream dis2=new DataInputStream(System.in);
        String dir3=dis.readLine();
        String path="E:/photos";
        String newpath=path + "/" +dir3;
        File outfile = new File(newpath);
        int newcount=0,newcount1=0;
        FileReader fr=new FileReader(infile);
        BufferedReader fr11= new BufferedReader(fr);
        FileWriter fw = new FileWriter(outfile);
        BufferedWriter bufferFileWriter  = new BufferedWriter(fw);
        Scanner input = new Scanner(infile);
        String[] outputArray1 = new String[31];
        String[] outputArray2 = new String[31];
        int i = 0;
               while (input.hasNextLine()) 
                {
                        String line = input.nextLine();
                        if(line.length() > 0)
                {
                        String[] columns = line.split(" ");
                        System.out.println("my first column : "+ columns[0] );
                        System.out.println("my second column : "+ columns[1] );
                        outputArray1[i] = columns[0];
                        outputArray2[i] = columns[1];
                        i++;
                }
                }
                String[][] temp = new String[2][];
                String[][] temp1 = new String[i][2];
                temp[0]= outputArray1;
                temp[1]= outputArray2;
               for (int k=0;k<2;k++)
        for (int j=0;j<i;j++)
        {
System.out.println("new row"+k+"new col"+j+"value="+temp[k][j]);
        }
        if (temp.length > 0) {
            for (int m = 0; m< temp[0].length; m++) {
                for (int n = 0; n< temp.length; n++) {
                temp1[m][n]=temp[n][m];
                    System.out.print(temp[n][m] + " ");
                }
                System.out.print("\n");
            }
        }
        System.out.println("transpose");
        for (int a=0;a<i;a++)
        for (int b=0;b<2;b++)
        {
        System.out.println("new row"+a+"new col"+b+"value="+temp1[a][b]);
        }
        System.out.println("sort");
    final Comparator<String[]> arrayComparator = new Comparator<String[]>() 
    {
        @Override
        public int compare(String[] o1, String[] o2)
        {
            return o1[1].compareTo(o2[1]);
        }
    };
    final Comparator<String[]> arrayComparator1 = new Comparator<String[]>() 
    {
        @Override
        public int compare(String[] o1, String[] o2) 
        {
            return o1[0].compareTo(o2[0]);
        }
    };
     Arrays.sort(temp1, arrayComparator);
     Arrays.sort(temp1, arrayComparator1);
     for(int p=0; p<i; p++)
        {
            for(int q=0; q<2; q++) 
            {
                System.out.print(temp1[p][q]+" ");
            }
            System.out.print("\n");
        }

                       fr.close();
                       fw.close();
                    bufferFileWriter.close();
}
}
18 14
18 14
19 15
19 15
20 16
20 16
21 17
21 17
22 18
22 18
22 18
22 18
23 19
23 19
23 19
23 19
24 20
24 20
24 20
25 20
25 20
25 20
25 21
25 21
25 21
26 21
26 21
26 21
27 21
27 21
27 21