-3

我有 2 个名为“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 

和具有以下内容的“text2”

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

任何人都可以帮我提供伪代码来解决上述问题。提前致谢。

4

6 回答 6

3

在 Unix 机器上:

sort -n -k1 file1 file2 > results.txt
于 2012-06-03T18:53:13.173 回答
2

将文件读入列表,调用:

Collections.sort(yourList);

然后遍历排序列表并将内容写入文件。

读取文件示例:http ://www.roseindia.net/java/beginners/java-read-file-line-by-line.shtml 写入文件示例:http ://www.roseindia.net/java/beginners/java -write-to-file.shtml

于 2012-06-03T19:01:20.633 回答
2

试试这个。

  1. 您从 file1 读取并将其存储在临时文件中,然后读取 file2 并将其也存储在同一个临时文件中。

  2. 现在使用 Scanner (ie. next()) 来读取这个临时文件,只使用 next() ,这将只读取每行中的第一个单词,将其转换为 doubleDouble.parseDouble()使用 this 进行比较,同时将整行放入TreeSet()as 字符串中

  3. 将 TreeSet() 内容写入文件 3。

  4. 最终结果将是你想要的。

只是认为这会有所帮助,我也在展示我阅读和附加的方式。

从文件中读取

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

 }


}
于 2012-06-03T19:02:11.367 回答
1

未测试:

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();
}
于 2012-06-03T18:51:43.937 回答
1

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

HashMap<Double,String> hash=new Hashmap<Double,String> ();

Double 是第一部分 Double(1191196800.681), String(- !AIVDM,1,1,,,13aG?N0rh20E6sjN1J=9d4<00000,0*1D)

 Map<String, String> sortedMap = new TreeMap<String, String>(hash);


out.println(sortedMap);
于 2012-06-03T18:59:39.917 回答
1

这是完成上述工作的简单程序。

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
 }

谢谢

于 2012-06-04T09:30:08.380 回答