Java 在某个文件夹下 创建多个文件

最近因为项目的问题 需要新建很多个文件和文件夹,手动创建实在是太麻烦了.就写了一个创建文件的方法.
主要的思路就是把文件名放到数组里,然后遍历出来.代码有点粗糙.

package com.emp.io;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 * @author employeeeee
 * @date 2018/9/12 9:06
 */
public class FileCreate {
    //创建文件夹
    private static String CreateFile(String dir) {


        File file = new File(dir);
        if (!file.exists()) {
            //创建文件夹
            boolean mkdir = file.mkdir();
            System.out.println("文件夹已创建");
        } else {
            System.out.println("文件夹已存在");
        }
        return dir;
    }

    public static void main(String[] args) {



        /*String fileName = "Form Control-Editor-表格控件Editor.MD";*/
        //定义数组存放文件名
        String[] fileName = {"10.Form Control - GanttChart - 表格控制 - 甘特图.MD",
                "11.Form Control - GroupBox - 表单控件 - GroupBox.MD",
                "12.Form Control - HTML Control - 表单控件 - HTML控件.MD",
                "13.Form Control - JMS Message Control - 表单控件 - JMS消息控件.MD",
                "14. Form Control - Label -   表格控制 - 标签.MD",
                "15.Form Control - ListBox - 表单控件 - ListBox.MD",
                "16. Form Control - Live Data -   表格控制 - 实时数据.MD",
                "17. Form Control - Live Data Browser -   表单控件 - 实时数据浏览器.MD",
                "18.Form Control - MaskEdit - 表单控件 - MaskEdit.MD",
                "19. Form Control - MessageGroup -   表单控件 - MessageGroup.MD",
                "20.Form Control - Panel - 表格控制 - 面板.MD",
                "21.Form Control - Picture Box - 表格控制 - 图片框.MD",
                "22. Form Control - ProgressBar -   表单控件 - ProgressBar.MD",
                "23. Form Control - Radio Button -   表格控制 - 单选按钮.MD",
                "24.Form Control - ReportViewer - 表单控件 - ReportViewer.MD",
                "25.Form Control - Resource Route Editor - 表单控件 - 资源路径编辑器.MD",
                "26.Form Control - RoundButton - 表单控件 - RoundButton.MD",
                "27.Form Control - Route Editor - 表单控件 - 路径编辑器.MD",
                "28.Form Control - Scale - 形式控制 - 规模.MD",
                "29.Form Control - ScrollPane - 表单控件 - ScrollPane.MD",
                "30.Form Control - Serial Port - 表格控制 - 串口.MD",
                "31.Form Control - Socket - 表格控制 - 插座.MD",
                "32.Form Control - Socket2 - 表格控制 - Socket2.MD",
                "33.Form Control - SplitPane - 表格控制 - SplitPane.MD",
                "34.Form Control - TaskPane - 表单控件 - TaskPane.MD",
                "35.Form Control - Timer - 表格控制 - 计时器.MD",
                "36. Form Control - TreeView -   表单控件 - TreeView.MD",
                "37.Form Control - Wafermap - 表格控制 - Wafermap.MD",
                "38.Form Control - WorkScheduleControl - 表单控件 - WorkScheduleControl.MD"
        };
        //创建文件夹
        String dir = "C:\\1";
        CreateFile(dir);
        //遍历输出
        for (String aFileName : fileName) {


            String dirName = dir + "\\" + aFileName;
            File file = new File(dirName);
            if (file.exists()) {
                System.out.println("创建失败有相同文件");
            } else {
                try {

                    BufferedWriter bw = new BufferedWriter(new FileWriter(dirName));
                   //这个位置可以添加在文件中要写的内容
                    System.out.println(aFileName + "文件创建成功");
                } catch (IOException e) {
                    e.printStackTrace();
                    System.out.println("创建文件失败!" + e.getMessage());
                }
            }


        }
    }


}

你可能感兴趣的:(Java 在某个文件夹下 创建多个文件)