繁体   English   中英

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

[英]read two columns from a text file and sort both columns in 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

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

import java.io.*;
import java.lang.*;
import java.util.*;

public class read
 {
        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][];
                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++) {
                    System.out.print(temp[n][m] + " ");
                }
                System.out.print("\n");
            }
        }
                       fr.close();
                       fw.close();
                    bufferFileWriter.close();
}
}

你可以采用这种方法,创建一个List<Item> ,其中Item是一个包含2个Column值的类型。 (x1和x2)

然后编写一个compareTo(Item o) ,它比较在compare中呈现给它的两个Item对象的x1值,如果给出了明确的答案,则返回该答案。

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

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

希望这可以帮助。

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

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();
    }
}

如您所见,在java.util.Collectionsjava.util.Collections调用sort()方法时使用比较器。

输出:

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

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM