FileReader和FileWrite实现文件复制

FileReader和FileWrite实现文件复制

  • 只能拷贝普通文本文件
public class Copy02 {
    public static void main(String[] args) {
        FileReader in = null;
        FileWriter out = null;
        try {
            //读
            in = new FileReader("file.java");
            //写
            out = new FileWriter("file.java");
            
            
            //一边读,一边写
            char[] chars = new char[1024 * 512];
            int readCount = 0;
            while((readCount = in.read(chars)) != -1) {
                out.write(char, 0, readCount);
            }
            
            //刷新
            out.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                }catch (IOException e) {
                    e.printStackTrace();
                }
            } 
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
上一篇:解决datafountain比赛提交.csv文件报错问题


下一篇:移动端 FileReader文件上传