java io 文件复制代码

package date;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class stream {
    
    public static void main(String[] args) throws IOException{
        
        File file = new File("f:/1.txt");//创建文件对象
        
        if(!file.exists()){
            try {
                file.createNewFile();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        
        File outFile = new File("f:/2.txt");//创建文件对象
        
        InputStream in = null;
        OutputStream out = null;
        
        try {
            in = new FileInputStream(file);
            out = new FileOutputStream(outFile);
            byte [] buff = new byte[1024];  //存入缓存
            int len = 0;
            while((len=in.read(buff))!=-1){
                out.write(buff, 0, len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally{
            in.close();  
            out.close();  
        }
        
    }

}

猜你喜欢

转载自blog.csdn.net/qq_17197861/article/details/79892223