(完美解决)java文件操作报错:java.io.FileNotFoundException:(拒绝访问)

01-错误信息:

Exception in thread "main" java.io.FileNotFoundException: e:\bb (拒绝访问。)
	at java.io.FileOutputStream.open0(Native Method)
	at java.io.FileOutputStream.open(Unknown Source)
	at java.io.FileOutputStream.<init>(Unknown Source)
	at java.io.FileOutputStream.<init>(Unknown Source)
	at com.eleven.SevenDemo04.copyFile(SevenDemo04.java:44)
	at com.eleven.SevenDemo04.main(SevenDemo04.java:21)

在这里插入图片描述

02-代码:

1)修改前:

public static void main(String[] args) throws Exception {

		File source = new File("d:\\aa\\aa.txt"); // 源目标路径
		File dest = new File("e:\\bb\\"); // 目标路径

		copyFile(source, dest);

	}

2)修改后:

错误的原因是读取的目录后面忘加了文件名!

public static void main(String[] args) throws Exception {

		File source = new File("d:\\aa\\aa.txt"); // 源目标路径
		File dest = new File("e:\\bb\\" + source.getName()); // 目标路径

		copyFile(source, dest);

	}
发布了90 篇原创文章 · 获赞 284 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/qq_41293896/article/details/104022127