java.io.FileNotFoundException: D:\游戏. (拒绝访问。)
at java.base/java.io.FileInputStream.open0(Native Method)
at java.base/java.io.FileInputStream.open(FileInputStream.java:213)
at java.base/java.io.FileInputStream.<init>(FileInputStream.java:155)
at sn.sxt.复制.TestCopy.copyFile(TestCopy.java:22)
at sn.sxt.复制.TestCopy.main(TestCopy.java:14)
这是代码
package sn.sxt.复制;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class TestCopy {
public static void main(String[] args) throws IOException {
File srcFile=new File("D:\\游戏.");
File targeFile =new File("D:\\游戏.");
copyFile(srcFile, targeFile);
}
public static void copyFile(File srcFile,File targeFile) throws IOException {
//(1)提高读取效率,从数据源 字节输入流
BufferedInputStream bis = null;
//(2)提高写入效率,写到目的地 字节输出流
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(new FileInputStream(srcFile));
bos = new BufferedOutputStream(new FileOutputStream(targeFile));
//(3)边读边写
byte[] buf=new byte[1024]; //数组当中中转站
int len=0; //用于储存数
while((len=bis.read(buf))!=0) { //把输入的数据存到buf数组中,赋值到len中 。当不等于0时(没有数据)停止
bos.write(buf,0,len); //添加到bos中 (缓存到buf数组中,从0开始,到循环结束)
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
//(5)关闭
try {
if(bos!=null) {
bos.close();
}if(bis!=null) {
bis.close();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}