zoukankan      html  css  js  c++  java
  • 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();
            }
        }
    }
    
    
  • 相关阅读:
    【故障处理】ORA-12162: TNS:net service name is incorrectly specified (转)
    android studio 编程中用到的快捷键
    java时间格式串
    android Error occurred during initialization of VM Could not reserve enough space for object heap Could not create the Java virtual machine.
    linux安装vmware
    x1c 2017 安装mint18的坑——grub2
    x1c2017 8G版 win linux的取舍纠结记录
    python的try finally (还真不简单)
    kafka+docker+python
    json文件不能有注释
  • 原文地址:https://www.cnblogs.com/NaCl/p/10489070.html
Copyright © 2011-2022 走看看