日期:2014-05-20 浏览次数:20871 次
package zhao; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.Vector; public class Test { public static void main(String args[]) { Vector<String> v = new Vector<String>(); BufferedReader bufin = null; BufferedWriter bufout = null; try { bufin = new BufferedReader(new FileReader("D:\\eclipse\\test.txt")); String s = null; while((s=bufin.readLine()) != null) { v.add(s); } bufin.close(); String [] array = v.toArray(new String[v.size()]); for(int i=array.length-1;i>=0;i-- ) { System.out.println(array[i]); } bufout = new BufferedWriter(new FileWriter("D:\\eclipse\\testin.txt")); for(String s1:v) { bufout.write(s1+"\n"); } bufout.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { if(bufin!=null) { try { bufin.close(); } catch (IOException e) { e.printStackTrace(); } } if(bufout!=null) { try { bufout.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
------解决方案--------------------
蹭分^_^
import java.io.*; import java.util.*; public class Test{ public void doSomething(String inFileName,String outFileName) throws IOException{ File inFile = new File(inFileName); File outFile = new File(outFileName); BufferedReader br = new BufferedReader(new FileReader(inFile)); BufferedWriter bw = new BufferedWriter(new FileWriter(outFile)); String s = null; Vector<String> list = new Vector<String>(); while((s = br.readLine()) != null){ list.add(s); } br.close(); Collections.reverse(list); int line = 1; for(String tmp:list){ System.out.println(tmp); bw.write((line++) + "\t" + tmp); bw.newLine(); } bw.flush(); bw.close(); } public static void main(String[] args){ try { new Test().doSomething("C:/in.txt","C:/out.txt"); } catch (IOException e) { e.printStackTrace(); } } }