public static int createFile(String type, String path) { File file = new File(path); if(type.equals("目录")) { if(file.mkdirs()) { return 1; } else { return 3; } } else if(type.equals("文件")) { File parentDir = file.getParentFile(); if(!parentDir.exists()) { if(parentDir.mkdirs()) { try { if(file.createNewFile()) { return 2; } else { return 3; } } catch (IOException e) { e.printStackTrace(); return 3; } } else { return 3; } } else { try { if(file.createNewFile()) { return 2; } else { return 3; } } catch (IOException e) { e.printStackTrace(); return 3; } } } else { return 3; } }

javaFiles-创建根据字符串参数创建文件编写一个方法实现创建文件的功能传入两个参数第一个参数字符串类型表示文件类型目录或文件第二个参数字符串类型表示文件路径返回类型是数字类型要求如下:如果第一个参数是目录类型则根据第二个参数创建目录创建成功返回数字1如果第一个参数是文件类型则根据第二个参数创建文件;如果文件所在的目录不存在则先创建目录再创建文件创建成功返回数字2如果创建失败返回数字3示例第一

原文地址: http://www.cveoy.top/t/topic/bT8d 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录