该帖已经被评为新手帖
|
|
---|---|
作者 | 正文 |
发表时间:2009-01-06
kim_ouyang 写道
public void sortFile(String sourceFile, String destFile) { BufferedReader bReader = null; PrintWriter pWriter = null; try { bReader = new BufferedReader(new InputStreamReader( new FileInputStream(sourceFile))); String keyString = bReader.readLine(); Map<Integer, String> map = new HashMap<Integer, String>(); while (keyString != null) { String[] ar = keyString.split(" "); map.put(Integer.valueOf(ar[1]), ar[0]); keyString = bReader.readLine(); } System.out.println("读取文件成功..."); Object[] objects = map.keySet().toArray(); Arrays.sort(objects); pWriter = new PrintWriter(new FileOutputStream(destFile)); int len = objects.length; for (int i = len - 1; i >= 0; i--) pWriter.println(new StringBuilder(map.get(objects[i])).append( " ").append(objects[i]).toString()); pWriter.flush(); } catch (Exception e) { e.printStackTrace(); } finally { if (bReader != null) { try { bReader.close(); } catch (IOException e) { e.printStackTrace(); } } if (pWriter != null) { pWriter.close(); } } System.out.println("保存文件成功...."); } 恩,不错 如果脱离这道题实际点的话和我一样没考虑到数字的 重复问题
|
|
返回顶楼 | |
发表时间:2009-01-06
import java.io.BufferedReader;
import java.io.File; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.LinkedList; import java.util.Arrays; public class SortFile{ // private Map<Integer,String> map; // private Set<Integer> set; private LinkedList<Integer> keyList=new LinkedList<Integer>(); private LinkedList<String> valueList=new LinkedList<String>(); public SortFile(){ // map=new HashMap<Integer,String>(); // set=new TreeSet<Integer>(); } public static void main(String[] args){ String filename = "D:/test1.txt"; String toFilename = "D:/test2.txt"; new SortFile().sortFile(filename, toFilename); } public void sortFile(String filename,String toFilename){ File file = new File(filename); BufferedReader reader=null; File toFile = new File(toFilename); PrintWriter writer = null; try { reader = new BufferedReader(new FileReader(file)); String tempString = null; String[] ss = null; while((tempString = reader.readLine()) != null){ ss = tempString.split(" "); String value = ss[0]; Integer key=Integer.parseInt(ss[1]); keyList.add(key); valueList.add(value); } int size=keyList.size(); Integer[] sortlist=keyList.toArray(new Integer[size]); Arrays.sort(sortlist); writer = new PrintWriter(new FileOutputStream(toFile)); for(Integer key : sortlist){ int index=keyList.indexOf(key); String value=valueList.remove(index); keyList.remove(index); writer.println(value+" "+key); } writer.flush(); } catch (Exception ex) { } finally { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } writer.close(); } } } 这个能解决重复问题。 |
|
返回顶楼 | |
发表时间:2009-01-06
最后修改:2009-01-06
LZ啊,公司面试肯定不是检查API那么简单。
JAVA上机做题一般要涉及面向对象的思维啊,可扩展性什么的,你写的纯粹就是面向过程的代码,所以看起来就是考验API,然后被刷也不知道为什么。 代码实现基本就是楼上 datuo那个意思。 关键词:对象~~ |
|
返回顶楼 | |
发表时间:2009-01-06
最后修改:2009-01-06
datuo 写道
自己把文本里面一行的东西定义个对象。实现Comparable接口
class Student implements Comparable { int age; String name; public Student(int age, String name) { this.age = age; this.name = name; } public int compareTo(Object o) { Student s = (Student)o; return age > s.age ? 1:(age == s.age ? 0 : -1); } } 随便你用什么排序都行了。还可以扩展到多个属性。
这种思路是很好的,也能很好解决后面数字重复的问题。而且这种完全用了面向对象的思想。 代码如下:student.java封装文件中的对象, Test.java为测试类 public class Student implements Comparable<Student> { private String name; private int orderNumber; public Student() { } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getOrderNumber() { return orderNumber; } public void setOrderNumber(int orderNumber) { this.orderNumber = orderNumber; } public int compareTo(Student o) { // return this.orderNumber>o.orderNumber?1:(this.orderNumber==o.orderNumber?0:-1);//小到大排序 return o.orderNumber > this.orderNumber ? 1 : (o.orderNumber == this.orderNumber ? 0 : -1);// 大到小排序 } }
Test.java import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class Test { public static void main(String[] ars){ Test test = new Test(); test.sortFile("d:/source.txt", "d:/dest.txt"); } public void sortFile(String sourceFile, String destFile) { BufferedReader bReader = null; PrintWriter pWriter = null; try { bReader = new BufferedReader(new InputStreamReader( new FileInputStream(sourceFile))); String keyString = bReader.readLine(); List<Student> list = new ArrayList<Student>(); while (keyString != null) { String[] ar = keyString.split(" "); Student student = new Student(); student.setName(ar[0]); student.setOrderNumber(Integer.parseInt(ar[1])); list.add(student); keyString = bReader.readLine(); } System.out.println("读取文件成功..."); Object[] objects = list.toArray(); Arrays.sort(objects); pWriter = new PrintWriter(new FileOutputStream(destFile)); for (Object o:objects) { Student student = (Student) o; pWriter.println(new StringBuilder(student.getName()) .append(" ").append(student.getOrderNumber()) .toString()); } pWriter.flush(); } catch (Exception e) { e.printStackTrace(); } finally { if (bReader != null) { try { bReader.close(); } catch (IOException e) { e.printStackTrace(); } } if (pWriter != null) { pWriter.close(); } } System.out.println("保存文件成功...."); } }
|
|
返回顶楼 | |
发表时间:2009-01-06
呵呵 LS 正解
|
|
返回顶楼 | |
发表时间:2009-01-06
弄的太复杂了吧?真要考虑可扩展性那要拆分好多类和方法。
再下去sping和面向接口都出来了 |
|
返回顶楼 | |
发表时间:2009-01-06
很基本的问题
但很重要 |
|
返回顶楼 | |
发表时间:2009-01-06
谢谢大家的指教,当时编写时的确没注意到面向对象的使用就只想功能的实现了,小弟这受教了!
|
|
返回顶楼 | |
发表时间:2009-01-06
你用正则表达式把它搞成数组,排序,我看好简单啊,
你写的好复杂,有没有意义 |
|
返回顶楼 | |
发表时间:2009-01-06
个人建议;
1. 命名不规范,不可取 read_writeFile 2. map是unique key,如果有数值相同的或同名的咋整 3. 简单实现,就是楼上提到的shell, sort -k2n <t.txt > r.txt 4. 排序,用API,自己写Comparator 5. 前置条件检查,public int compare(Object o1, Object o2),最好做null判断. |
|
返回顶楼 | |