自编工具总是临时抱佛脚来得顺溜,宜常备手边以提高工作效率:

package com.hy;

import java.io.File;

/**
 * 日常工作常用的一些工具方法
 * @author 逆火
 *
 */
public class CommonTools {
    /**
     * 列出某目录下文件名,在控制台输出
     * @param dir 目录
     * @param extension 文件扩展名,*或all代表全部
     */
    public static void listFilesUnderDir(String dir,String extension) {
        File folder = new File(dir);
        File[] files = folder.listFiles();
        for (int i = 0; i < files.length; i++) {
            if (files[i].isFile()) {
                if(extension.equals("*")||extension.equals("all")) {
                    System.out.println(files[i].getName());
                }else if(files[i].getName().endsWith(extension)) {
                    System.out.println(files[i].getName());
                }
            }
        }
    }

    /**
     * 将一行字符串劈分成数组形式
     * 输入:splitString2Array("qwwq,ew,ew,ew,ew,wwq,wq,121,112",",");
     * 输出:String[] arr={"qwwq","ew","ew","ew","ew","wwq","wq","121","112"};
     * @param line
     */
    public static void splitString2Array(String line,String seperator) {
        String[] arr=line.split(seperator);

        for(int i=0;i<arr.length;i++) {
            arr[i]="\""+arr[i]+"\"";
        }

        System.out.println("String[] arr={"+String.join(",",arr)+"};");
    }

    /**
     * 将一串文字变成竖向
     * @param line
     * @param seperator
     */
    public static void verticalize(String line,String seperator) {
        String[] arr=line.split(seperator);
        System.out.println(String.join("\n",arr));
    }

    public static void main(String[] args) {
        listFilesUnderDir("D:\\sysbackup\\","GHO");
        splitString2Array("北京,杭州,南京,扬州,苏州,无锡,扬中,泰州,镇江",",");
        verticalize("北京,杭州,南京,扬州,苏州,无锡,扬中,泰州,镇江",",");
    }
}

输出:

t440pwin10JavaOffice_190907.GHO
t440pwin10Java_190906.GHO
t440pwin10only_190906.GHO
win10dev190927.GHO
String[] arr={"北京","杭州","南京","扬州","苏州","无锡","扬中","泰州","镇江"};
北京
杭州
南京
扬州
苏州
无锡
扬中
泰州
镇江
/**
     * 读文件,排序,输出
     * @param filePathname
     */
    public static void readSortPrint(String filePathname) {
        try {
            List<String> list=new ArrayList<String>();
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(filePathname), "UTF-8"));
            String line = null;
            while( ( line = br.readLine() ) != null )
                list.add(line);
            br.close();

            Collections.sort(list);

            for(String str:list) {
                System.out.println(str);
            }

        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

--未完待续--

05-11 05:03