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

时间:2013-09-02 19:01:56

标签: java file text

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代码试图读取文件并打印出其值: 在这里我将整数存储在不同的数组中 代码是:

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

3 个答案:

答案 0 :(得分:2)

  

你可以采用这种方法,创建一个项目为List<Item>的项目   包含2列值的类型。 (x1和x2)

     

然后写一个compareTo(Item o)来比较两者的x1值   在比较中呈现给它的项目对象,如果这给出了明确的   回答,回答了那个答案。

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

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

希望这有帮助。

答案 1 :(得分:0)

以下内容可能会让您了解自己可以做些什么。此示例使用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();
    }
}

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

输出:

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

答案 2 :(得分:0)

我的问题的答案是:

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