用File类在D盘创建文件夹和文件

import java.io.*;

public class CreateFile{
    
    public static void main(String[] args){
        
        String[] str = new String[6];
        str[0] = "D:/A/B/E";
        str[1] = "D:/A/B/F.txt";
        str[2] = "D:/A/C/G/H.txt";
        str[3] = "D:/A/C/I";
        str[4] = "D:/A/C/J";
        str[5] = "D:/A/D.txt";
        
        File[] file = new File[str.length];
        
        for(int i=0;i<file.length;i++){
            file[i] = new File(str[i]);
            
            if(!file[i].exists()){
                if(str[i].endsWith("txt")){
                    if(!file[i].getParentFile().exists()){
                        file[i].getParentFile().mkdirs();
                    }
                    try{
                        file[i].createNewFile();
                    } catch(IOException e){
                        e.printStackTrace();
                    }
                } else {
                    file[i].mkdirs();
                }
            }
            
        }
        
    }
    
}

猜你喜欢

转载自www.cnblogs.com/yxfyg/p/12388603.html