JavaIO(四):文件操作

Author Avatar
罗炜光 4月 30, 2016
  • 在其它设备中阅读本文章

新建

    /**
     * 新建文件
     * @param path 绝对路径
     * @return 新建成功返回ture,新建失败或以存在此文件返回false
     */
    public static boolean newFile(String path)
    {
        File file = new File(path);
        file.getParentFile().mkdirs();
        try
        {
            return file.createNewFile();
        } catch (IOException e)
        {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 新建文件夹
     * @param path 绝对路径
     * @return 新建成功返回ture,新建失败或以存在此文件夹返回false
     */
    public static boolean newFolder(String path)
    {
        File file = new File(path);
        return file.mkdirs();
    }

重命名

    /**
     * 文件重命名
     * @param path 绝对路径
     * @param newname 新名称
     * @return 文件重命名 成功返回ture,失败或不存在此文件返回false
     */
    public static boolean renameTo(String path,String newname)
    {
        File file = new File(path);
        if(file.renameTo(new File(file.getParent(), newname)))
        {
            return true;
        }
        return false;
    }

删除

    /**
     * 删除文件或文件夹
     * @param path 绝对路径
     * @return 文件不存在或以删除返回ture
     */
    public static boolean delete(String path)
    {
        File file = new File(path);
        if(!file.exists())
        {
            return true;
        }
        if(file.isDirectory())
        {
            String[] filelists = file.list();
            for(String childfile : filelists)
            {
                delete(path + File.separator + childfile);
            }
        }
        return file.delete();
    }

列表

    public static List<String> filelists(String path)
    {
        return filelists(path,false);
    }

    public static List<String> filelists(String path,boolean isSearchChildList)
    {
        return filelists(path, isSearchChildList, false);
    }

    public static List<String> filelists(String path,boolean isSearchChildList,boolean isAddFolder)
    {
        return filelists(path, isSearchChildList,isAddFolder, null);
    }

    /**
     * 列出目录下及其子目录下所有文件名含有fileFilrer字符数组中的文件,但不包含文件夹
     * @param path 绝对路径
     * @param isSearchChildList 是否搜索子目录
     * @param isAddFolder 是否添加文件夹
     * @param fileFilter 过滤字符数组
     * @return 返回的为绝对路径
     */
    public static List<String> filelists(String path,boolean isSearchChildList,boolean isAddFolder,String[] fileFilter)
    {
        List<String> list = new ArrayList<String>();
        File file = new File(path);
        String[] filelists = file.list(new FilenameFilter()
        {
            @Override
            public boolean accept(File dir, String name)
            {
                if(fileFilter == null)
                {
                    return true;
                }

                for(String filterStr: fileFilter)
                {
                    if(name.contains(filterStr))
                    {
                        return true;
                    }
                }
                return false;
            }
        });
        for(String str: filelists)
        {
            if(new File(path,str).isDirectory())
            {
                if(isAddFolder)
                    list.add(path + File.separator + str);

                if(isSearchChildList)
                {
                    List<String> childLists= filelists(path + File.separator + str,isSearchChildList,isAddFolder,fileFilter);
                    list.addAll(childLists);
                }

            }
            else 
            {
                list.add(path + File.separator + str);
            }
        }
        return list;
    }

复制

public static boolean copy(String path,String newfilepath) throws IOException
    {
        //128KB
        return copy(path,newfilepath,false);
    }


    public static boolean copy(String path,String newfilepath,boolean overlay) throws IOException
    {
        //128KB
        return copy(path,newfilepath,overlay,131072);
    }




    /**
     * 复制文件
     * @param path 绝对路径
     * @param newfilepath 绝对路径
     * @param overlay 是否进行覆盖
     * @param length 复制缓冲区的大小
     * @return 复制文件成功返回ture,未成功返回false
     * @throws IOException IOException 输入输出流关闭异常
     */
    public static boolean copy(String path,String newfilepath,boolean overlay,int length) throws IOException
    {
        int bufferlength = length;
        if(newfilepath.startsWith(path))
        {
            return false;
        }
        File file = new File(path);
        File newfile = new File(newfilepath);
        if(!file.exists())
        {
            return false;
        }
        if(newfile.exists())
        {
            if(overlay)
            {
                if(!file.isDirectory())
                    newfile.delete();
            }
            else 
            {
                return false;
            }
        }
        if(file.isDirectory())
        {
            newfile.mkdirs();
            String[] filelists = file.list();
            for(String childfile : filelists)
            {
                copy(path + File.separator +childfile, newfilepath + File.separator +childfile,overlay,length);
            }
            return true;
        }
        else 
        {
            newfile.getParentFile().mkdirs();
            InputStream inputStream = null;  
            OutputStream outputStream = null;  
            try {  
                inputStream = new BufferedInputStream(new FileInputStream(file));  
                outputStream = new BufferedOutputStream(new FileOutputStream(newfile));  
                byte[] buffer = new byte[bufferlength];  
                int i;  
                while ((i = inputStream.read(buffer)) != -1) 
                {  
                    outputStream.write(buffer, 0, i);  
                }
                return true;
            }  
            catch (Exception e) {  
                e.printStackTrace();  
                return false;
            } finally {  
                inputStream.close();;  
                outputStream.close();;  
            }  
        }
    }

大小

/**
     * 文件或文件夹大小
     * @param path 绝对路径
     * @return 单位为byte
     */
    public static long fileSizes(String path)
    {
        long fileSizes = 0;
        File file = new File(path);
        if(!file.exists())
        {
            return 0;
        }
        if(file.isDirectory())
        {
            String[] filelists = file.list();
            for(String childfile : filelists)
            {
                fileSizes += fileSizes(path + File.separator + childfile);
            }
        }
        return file.length() + fileSizes;
    }