ITeye博客

规范的流方法之复制文件代码

tobemoved · 2008-07-08 09:58 · java代码 · Markdown

import java.util.ArrayList; import java.util.Vector; import java.util.Map; import java.util.Hashtable; import java.io.*;

/**

  • Created by IntelliJ IDEA.

  • User: Administrator

  • Date: 2008-7-2

  • Time: 13:40:28

  • To change this template use File | Settings | File Templates. */ public class Newone { public static void main(String arg) throws Exception { Newone exam = new Newone(); exam.dod(); } public void dod() { //初始化输入、输出流和缓冲流 FileReader fr; FileWriter fw; BufferedReader in = null; BufferedWriter out = null; try{ //源文件资源 File source = new File("source.txt"); //获得源文件路径(默认为C:\Documents and Settings\Administrator\IdeaProjects\newone) System.out.println(source.getAbsolutePath()); //创建复制文件名 File copy = new File("copy.txt");

      fr = new FileReader(source);
     in = new BufferedReader(fr);
      fw = new FileWriter(copy);
     out = new BufferedWriter(fw);
     //进行复制运作
     String s = null;
     //循环读取每一行并且写入
     while((s = in.readLine()) != null)
         out.write( s);
    }catch (Exception eof){}
    finally{
        //必要的规范写法,确保关闭流,解放占用内存
        try {
            if(in != null)
                 in.close();
        }catch(IOException e)  {}
    
         try {
             if(out != null) {
                  out.close();
             }
         }catch(IOException ff) { }
    }
    

    } }

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。