其他分享
首页 > 其他分享> > FileOutputStram

FileOutputStram

作者:互联网

1、FileOutputStream实现了向文件中写出byte数据的方法

拷贝方法

public static void copyFile(File srcFile,File destFile) throws IOException {
        if(!srcFile.exists()) {
            throw new IllegalArgumentException("文件:"+srcFile+"不存在");
        }
        if(!srcFile.isFile()) {
            throw new IllegalArgumentException(srcFile + "不是文件");
        }
        FileInputStream  in = new FileInputStream(srcFile);
        FileOutputStream out = new FileOutputStream(destFile);
        byte[] buf =new byte[8*1024];
        int b;
        while((b=in.read(buf,0,buf.length))!=-1) {
            out.write(buf,0,b);
            out.flush();//最好jias
        }
        in.close();
        out.close();
    }

 

测试类

public class IOUtilTest3 {
    public static void main(String[] args) {
        try {
            IOUtil.copyFile(new File("文件路径"), new File("文件路径"));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
​

 

 

标签:srcFile,FileOutputStram,FileOutputStream,File,new,buf,out
来源: https://www.cnblogs.com/mhy123/p/16393351.html