当前位置: 代码迷 >> J2SE >> 请教一个IO流的有关问题,只要给出代码解决,马上结帖!
  详细解决方案

请教一个IO流的有关问题,只要给出代码解决,马上结帖!

热度:81   发布时间:2016-04-24 13:07:48.0
请教一个IO流的问题,只要给出代码解决,马上结帖!!!
打开一个文本文件,每次读取一行内容。将每行作为一个String读入,并将那个String对象置入一个Vector里。按相反的顺序打印出Vector中的所有行 
在上面的基础上又打开一个文本文件,以便将文字写入其中。将Vector中的行随同行号一起写入文件。

------解决方案--------------------
Java code
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();                }            }        }                       } }
------解决方案--------------------
蹭分^_^

Java code
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();        }    }}
  相关解决方案