1. 程式人生 > >FIle類常用工具方法整理(持續更新)

FIle類常用工具方法整理(持續更新)

urn mef iou all filepath tabs ipa comm NPU

1.遞歸遍歷一個目錄,獲取所有文件名(也可以取到絕對路徑)

public static void traverse(String filePath, List<String> files) {
        if (StringUtils.isBlank(filePath)){
            return ;
        }
        try{
            File superFile = new File(filePath);
            if (superFile.exists()) {
                File[] fileList 
= superFile.listFiles(); if (null != files && fileList.length > 0) { for (File file : fileList) { // 還是文件夾 if (file.isDirectory()) { traverse(file.getAbsolutePath(),files); }
else { files.add(file.getName()); //文件名 //files.add(file.getAbsolutePath()); //文件絕對路徑 } } } } }catch (Exception e){ //log } return
; }

2.獲取文件大小,自動用K、M、G表示。

public static String parseSize(long length){
        StringBuilder size = new StringBuilder();
        DecimalFormat format = new DecimalFormat("###.0");
        if (length < 1024) {
            size.append((int) length).append(" B");
        }else if (length >= 1024 && length < 1024 * 1024) {
            double i = (length / (1024.0));
            size.append(format.format(i)).append(" K");
        }else if (length >= 1024 * 1024 && length < 1024 * 1024 * 1024) {
            double i = (length / (1024.0 * 1024.0));
            size.append(format.format(i)).append(" M");
        }else if (length >= 1024 * 1024 * 1024) {
            double i = (length / (1024.0 * 1024.0 * 1024.0));
            size.append(format.format(i)).append(" G");
        }
        return size.toString();
    }

3.Multipart文件轉存為本地的File。

public static void multipartToFile(MultipartFile file, String fileFolder){
        FileOutputStream outputStream = null;
        try {
            File newFileFolder = new File(fileFolder);
            if (!newFileFolder.exists()) {
                newFileFolder.mkdirs();
            }

            fileFolder = newFileFolder.getAbsolutePath() + File.separator + file.getOriginalFilename();

            outputStream = new FileOutputStream(new File(fileFolder));
            IOUtils.copy(file.getInputStream(), outputStream);
        } catch (Exception e) {
            // log
        } finally {
            IOUtils.closeQuietly(outputStream);
        }
    }

4.清理指定目錄下一天前(時間可以指定)的文件。

public static void cleanDirectory(String dir, long ttl) {
        File file = new File(dir);
        String[] subDirNames = file.list(new FilenameFilter() {
            @Override
            public boolean accept(File current, String name) {
                return new File(current, name).isDirectory();
            }
        });
        if (subDirNames != null) {
            for (String name : subDirNames) {
                File subDir = new File(dir + File.separator + name);
                if (System.currentTimeMillis() - subDir.lastModified() > ttl) {
                    try {
                        FileUtils.deleteDirectory(subDir);  //import org.apache.commons.io.FileUtils;
                    } catch (Exception e) {
                        // log
                    }
                }
            }
        }
    }

5.把字符串存入指定文件。

public static void strToFile(String content, File outFile) {
        OutputStream os = null;
        try {
            File parent = outFile.getParentFile();
            if (!parent.exists()) {
                parent.mkdirs();
            }
            if (!outFile.exists()) {
                outFile.createNewFile();
            }
            os = new FileOutputStream(outFile);
            IOUtils.write(content, os);
        } catch (Exception e) {
            // log
        } finally {
            IOUtils.closeQuietly(os);
        }
    }

FIle類常用工具方法整理(持續更新)