IO流复制文件Exception in thread "main" java.io.FileNotFoundException: A (拒绝访问。) 异常
来源:互联网 发布:节奏大师没有网络可用 编辑:程序博客网 时间:2024/06/06 15:46
在学习java的时候写了一段程序,利用IO流复制文件。但是运行时出现拒绝访问:
Exception in thread "main" java.io.FileNotFoundException: A (拒绝访问。)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at cn.kang01.Mycopy.Mycopying(Mycopy.java:44)
at cn.kang01.Mycopy.main(Mycopy.java:28)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at cn.kang01.Mycopy.Mycopying(Mycopy.java:44)
at cn.kang01.Mycopy.main(Mycopy.java:28)
经排查,终于找到原因!IO操作的是文件而不是文件夹,那么我们在写源文件时,就要谨慎。
package cn.kang01;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 Mycopy {public static void main(String[] args) throws IOException {// 封装目录File srcfile = new File("A");// 源文件File destfile = new File("B");// 目标文件// System.out.println(srcfile.getAbsoluteFile());// 如果目标文件不存在,就创建if (!destfile.exists()) {destfile.mkdir();}File[] f = srcfile.listFiles();// 获得源文件下所有文件路径for (File oldfile : f) {// System.out.println(n.getName());String name = n.getName();// 获得名字File newfile = new File(destfile, name);// 创建新的路径(目标路径)//System.out.println(newfile);Mycopying(oldfile, newfile);//可能很多朋友会出现文件拒绝访问,问题就是出在这里,IO是处理文件而不是文件夹//我第一次写的时候也是出现拒绝访问,经过排查问题就是Mycopying(sfile, dfile)方法的第一个参数:源文件,//有的朋友可能会写成前边封装的srcfile,sfile必须是文件,不能是文件夹;//srcfile.listFiles()获得所有文件和子目录,我们可以操作数组的元素。}}/** * 复制文件 * * @param sfile * 源文件 * @param dfile * 目标文件 * @throws IOException */public static void Mycopying(File sfile, File dfile) throws IOException {BufferedInputStream bis = new BufferedInputStream(new FileInputStream(sfile));BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(dfile));byte[] b = new byte[1024];int len = 0;while ((len = bis.read(b)) != -1) {bos.write(len);bos.flush();}bos.close();bis.close();}}
阅读全文
0 0
- IO流复制文件Exception in thread "main" java.io.FileNotFoundException: A (拒绝访问。) 异常
- Exception in thread "main" java.io.FileNotFoundException: E:\java\.metadata (拒绝访问。)的理解
- Exception in thread "main" java.io.FileNotFoundException
- 关于IO流在复制文件时出现java.io.FileNotFoundException: D:\xxx (拒绝访问。) 拒绝访问的问题
- java.io.FileNotFoundException: savepath\huachuan\photo (拒绝访问。)异常处理
- 解决io流java.io.FileNotFoundException: 【D:\\***\\***】 (拒绝访问。)
- java.io.FileNotFoundException: ……(拒绝访问。)
- java.io.FileNotFoundException: D:\Test (拒绝访问。)
- java.io.FileNotFoundException: F:\jstorm (拒绝访问.)
- java.io.FileNotFoundException \xxx(拒绝访问。)
- java.io.FileNotFoundException: e:\temp (拒绝访问。)
- java.io.FileNotFoundException: url(拒绝访问。)问题
- Exception in thread "main" java.lang.RuntimeException: java.sql.SQLException: Io 异常: The Network Ada
- spark程序异常:Exception in thread "main" java.io.IOException: No FileSystem for scheme: hdfs
- UDP网络编程时出现:Exception in thread "main" java.io.EOFException异常
- Exception in thread "main" java.io.IOException: Permission denied
- Exception in thread "main" java.io.IOException: Job failed!
- Exception in thread "main" java.io.IOException: Permission denied
- 静态调用OpenCV库以及生成自己的静态库
- POI2Vec Geographical Latent Representation for Predicting Future Visitors 论文笔记
- 学习for in循环
- UVA
- java提高篇(十)-----详解匿名内部类
- IO流复制文件Exception in thread "main" java.io.FileNotFoundException: A (拒绝访问。) 异常
- (C++)vector总结
- 封装数据库查询方法
- 表单标签<form>
- POJ3041 二分图最小点覆盖(匈牙利算法)
- android 文字的处理
- 在分布式配置K3中间层时报中间层创建事务失败 处理方法
- 【DP】HDU 6170/2017多校9 1010 Two strings
- static的作用