文件之间的拷贝(拷贝图片实例)java.io.FileNotFoundException: G:\dad (拒绝访问。)通过绝对路径获取各种文件名
创始人
2024-01-28 01:25:20
0

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

    }
}
 

相关内容

热门资讯

新型中小投资项目 5个项目 新... 提起适合穷人的18个创业项目小投资的都有哪些,想必大家都有一定了解,有人问请问现在新型投资项目有哪些...
学生自主创业 学生自主创业 学... 以下是西安领军教育集团董事长吕鹏程在2012腾讯大秦网教育论坛主题演讲:学生自主创业不能说的秘密实录...
一个合适的创业合作伙伴很重要 ... 找到一个合适的创业合作伙伴那样的话,成功率就会大很多,可是要如何才能找到一个好的合作伙伴呢?下面由小...
学生自主创业项目 学生自主创业...   :保健面包房开一家面包店投资不大,对很多人来说是件容易的事。但用传统的烤制方式再加上传统的配料,...
深圳创业项目 深圳创业项目 深... 深圳市一个人杰地灵的地方,南方城市的标杆,很多年轻人都向往的城市,有挑战,更有机会,从数据上看深圳的...
农林致富好项目有哪些 这四个农... 农林致富好项目有哪些?现在大家都有一个思想,尤其是在农村不少人都想自己创业赚钱,因为毕竟在农村创业优...
代理小本创业新开店项目 代理小...   创业开店项目小本创业项目推荐:纺吧,休闲娱乐的新时尚1在城市繁华居民区里或在游乐休闲集聚地租一套...
浙江福建等地积极应对台风“丹娜...   央视网消息(新闻联播):7月7日,中央气象台发布台风蓝色预警。浙江、福建等地加强防范,积极应对将...
怎么创业 怎么创业 怎么创业呢... 一无所有的人怎么创业?分类:创业故事|如何创业|Word文档下载一无所有的人怎么创业?创业,先从认识...
你们是怎么创业的? 你们是怎么... 女朋友在毕业后的第1年内因为忍受不了事业单位的不良风气决定离职。同年,我因为忍受不了所任职私企的不良...