public void copyFile(File oldFile, String newPath) throws FileNotFoundException,IOException {
try {
int bytesum = 0;
int byteread = 0;
if (oldFile.exists()) { //文件存在时
InputStream inStream = new FileInputStream(oldFile); //读入原文件
FileOutputStream fs = new FileOutputStream(newPath);
byte[] buffer = new byte[1444];
int length;
while ( (byteread = inStream.read(buffer)) != -1) {
bytesum += byteread; //字节数 文件大小
System.out.println(bytesum);
fs.write(buffer, 0, byteread);
}
inStream.close();
}
} catch (Exception e) {
System.out.println("复制单个文件操作出错");
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
new SystemRead();
}
}
复制代码
错误信息为:
D:\Documents\美图图库\示例图片_01.jpg
C:\Documents and Settings\康明轩\workspace\机器学习和测试\data
复制单个文件操作出错 java.io.FileNotFoundException: C:\Documents and Settings\康明轩\workspace\机器学习和测试\data (拒绝访问。)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:179)
at java.io.FileOutputStream.<init>(FileOutputStream.java:70)
at com.machine.study.SystemRead.copyFile(SystemRead.java:33)
at com.machine.study.SystemRead.<init>(SystemRead.java:23)
at com.machine.study.SystemRead.main(SystemRead.java:51)