Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用java对两个文件的内容进行排序_Java_Sorting - Fatal编程技术网

用java对两个文件的内容进行排序

用java对两个文件的内容进行排序,java,sorting,Java,Sorting,我有两个名为“text1”的文本文件,内容如下 1191196800.681 - !AIVDM,1,1,,,13aG?N0rh20E6sjN1J=9d4<00000,0*1D 1191196800.683 - !AIVDM,1,1,,,13aG?N0rh20E6sjN1J=9d4<00000,0*1D 1191196800.685 - !AIVDM,1,1,,,13aG?N0rh20E6sjN1J=9d4<00000,0*1D 1191196800.682 - !AIVDM

我有两个名为“text1”的文本文件,内容如下

1191196800.681 - !AIVDM,1,1,,,13aG?N0rh20E6sjN1J=9d4<00000,0*1D
1191196800.683 - !AIVDM,1,1,,,13aG?N0rh20E6sjN1J=9d4<00000,0*1D
1191196800.685 - !AIVDM,1,1,,,13aG?N0rh20E6sjN1J=9d4<00000,0*1D 
1191196800.682 - !AIVDM,1,1,,,13aG?N0rh20E6sjN1J=9d4<00000,0*1D
1191196800.684 - !AIVDM,1,1,,,13aG?N0rh20E6sjN1J=9d4<00000,0*1D
1191196800.686 - !AIVDM,1,1,,,13aG?N0rh20E6sjN1J=9d4<00000,0*1D 
 1191196800.681 - !AIVDM,1,1,,,13aG?N0rh20E6sjN1J=9d4<00000,0*1D
 1191196800.682 - !AIVDM,1,1,,,13aG?N0rh20E6sjN1J=9d4<00000,0*1D
 1191196800.683 - !AIVDM,1,1,,,13aG?N0rh20E6sjN1J=9d4<00000,0*1D
 1191196800.684 - !AIVDM,1,1,,,13aG?N0rh20E6sjN1J=9d4<00000,0*1D
 1191196800.685 - !AIVDM,1,1,,,13aG?N0rh20E6sjN1J=9d4<00000,0*1D
1196800.681-!AIVDM,1,1,13aG?N0rh20E6sjN1J=9d4未测试:

BufferedReader reader1 = new BufferedReader(new FileReader("file1.txt"));
BufferedReader reader2 = new BufferedReader(new FileReader("file2.txt"));
PrintWriter out = new PrintWriter ("out.txt");

String line1 = reader1.readLine();
String line2 = reader2.readLine();

while(line1 !=null && line2 != null) {
  out.println(line1);
  out.println(line2);
  line1 = reader1.readLine();
  line2 = reader2.readLine();
}
在unix计算机上:

sort -n -k1 file1 file2 > results.txt

读取这两个文件并将其存储到哈希映射中,如下所示

HashMap<Double,String> hash=new Hashmap<Double,String> ();
HashMap hash=newhashmap();
双人是第一部分
双精度(1191196800.681),字符串(-!AIVDM,1,1,,,13aG?N0rh20E6sjN1J=9d4将文件读入列表,调用:

Collections.sort(yourList);
然后遍历已排序的列表并将内容写入文件

读取文件示例: 写文件示例:

试试这个

  • 读取文件1并将其存储在临时文件中,然后读取文件2并将其也存储在同一临时文件中

  • 现在使用Scanner(即
    next()
    )来读取这个临时文件,只使用next(),这将只读取每行中的第一个单词,使用
    double.parseDouble()将其转换为double。
    将整行作为字符串放到
    TreeSet()
    中进行比较

  • 在文件3中写入TreeSet()内容

  • 最终结果将是你想要的

  • 虽然这会很有帮助,但我也展示了我阅读和附加的方式

    用于从文件中读取

    File f = new File("my.txt");
    FileReader fr = new FileReader(f);
    BufferedReader br  = new BufferedReader(fr);
    
    String s = null;
    
    while ((br=readLine())!=null) {
    
    // Do whatever u want to do with the content of the file,eg print it on console using SysOut...etc
    
    }
    
    br.close();
    
    要写入文件,请执行以下操作:

    Boolean isDone = true;
    Scanner scan = new Scanner(System.in);
    File f = new File("my.txt");
    FileWriter fr = new FileWriter(f,true);
    BufferedWriter br  = new BufferedWriter(fr);
    
    while (b) {
    
       if (!b) {
    
     br.append(new Scanner(System.in).nextLine());
    
     }
    
    
    }
    

    下面是完成上述工作的简单程序

    public class RunSysCmd {
    
    /**Executes the Linux command necessary for sorting
     * @param String
     */
    public static void main(String[] args) {
    
        try {
            // command to be executed
            String cmd = "/usr/bin/sort -n -k1 /home/General_DataStructure/r1.nmea /home/General_DataStructure/r2.nmea";
    
            // new file where the result will be stored
            BufferedWriter out = new BufferedWriter(new FileWriter(new File("/home/General_DataStructure/r3.nmea")));
            String line;
    
            // run the command specified in the cmd variable
            final Process process = Runtime.getRuntime().exec(cmd);
    
            // read the result executed by the previous command
            BufferedReader buf = new BufferedReader(new InputStreamReader(process.getInputStream()));
    
            // write the output of the command to new file
            while ((line = buf.readLine())!=null) {
                out.write(line);
                out.newLine();
    
            }
    
            // close the file
            buf.close();
            out.close();
    
            // causes the thread to wait until the process represented by this Process object the is terminated
            process.waitFor();
    
            // get the return value of the process. The value 0 means successful execution of the thread
            int returnCode = process.exitValue();
            System.out.println(returnCode);
    
        } catch (IOException e) {
            e.printStackTrace();
    
        }
    
        catch (InterruptedException e) {
            e.printStackTrace();
    
        }
    
      }// main ends here
     }
    

    谢谢

    这两个文件都已经自己排序了吗?是的,文件是自己排序的。而且,这不需要任何Java代码。在Unix和Windows中都可以通过命令行完成(尽管在Windows中有点困难)
    public class RunSysCmd {
    
    /**Executes the Linux command necessary for sorting
     * @param String
     */
    public static void main(String[] args) {
    
        try {
            // command to be executed
            String cmd = "/usr/bin/sort -n -k1 /home/General_DataStructure/r1.nmea /home/General_DataStructure/r2.nmea";
    
            // new file where the result will be stored
            BufferedWriter out = new BufferedWriter(new FileWriter(new File("/home/General_DataStructure/r3.nmea")));
            String line;
    
            // run the command specified in the cmd variable
            final Process process = Runtime.getRuntime().exec(cmd);
    
            // read the result executed by the previous command
            BufferedReader buf = new BufferedReader(new InputStreamReader(process.getInputStream()));
    
            // write the output of the command to new file
            while ((line = buf.readLine())!=null) {
                out.write(line);
                out.newLine();
    
            }
    
            // close the file
            buf.close();
            out.close();
    
            // causes the thread to wait until the process represented by this Process object the is terminated
            process.waitFor();
    
            // get the return value of the process. The value 0 means successful execution of the thread
            int returnCode = process.exitValue();
            System.out.println(returnCode);
    
        } catch (IOException e) {
            e.printStackTrace();
    
        }
    
        catch (InterruptedException e) {
            e.printStackTrace();
    
        }
    
      }// main ends here
     }