文件之间的拷贝(拷贝图片实例)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博客

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.在目标文件夹下创建一个和源文件夹同名的文件夹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();

    }
}

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

    }
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值