zoukankan      html  css  js  c++  java
  • 【工具】往制定路径写入指定文件

    在公司做事的时候突然有这样一个需求,就是对项目中每个含有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();
            }
            
        }
        
    }
     
  • 相关阅读:
    沙盒解决方案与场解决方案之间的差异
    Windows 7:77 个 Windows 7 提示
    SharePoint disable loopback check
    SharePoint 2010 工作流解决方案:序言
    SharePoint 2010 查看“运行时错误”
    sharepoint 链接库链接在新窗口打开
    如何启用SharePoint 2010的代码块
    沙盒解决方案注意事项
    ie8.0 不能用document.all兼容IE7模式
    php导出数据到excel,防止身份证等数字字符格式变成科学计数的方法
  • 原文地址:https://www.cnblogs.com/cuglkb/p/7903118.html
Copyright © 2011-2022 走看看