java指定路径下创建多个文件夹和文件

  • Post author:
  • Post category:java


java指定路径下创建多个文件夹和文件

package IO;

import

java.io

.*;

import java.text.DecimalFormat;

public class FolderDemo {


public static void main(String[] args) throws IOException {


FolderDemo folderDemo = new FolderDemo();

folderDemo.deleteFolder(“E:\NewFolder”);

folderDemo.newFolder();

}

public void newFolder() throws IOException {


DecimalFormat df = new DecimalFormat(“0”);

File file = new File(“E:\NewFolder”);

boolean b = file.exists();

if (b) {


System.out.println(“文件夹已创建”);

} else {


file.mkdirs();

}

boolean button = true;

while (button) {

        for (int i = 1; i <= 10; i++) {
            File f = new File("E:\\NewFolder\\" + "Folder--" + df.format(i));
            boolean isFile2 = f.mkdirs();
            if (isFile2) {
                System.out.println("文件夹创建成功");
            } else {
                System.out.println("文件夹创建失败");
            }
            String folderName = f.getName();
            for (int a = 1; a <= 10; a++) {
                File folderFile = new File(file+ folderName + "\\" + "Folder-File--" + df.format(a));
                boolean c = folderFile.createNewFile();
                if (c) {
                    System.out.println("文件创建成功");
                } else {
                    System.out.println("文件创建失败");
                }
                String filename = folderFile.getName();
                FileOutputStream fileOutputStream = new FileOutputStream(file + folderName + "\\" + filename);
                OutputStreamWriter osw = new OutputStreamWriter(fileOutputStream, "GBK");
                BufferedWriter bufferedWriter = new BufferedWriter(osw);
                for (int x = 1; x <= 10; x++) {
                    bufferedWriter.write(filename+"\n");
                }
                bufferedWriter.close();

            }
            if (i == 10) {
                button = false;
            }
        }
    }
}
public static void deleteFolder(String folderPath) {
    File file = new File(folderPath);
    try {
        if (file.isDirectory()) {
            File[] subFiles = file.listFiles();
            if (subFiles != null && subFiles.length > 0) {
                for (int i = 0; i < subFiles.length; i++) {
                    deleteFolder(subFiles[i].getPath());
                }
            }
        }
        if (file.delete()) {
        } else {
            System.out.println("删除失败!");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

}



版权声明:本文为qq_42867525原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。