1.报错解决 :java.io.FileNotFoundException: G:\dad (拒绝访问。)

参考文献:(364条消息) java.io.FileNotFoundException:(拒接访问)_corelone2的博客-CSDN博客_java.io.filenotfoundexception

2.code

代码参考地址:(364条消息) java中文件拷贝的几种方式_babarianDual的博客-CSDN博客

package day01;import java.io.*;
import java.nio.channels.FileChannel;
import java.nio.file.Files;public class 文件拷贝 {public static void main(String[] args) throws IOException {String s1="G:\\dad\\hb.jpg";String s2="G:\\dada\\12.png";File file1 = new File(s1);//获取文件名称String name = file1.getName();System.out.println(name);File file2 = new File(s2);copyFileByChannel(file1,file2);}public static void copyFileByChannel(File file, File fileTo) throws IOException {FileChannel fileChannel = new FileInputStream(file).getChannel();FileChannel fileChannelTo = new FileOutputStream(fileTo).getChannel();for (long count = fileChannel.size(); count > 0; ) {long transferred = fileChannel.transferTo(fileChannel.position(), count, fileChannelTo);count -= transferred;}}public static void copyFileByStream(File file, File fileTo) throws IOException {InputStream in = new FileInputStream(file);OutputStream out = new FileOutputStream(fileTo);byte[] buffer = new byte[1024];int length;while ((length = in.read(buffer)) > 0) {out.write(buffer, 0, length);}}}
package day01;import java.io.*;
import java.nio.channels.FileChannel;
import java.nio.file.Files;public class 文件拷贝 {public static void main(String[] args) throws IOException {String s1="G:\\dad\\hb.jpg";String s2="G:\\dada\\12.png";File file1 = new File(s1);//获取文件名称String name = file1.getName();System.out.println(name);File file2 = new File(s2);copyFileByChannel(file1,file2);}public static void copyFileByChannel(File file, File fileTo) throws IOException {FileChannel fileChannel = new FileInputStream(file).getChannel();FileChannel fileChannelTo = new FileOutputStream(fileTo).getChannel();for (long count = fileChannel.size(); count > 0; ) {long transferred = fileChannel.transferTo(fileChannel.position(), count, fileChannelTo);count -= transferred;}}public static void copyFileByStream(File file, File fileTo) throws IOException {InputStream in = new FileInputStream(file);OutputStream out = new FileOutputStream(fileTo);byte[] buffer = new byte[1024];int length;while ((length = in.read(buffer)) > 0) {out.write(buffer, 0, length);}}}

运行结果

public static void main(String[] args) throws IOException {//文件的全路径String s1="G:\\dad\\hh.png";System.out.println(s1);//通过文件的全路径获取文件名System.out.println(s1.substring(s1.lastIndexOf("\\")).replace("\\",""));//通过文件的全路径获 文件名之前的路径System.out.println(s1.substring(0,s1.lastIndexOf("\\")));String s2="G:\\dada\\12.png";//这一步获取了文件夹名称  String s1="G:\\dad\\hh.png";--这获取的是文件名称File file1 = new File(s1.substring(0,s1.lastIndexOf("\\")));//获取文件名称String name = file1.getName();System.out.println("我是绝对路径"+file1.getAbsolutePath());System.out.println(name);File file2 = new File(s2);// copyFileByChannel(file1,file2);}

//从一个文件夹目录中拷贝文件到另一个文件夹目录

   File srcFile = new File("G:\\dad\\hb99.jpg");File destDir = new File("G:\\dada");//在destDir下创建一个和srcFile同名的文件File destFile = new File(destDir,srcFile.getName());FileInputStream fis = new FileInputStream(srcFile);FileOutputStream fos = new FileOutputStream(destFile);byte[] bytes = new byte[1024];int len;while((len=fis.read(bytes))!=-1){//把读到的内容写入新文件中fos.write(bytes,0,len);}//释放资源fis.close();fos.close();
File srcFile = new File("G:\\dad\\hb99.jpg");
File destDir = new File("G:\\dada");
//在destDir下创建一个和srcFile同名的文件
File destFile = new File(destDir,srcFile.getName());
FileInputStream fis = new FileInputStream(srcFile);
FileOutputStream fos = new FileOutputStream(destFile);byte[] bytes = new byte[1024];
int len;
while((len=fis.read(bytes))!=-1){//把读到的内容写入新文件中fos.write(bytes,0,len);
}
//释放资源
fis.close();
fos.close();

5.2 文件夹复制

要求定义一个方法,该方法能够实现文件夹的复制

public class Test09 {public static void main(String[] args) throws IOException {//要求定义一个方法,该方法能够实现文件夹的复制,考虑有子文件夹的情况File srcDir = new File("C:\\Users\\root\\Desktop\\test");File dest = new File("C:\\Users\\root\\Desktop\\test2");copyDir(srcDir,dest);}//File srcDir  源文件夹//File dest要复制到哪个目录public static void copyDir(File srcDir,File dest ) throws IOException {if(!(srcDir.exists()&&srcDir.isDirectory())){throw new RuntimeException("源文件夹必须存在并且是一个文件夹");}if(!dest.isDirectory()){throw new RuntimeException("目标文件夹必须是一个文件夹");}//1.在目标文件夹下创建一个和源文件夹同名的文件夹destDirFile destDir = new File(dest,srcDir.getName());destDir.mkdirs();//2.获取源文件夹下的所有子文件File[] files = srcDir.listFiles();if(files!=null){//3.遍历数组,复制每一个文件到目标文件夹destDir下for (File file : files) {if(file.isFile()){copyFile(file,destDir);}else {//复制文件夹copyDir(file,destDir);}}}}//源文件的路径  File srcFile//目标文件的存放目录路径  File destDirpublic static void copyFile(File srcFile,File destDir) throws IOException {//在destDir下创建一个和srcFile同名的文件File destFile = new File(destDir,srcFile.getName());//读取源文件  把读到的数据写入目标文件destFileFileInputStream fis = new FileInputStream(srcFile);FileOutputStream fos = new FileOutputStream(destFile);byte[] bytes = new byte[1024];int len;while((len=fis.read(bytes))!=-1){//把读到的内容写入新文件中fos.write(bytes,0,len);}//释放资源fis.close();fos.close();}
}

public class Test09 {
    public static void main(String[] args) throws IOException {
        //要求定义一个方法,该方法能够实现文件夹的复制,考虑有子文件夹的情况
        File srcDir = new File("C:\\Users\\root\\Desktop\\test");
        File dest = new File("C:\\Users\\root\\Desktop\\test2");
        copyDir(srcDir,dest);
    }

//File srcDir  源文件夹
    //File dest要复制到哪个目录
    public static void copyDir(File srcDir,File dest ) throws IOException {
        if(!(srcDir.exists()&&srcDir.isDirectory())){
            throw new RuntimeException("源文件夹必须存在并且是一个文件夹");
        }
        if(!dest.isDirectory()){
            throw new RuntimeException("目标文件夹必须是一个文件夹");
        }
        //1.在目标文件夹下创建一个和源文件夹同名的文件夹destDir
        File destDir = new File(dest,srcDir.getName());
        destDir.mkdirs();
        //2.获取源文件夹下的所有子文件
        File[] files = srcDir.listFiles();
        if(files!=null){
            //3.遍历数组,复制每一个文件到目标文件夹destDir下
            for (File file : files) {
                if(file.isFile()){
                    copyFile(file,destDir);
                }else {
                    //复制文件夹
                    copyDir(file,destDir);
                }

}
        }

}

//源文件的路径  File srcFile
    //目标文件的存放目录路径  File destDir
    public static void copyFile(File srcFile,File destDir) throws IOException {
        //在destDir下创建一个和srcFile同名的文件
        File destFile = new File(destDir,srcFile.getName());
        //读取源文件  把读到的数据写入目标文件destFile
        FileInputStream fis = new FileInputStream(srcFile);
        FileOutputStream fos = new FileOutputStream(destFile);

byte[] bytes = new byte[1024];
        int len;
        while((len=fis.read(bytes))!=-1){
            //把读到的内容写入新文件中
            fos.write(bytes,0,len);
        }
        //释放资源
        fis.close();
        fos.close();

}
}

文件之间的拷贝(拷贝图片实例)java.io.FileNotFoundException: G:\dad (拒绝访问。)通过绝对路径获取各种文件名相关推荐

  1. java文件夹拒绝访问-java.io.FileNotFoundException: .\xx\xx (拒绝访问。)

    1问题描述 今天做项目时创建一个文件(图片路径)在然创建的时候报异常java.io.FileNotFoundException: .\xx\xx (拒绝访问.) 2问题展示 java.io.FileN ...

  2. Exception in thread “main“ java.io.FileNotFoundException: C:\Temp (拒绝访问。)

    Exception in thread "main" java.io.FileNotFoundException: C:\Temp (拒绝访问.)     at java.io.F ...

  3. java.io.FileNotFoundException: D:\hadoop (拒绝访问。)

    ps: 如果有任何问题可以评论留言,我看到后会及时解答,评论或关注,您的鼓励是我分享的最大动力 转载请注明出处: https://blog.csdn.net/qq_40938301/article/d ...

  4. java基础—java.io.FileNotFoundException: D:\\AAA(拒绝访问。)

    今天在遍历文件夹里面的文件,并读取文件中的内容的时候. 使用 FileReader f = new FileReader(new File("D:\\AAA")); 出现了这个ja ...

  5. Java文件下载FileNotFoundException: G:\xxx 拒绝访问异常

    这个异常起始就是没有指定下载文件 既然想要下载东西,就必须告诉程序你想要下载那个文件,不然程序无法知道,到了指定文件夹之后你要下载那个文件.以jspSmartUpload下载文件的正确的写法如下: s ...

  6. 记录一个bug 关于 java 解压缩包 写入本地报错 :java.io.FileNotFoundException(系统找不到指定的路径。)

    记录一个bug 关于 java 解压缩包 写入本地报错 :java.io.FileNotFoundException(系统找不到指定的路径.) 第一想法是路径问题: 1.检查文件路径是否正确 2.另外 ...

  7. Java如何获取IP属地 ip2region failed to create searcher with x:java.io.FileNotFoundException:( 系统找不到指定的路径)

    目录 引言 获取ip地址 使用ServerHttpRequest获取ip完整地址: 获取ip地址的源代码 接口调用源代码 使用HttpServletRequest获取ip完整地址 获取ip地址的源代码 ...

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

    01-错误信息: Exception in thread "main" java.io.FileNotFoundException: e:b (拒绝访问.)at java.io.F ...

  9. java.io.FileNotFoundException:(拒接访问)

    一.问题 在使用FileInputStream或FileOutputStream时会遇到如下问题1和问题2. 问题1: java.io.FileNotFoundException: .\xxx\xxx ...

最新文章

  1. SQLSERVER系统数据库工作原理
  2. gradle常用命令
  3. android file 创建时间,获得文件的创建时间(精确到时分秒)
  4. Leetcode 25 K个一组翻转链表 (每日一题 20210719)
  5. 2021暑假每日一题 【week8 完结】
  6. 聊聊如何设计千万级吞吐量的.Net Core网络通信!
  7. 数据查询和业务流分开_TiDB HTAP 助力小红书业务升级
  8. 彩色手绘元宵节插画风素材图片
  9. Spring Boot学习总结(14)——Spring Boot常见面试题汇总
  10. ASP.NET CORE的H5上传
  11. 京东运营 不错的帖子
  12. 【SLAM学习笔记4】卡方检验chi-square
  13. HTTP状态码429的含义
  14. Nodejs如何连接Mysql
  15. 研二导师画大饼,不给时间实习,咋办
  16. 百度地图点聚合优化重写
  17. 漫谈程序员系列:谁是为加班而生的
  18. 对良/恶性肿瘤的预测python代码实现
  19. PAT 乙级练习 1050 螺旋矩阵 - 超级详细的思路讲解
  20. 土地利用情况分析(土地利用指数)

热门文章

  1. fastadmin html完整版,FastAdmin完整版(后台开发软件)
  2. 最短路算法 算法 python实现
  3. 远程服务器ip地址可以更改吗,Web远程管理服务器端的IP地址如何进行设置?
  4. JavaScript - 专题 - 彻底搞懂parseInt
  5. 清华姚班90后学霸、MIT博士吴佳俊即将加入斯坦福任助理教授
  6. python中int函数如何取整
  7. idea修改git提交地址
  8. 抓狂的QQ离开自动回复
  9. GIT学习0基础到入门(附图)
  10. 收集的一些学习SharePoint 2007的网站