【发布时间】:2009-10-04 23:57:12
【问题描述】:
我正在从三个文件中读取数据,然后我想根据某些标准对记录进行排序
每个文件都有以下格式的记录
名字姓氏性别颜色日期
这是我拥有的代码...我只能使用 collections.sort 对名字进行排序...我如何对其他列进行排序...任何帮助将不胜感激..
import java.io.BufferedInputStream;
import java.io.*;
import java.sql.*;
import java.io.RandomAccessFile;
import java.io.*;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.lang.Object;
import java.util.Vector;
import java.util.*;
/**
* This program reads a text file line by line and print to the console.
* It uses FileOutputStream to read the file.
*/
public class test1 {
public static void main(String[] args)
{
try {
ArrayList<String> rows = new ArrayList<String>();
// ArrayList<String> rows1 = new ArrayList<String>();
// ArrayList<String> rows2 = new ArrayList<String>();
// ArrayList<String> rows3 = new ArrayList<String>();
BufferedReader comma = new BufferedReader(new FileReader("C:\\comma.txt"));
BufferedReader pipe = new BufferedReader(new FileReader("C:\\pipe.txt"));
BufferedReader space = new BufferedReader(new FileReader("C:\\space.txt"));
String c= "";
String p;
String s;
while((c = comma.readLine()) != null) {
rows.add(c);
}
Collections.sort(rows);
// Collections.sort(rows2);
FileWriter writer = new FileWriter("output.txt");
for(String cur: rows)
{
writer.write(cur+"");
}
// for(String cur: rows1)
// writer.write(cur+"\n");
// for(String cur: rows2)
// writer.write(cur+"\n");
comma.close();
// pipe.close();
// space.close();
writer.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
【问题讨论】:
-
随意省略导入
-
这也是他的工作面试代码示例。
-
您在 SO 中发布求职面试练习的事实非常有趣。