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


    在公司做事的时候突然有这样一个需求,就是对项目中每个含有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();
            }
            
        }
        
    }
     
  • 相关阅读:
    Ajax请求ashx 返回 json 格式数据常见问题
    netbeans 优化设置
    Java基础static的探究
    mybatis中:selectKey返回最近插入记录的id
    Java map 详解
    Statement和PreparedStatement的异同
    css3 @media支持ie8用respond.js 解决IE6~8的响应式布局问题
    apply和call
    兼容性问题:backgroud-size支持IE8浏览器的方法
    json对象和json字符串之间的转化
  • 原文地址:https://www.cnblogs.com/cuglkb/p/7903118.html
Copyright © 2020-2023  润新知