I/O的学习

I/O学习的开始

import java.io.FileWriter;
import java.io.IOException;

public class Myio {
    
    
    public static void main(String[] args) {
    
    
        FileReader fr = null;
        FileWriter fw = null;
        try {
    
    
            File srcFile = new File("D:\\test\\test.txt");//test文件中已经保存了一些文字
            File destFile = new File("D:\\test1\\test1.txt");
            fr = new FileReader(srcFile);
            fw = new FileWriter(destFile);
            char[] cbuf = new char[1];
            int len;
            while ((len = fr.read(cbuf)) != -1) {
    
    
                fw.write(cbuf,0,len);
                System.out.print(cbuf);
            }
        }catch (Exception e) {
    
    
            e.printStackTrace();
        }finally {
    
    
            try {
    
    
                if(fw != null)
                    fw.close();
            }catch (IOException e) {
    
    
                e.printStackTrace();
            }finally {
    
    
                try {
    
    
                    if (fr != null)
                        fr.close();
                }catch (IOException e) {
    
    
                    e.printStackTrace();
                }
            }
        }
    }
}

运行结果如下:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/YiNianShangE/article/details/125038652