【工具】往制定路径写入指定文件

在公司做事的时候突然有这样一个需求,就是对项目中每个含有java文件的包,添加一个package.html文件。

我们公司的项目是以微服务为主,几个微服务下来,不知道有多少包,于是就随手写了一个小工具,作用是往指定路径添加指定文件。

代码如下

public class FileUtils
{
    private static String index = ".java";
    
    private static String path = "D:\\...";
    
    private static String srcPath = "D:\\...\\package.html";
    
    public static void main(String[] args)
    {
        
        List<String> javaPathList = new ArrayList<String>();
        javaPathList = checkFile(path, index, javaPathList);
        if (javaPathList == null || javaPathList.size() == 0)
        {
            System.out.println("改路径下不存在Java文件");
        }
        else
        {
            for (String str : javaPathList)
            {
                File desFile = new File(str);
                String desPath = desFile.getParent();
                try
                {
                    writeFile(desPath, srcPath);
                }
                catch (Exception e)
                {
                    // TODO: handle exception
                    e.printStackTrace();
                }
            }
        }
        
    }
    
    /**
    * 检查路径下是否有结尾为index的文件
    * 并返回文件名列表
    * <功能详细描述>
    * @param path 文件路径
    * @param index 结尾标志
    * @param list 结果列表
    * @return 结果列表
    * @see [类、类#方法、类#成员]
    */
    public static List<String> checkFile(String path, String index, List<String> list)
    {
        File file = new File(path);
        if (!file.exists())
        {
            System.out.println("path not exist");
            return list;
        }
        File[] files = file.listFiles();
        if (files == null || files.length == 0)
        {
            System.out.println("no file in path");
            return list;
        }
        for (File file2 : files)
        {
            if (file2.isFile() && file2.getName().endsWith(index)) //判断文件名是否包含.java
            {
                list.add(file2.getPath());
            }
            if (file2.isDirectory())
            { //是路径
                checkFile(file2.getPath(), index, list);
            }
        }
        return list;
    }
    
    /**
     * 往指定path路径添加file文件
     * <功能详细描述>
     * @param path
     * @param file
     * @return
     * @throws IOException 
     * @see [类、类#方法、类#成员]
     */
    public static void writeFile(String desPath, String srcPath)
    {
        File srcFile = new File(srcPath);
        if (!srcFile.exists())
        {
            System.out.println("src file not exists");
            return;
        }
        
        File pathFile = new File(desPath);
        if (!pathFile.isDirectory())
        {//如果不是路径
            System.out.println("srcPath is not a path");
            return;
        }
        
        File desFile = new File(desPath + "\\" + srcFile.getName()); //创建文件
        if (!desFile.exists())
        {
            try
            {
                desFile.createNewFile();
            }
            catch (IOException e)
            {
                // TODO: handle exception
                System.out.println("create new file in desPath failed");
                e.printStackTrace();
            }
            
        }
        else
        {
            System.out.println("desPath exist file");
            return;
        }
        
        FileReader fileReader;
        FileWriter fileWriter;
        try
        {
            fileReader = new FileReader(srcFile);
            fileWriter = new FileWriter(desFile);
            BufferedReader bufferedReader = new BufferedReader(fileReader);
            BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
            String str = null;
            while ((str = bufferedReader.readLine()) != null)
            {
                bufferedWriter.write(str);
                bufferedWriter.newLine();
                bufferedWriter.flush();
            }
            System.out.println("write file successfully");
        }
        catch (Exception e)
        {
            // TODO: handle exception
            System.out.println("write file failed");
            e.printStackTrace();
        }
        
    }
    
}
 

 

posted on 2017-12-10 16:53  猫咪大王  阅读(577)  评论(0编辑  收藏  举报