我有两个名为“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
任何人都可以帮我提供伪代码来解决上述问题。提前谢谢。
答案 0 :(得分:3)
在unix机器上:
sort -n -k1 file1 file2 > results.txt
答案 1 :(得分: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
答案 2 :(得分:2)
试试这个。
您从file1读取并将其存储在临时文件中,然后读取文件2并将其存储在同一个临时文件中。
现在使用Scanner(即next()
)来读取这个临时文件,只使用next(),这将只读取每行中的第一个单词,使用{{将其转换为double 1}}使用它进行比较,同时将整个行放在Double.parseDouble()
中作为字符串
将TreeSet()内容写入file3。
最终结果将是你想要的。
只是认为这会有所帮助,我也会展示我阅读和追加的方式。
从文件中读取
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();
答案 3 :(得分: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();
}
答案 4 :(得分:1)
读取两个文件将其存储到哈希映射中,如下所示
HashMap<Double,String> hash=new Hashmap<Double,String> ();
Double是第一部分 双(1191196800.681),字符串( - !AIVDM,1,1 ,,, 13aG?N0rh20E6sjN1J = 9d4&lt; 00000,0 * 1D)
Map<String, String> sortedMap = new TreeMap<String, String>(hash);
out.println(sortedMap);
答案 5 :(得分: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
}
谢谢