• Java File类的操作


    题目描述

    A文件夹下面有多个子文件夹,然后子文件下后面有一些.jpg文件,要求把这些.jpg文件完全拷贝复制到B文件夹。

    实现思路

    先遍历循环A文件夹下的文件,然后找到符合.jpg的文件,放到一个列表中,然后再把列表中的jpg文件放到B文件夹上。

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.nio.file.Files;
    import java.util.LinkedList;
    import java.util.List;
    import java.util.Queue;
    
    public class Picture {
        private static LinkedList<File> fileQueue= new LinkedList<>();
        private final String aPath="C:\Users\m088163\Pictures";
        private final String bPath="D:\temp";
    
        /**
         * 将源文件夹下所有的.jpg文件找到,并存在fileQueue 列表中。
         * @param path 需要查找文件的路径名
         * @param subStr 匹配的文件类型
         */
        public void FindJpg(String path,String subStr){
            File file=new File(path);
            File[] files=file.listFiles();
            for(int i=0;i<files.length;i++){
                if(files[i].isDirectory()){
                    FindJpg(files[i].getAbsolutePath(),subStr);
                }
                if(files[i].getAbsolutePath().contains(subStr)){
                    fileQueue.add(files[i]);
                }
            }
        }
    
        /**
         * 将fileQeueue中的jpg文件存在目标文件夹。
         * @param path
         * @throws Exception
         */
        public void moveJpg(String path) throws Exception{
            String myPath="";
            File newFile;
            for(File files:fileQueue){
                myPath=path+"\";
                myPath+=files.getName();
                System.out.println(files.getName());
                newFile=new File(myPath);
                if(newFile.exists()){
                    System.out.println("创建文件失败"+newFile+"失败,目标文件已经存在");
                }
                if(!newFile.getParentFile().exists()){
                    System.out.println("文件的根目录不存在,创建根目录");
                    newFile.getParentFile().mkdir();
                }
                copyfile(files.getAbsoluteFile(),newFile);
            }
        }
    
        /**
         * 写入文件操作
         * @param fromDest
         * @param toDest
         * @throws Exception
         */
        public void copyfile(File fromDest,File toDest ) throws Exception{
            FileInputStream is =new FileInputStream(fromDest);
            FileOutputStream os =new FileOutputStream(toDest);
            byte[] b =new byte[1024];
            int temp=0;
            while((temp=is.read(b))!=-1){
                os.write(b,0,temp);
            }
            is.close();
            os.close();
    
        }
        public static void main(String[] args) {
            Picture picture = new Picture();
            picture.FindJpg(picture.aPath, ".jpg");
            try {
                picture.moveJpg(picture.bPath);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.nio.file.Files;
    import java.util.LinkedList;
    import java.util.List;
    import java.util.Queue;
    
    public class Picture {
        private static LinkedList<File> fileQueue= new LinkedList<>();
        private final String aPath="C:\Users\m088163\Pictures";
        private final String bPath="D:\temp";
    
        /**
         * 将源文件夹下所有的.jpg文件找到,并存在fileQueue 列表中。
         * @param path 需要查找文件的路径名
         * @param subStr 匹配的文件类型
         */
        public void FindJpg(String path,String subStr){
            File file=new File(path);
            File[] files=file.listFiles();
            for(int i=0;i<files.length;i++){
                if(files[i].isDirectory()){
                    FindJpg(files[i].getAbsolutePath(),subStr);
                }
                if(files[i].getAbsolutePath().contains(subStr)){
                    fileQueue.add(files[i]);
                }
            }
        }
    
        /**
         * 将fileQeueue中的jpg文件存在目标文件夹。
         * @param path
         * @throws Exception
         */
        public void moveJpg(String path) throws Exception{
            String myPath="";
            File newFile;
            for(File files:fileQueue){
                myPath=path+"\";
                myPath+=files.getName();
                System.out.println(files.getName());
                newFile=new File(myPath);
                if(newFile.exists()){
                    System.out.println("创建文件失败"+newFile+"失败,目标文件已经存在");
                }
                if(!newFile.getParentFile().exists()){
                    System.out.println("文件的根目录不存在,创建根目录");
                    newFile.getParentFile().mkdir();
                }
                copyfile(files.getAbsoluteFile(),newFile);
            }
        }
    
        /**
         * 写入文件操作
         * @param fromDest
         * @param toDest
         * @throws Exception
         */
        public void copyfile(File fromDest,File toDest ) throws Exception{
            FileInputStream is =new FileInputStream(fromDest);
            FileOutputStream os =new FileOutputStream(toDest);
            byte[] b =new byte[1024];
            int temp=0;
            while((temp=is.read(b))!=-1){
                os.write(b,0,temp);
            }
            is.close();
            os.close();
    
        }
        public static void main(String[] args) {
            Picture picture = new Picture();
            picture.FindJpg(picture.aPath, ".jpg");
            try {
                picture.moveJpg(picture.bPath);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    
  • 相关阅读:
    navicat for mysql (本人亲测,真实有效)
    python 8 days
    python 19 days
    python 20 days
    python 21 days
    python 10 days
    python 9 days
    python 14 days
    python 15 days
    python 16 days
  • 原文地址:https://www.cnblogs.com/NaCl/p/10489070.html
Copyright © 2020-2023  润新知