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


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

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

    2.code

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

    1. package day01;
    2. import java.io.*;
    3. import java.nio.channels.FileChannel;
    4. import java.nio.file.Files;
    5. public class 文件拷贝 {
    6. public static void main(String[] args) throws IOException {
    7. String s1="G:\\dad\\hb.jpg";
    8. String s2="G:\\dada\\12.png";
    9. File file1 = new File(s1);
    10. //获取文件名称
    11. String name = file1.getName();
    12. System.out.println(name);
    13. File file2 = new File(s2);
    14. copyFileByChannel(file1,file2);
    15. }
    16. public static void copyFileByChannel(File file, File fileTo) throws IOException {
    17. FileChannel fileChannel = new FileInputStream(file).getChannel();
    18. FileChannel fileChannelTo = new FileOutputStream(fileTo).getChannel();
    19. for (long count = fileChannel.size(); count > 0; ) {
    20. long transferred = fileChannel.transferTo(fileChannel.position(), count, fileChannelTo);
    21. count -= transferred;
    22. }
    23. }
    24. public static void copyFileByStream(File file, File fileTo) throws IOException {
    25. InputStream in = new FileInputStream(file);
    26. OutputStream out = new FileOutputStream(fileTo);
    27. byte[] buffer = new byte[1024];
    28. int length;
    29. while ((length = in.read(buffer)) > 0) {
    30. out.write(buffer, 0, length);
    31. }
    32. }
    33. }
    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);
    
    }

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

    1. File srcFile = new File("G:\\dad\\hb99.jpg");
    2. File destDir = new File("G:\\dada");
    3. //在destDir下创建一个和srcFile同名的文件
    4. File destFile = new File(destDir,srcFile.getName());
    5. FileInputStream fis = new FileInputStream(srcFile);
    6. FileOutputStream fos = new FileOutputStream(destFile);
    7. byte[] bytes = new byte[1024];
    8. int len;
    9. while((len=fis.read(bytes))!=-1){
    10. //把读到的内容写入新文件中
    11. fos.write(bytes,0,len);
    12. }
    13. //释放资源
    14. fis.close();
    15. 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 文件夹复制

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

    1. public class Test09 {
    2. public static void main(String[] args) throws IOException {
    3. //要求定义一个方法,该方法能够实现文件夹的复制,考虑有子文件夹的情况
    4. File srcDir = new File("C:\\Users\\root\\Desktop\\test");
    5. File dest = new File("C:\\Users\\root\\Desktop\\test2");
    6. copyDir(srcDir,dest);
    7. }
    8. //File srcDir 源文件夹
    9. //File dest要复制到哪个目录
    10. public static void copyDir(File srcDir,File dest ) throws IOException {
    11. if(!(srcDir.exists()&&srcDir.isDirectory())){
    12. throw new RuntimeException("源文件夹必须存在并且是一个文件夹");
    13. }
    14. if(!dest.isDirectory()){
    15. throw new RuntimeException("目标文件夹必须是一个文件夹");
    16. }
    17. //1.在目标文件夹下创建一个和源文件夹同名的文件夹destDir
    18. File destDir = new File(dest,srcDir.getName());
    19. destDir.mkdirs();
    20. //2.获取源文件夹下的所有子文件
    21. File[] files = srcDir.listFiles();
    22. if(files!=null){
    23. //3.遍历数组,复制每一个文件到目标文件夹destDir下
    24. for (File file : files) {
    25. if(file.isFile()){
    26. copyFile(file,destDir);
    27. }else {
    28. //复制文件夹
    29. copyDir(file,destDir);
    30. }
    31. }
    32. }
    33. }
    34. //源文件的路径 File srcFile
    35. //目标文件的存放目录路径 File destDir
    36. public static void copyFile(File srcFile,File destDir) throws IOException {
    37. //在destDir下创建一个和srcFile同名的文件
    38. File destFile = new File(destDir,srcFile.getName());
    39. //读取源文件 把读到的数据写入目标文件destFile
    40. FileInputStream fis = new FileInputStream(srcFile);
    41. FileOutputStream fos = new FileOutputStream(destFile);
    42. byte[] bytes = new byte[1024];
    43. int len;
    44. while((len=fis.read(bytes))!=-1){
    45. //把读到的内容写入新文件中
    46. fos.write(bytes,0,len);
    47. }
    48. //释放资源
    49. fis.close();
    50. fos.close();
    51. }
    52. }

    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();

        }
    }
     

  • 相关阅读:
    linux 线程同步
    SSD目标检测网络模型详细介绍
    Worthington公司氨基酸氧化酶,L-的特异性分析
    使用Python和BeautifulSoup提取网页数据的实用技巧
    CSS笔记——css阿里图标、iframe内联框架
    【Mysql】join连接条件有重复的数据的情况
    python面试题总结(四)
    Vue中如何为id绑定内联计算属性
    adb删除系统应用
    amv是什么文件格式?如何播放amv视频?
  • 原文地址:https://blog.csdn.net/weixin_61503139/article/details/127889411