Java 修改文件名

/ 语言 / 没有评论 / 1174浏览
        public static void main(String[] args) throws Exception {



            String  oldPath =  "/Users/admin/test/jmeter/123.log";
            String  newPath =  "/Users/admin/test/jmeter/123/qweqweqweq.log";

            //创建指定的路径
            File directory = new File(newPath);
            //获取文件夹 路径
            String courseFile = directory.getParent();
            File file = new File(courseFile );//
            if(!file.exists()){//如果文件夹不存在
                file.mkdir();//创建文件夹
            }
            File oldFile = new File(oldPath );
            File newFile = new File(newPath );
            //重命名
            System.out.println(oldFile.renameTo(newFile));
            
          
        }