File dir = new File(url); if(!dir.exists()){ dir.mkdirs(); }
时间: 2024-06-06 14:10:59 浏览: 101
This code creates a new File object called "dir" with a specified URL. It then checks if the directory exists using the "exists()" method. If the directory does not exist, it creates a new directory using the "mkdirs()" method. The "mkdirs()" method creates any necessary but nonexistent parent directories as well.
Overall, this code is used to ensure that the specified directory exists before attempting to perform any file operations on it.
相关问题
while (calendar .getTime().before(TUpdEndTm)) Date date = calendar .getTime(); calendar.add(Calendar .DAY_OF_YEARamount: 1) TUpdTm = sdf.format(date) ; String dir = rootdir + "/" + TUpdTm; // 连接FTP ChannelSftp connect = FtpUtil.connect(username, password, host, port);List<String> rootFilelists = FtpUtil.listFiles(connect, rootdir); if (!rootFilelists.isEmpty() && rootFilelists.contains(TUpdTm)) { try {vatService.deletefile( delpath: imageFile + "/" + TUpdTm);vatService.deletefile( delpath: imageFile + "/" + TUpdTm + ",zip");} catch (Exception e1) { e1.printStackTrace(); // 创建存放单证的文件夹 File cImFile = new File(imageFile);if (!clmFile.exists() && !clmFile.isDirectory()) {cImFile.mkdir(): // 创建以公式日期命名存放单证的文件夹 File clmNoFile = new File( pathname: imageFile + TUpdTm); if (!clmNoFile.exists() && !clmNoFile.isDirectory()) { clmNoFile.mkdir(); OutputStream os = null; try { List<String> fileLists = FtpUtil.listFiles(connect, dir);List<String> gsNumList = new ArrayList<String>(0); if (!fileLists.isEmpty()) { /*List<String> gsList = ps.getGsList(fileLists); for (String gs : gsList) { for (String file : fileLists) {if(file.contains(gs)){gsNumList.add(file); gsNumList = fileLists; if (!gsNumList.isEmpty()) { for (String file : gsNumList) { File localFile = new File( pathname: imageFile + TUpdTm + "/" + file);if (!localFile.getParentFile().exists()) { localFile.getParentFile().mkdirs(); TocalFile.createNewFile() connect.cd(dir);// 进入所在路径 os = new FileOutputStream(localFile); connect.get(file, os); os.close(); } else { serviceResponse.setReturn_type(ActionConstant.RESPONSE_RESULT_TYPE_ERROR);serviceResponse.setReturnMessage("该日期不存在公示异议!"); // /打包Zip vatService.createZip( sourcePath: imageFile + TUpdTm,zipPath: imageFile + TUpdTm + ".zip"); } catch (Exception e) { e.printStackTrace() : } finally { if (null != ftpFileInput) { ftpFileInput.close() : os.close(); l*//删除文件夹下所有文件 serviceResponse.setReturnMessage("true");} else { serviceResponse .setReturn_type(ActionConstant.RESPONSE_RESULT_TYPE_ERROR);serviceResponse.setReturnMessage(该日期不存在公示异议!"); return serviceResponse; 在这段代码中,TUpdTm会在while的循环中变成三个不同的值,如何能让只要有其中一次的TUpdTm满足if条件就不进入else,相反,三次TUpdTm的值均不满足if条件才进入else,不能使用break跳出循环的方法
您可以使用一个boolean类型的变量来记录是否满足if条件,只要有一次满足就将这个变量置为true,当三次TUpdTm的值均不满足if条件时,判断这个变量是否为false,如果是则进入else。代码示例:
```java
boolean isTUpdTmValid = false; // 记录TUpdTm是否满足if条件
while (calendar.getTime().before(TUpdEndTm)) {
Date date = calendar.getTime();
calendar.add(Calendar.DAY_OF_YEAR, 1);
TUpdTm = sdf.format(date);
String dir = rootdir + "/" + TUpdTm;
// 连接FTP
ChannelSftp connect = FtpUtil.connect(username, password, host, port);
List<String> rootFilelists = FtpUtil.listFiles(connect, rootdir);
if (!rootFilelists.isEmpty() && rootFilelists.contains(TUpdTm)) {
try {
vatService.deletefile(delpath: imageFile + "/" + TUpdTm);
vatService.deletefile(delpath: imageFile + "/" + TUpdTm + ",zip");
} catch (Exception e1) {
e1.printStackTrace();
}
// 创建存放单证的文件夹
File cImFile = new File(imageFile);
if (!clmFile.exists() && !clmFile.isDirectory()) {
cImFile.mkdir();
}
// 创建以公式日期命名存放单证的文件夹
File clmNoFile = new File(pathname: imageFile + TUpdTm);
if (!clmNoFile.exists() && !clmNoFile.isDirectory()) {
clmNoFile.mkdir();
OutputStream os = null;
try {
List<String> fileLists = FtpUtil.listFiles(connect, dir);
List<String> gsNumList = new ArrayList<String>(0);
if (!fileLists.isEmpty()) {
/*List<String> gsList = ps.getGsList(fileLists);
for (String gs : gsList) {
for (String file : fileLists) {
if(file.contains(gs)){
gsNumList.add(file);
}
}
}*/
gsNumList = fileLists;
if (!gsNumList.isEmpty()) {
for (String file : gsNumList) {
File localFile = new File(pathname: imageFile + TUpdTm + "/" + file);
if (!localFile.getParentFile().exists()) {
localFile.getParentFile().mkdirs();
}
TocalFile.createNewFile();
connect.cd(dir);// 进入所在路径
os = new FileOutputStream(localFile);
connect.get(file, os);
os.close();
}
} else {
serviceResponse.setReturn_type(ActionConstant.RESPONSE_RESULT_TYPE_ERROR);
serviceResponse.setReturnMessage("该日期不存在公示异议!");
}
}
// /打包Zip
vatService.createZip(sourcePath: imageFile + TUpdTm, zipPath: imageFile + TUpdTm + ".zip");
isTUpdTmValid = true; // 标记TUpdTm满足if条件
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != ftpFileInput) {
ftpFileInput.close();
}
os.close();
// 删除文件夹下所有文件
serviceResponse.setReturnMessage("true");
}
}
}
}
if (!isTUpdTmValid) { // TUpdTm三次均不满足if条件
serviceResponse.setReturn_type(ActionConstant.RESPONSE_RESULT_TYPE_ERROR);
serviceResponse.setReturnMessage("该日期不存在公示异议!");
return serviceResponse;
} else {
// do something
}
```
private void init() { this.list = new ArrayList<>(); //this.filePath = Environment.getExternalStorageDirectory().getPath() + "/sdcard/xiaoshipin/import/inventory.xlsx"; File dir = new File(Environment.getExternalStorageDirectory(), "/sdcard/xiaoshipin/import/"); if (!dir.exists()) { dir.mkdirs(); } String filePath = dir.getPath() + "/inventory.xlsx"; this.excelList = ExcelUtils.read(filePath); if (excelList != null) { for (Map<String, String> map : excelList) { ExcelImportViewModel model = new ExcelImportViewModel(); model.SKU =Double.parseDouble(map.get("SKU")); model.name = map.get("商品"); model.barCode = map.get("條碼"); model.real_inventory = Double.parseDouble(map.get("預期應有庫存")); model.difference = map.get("差值"); model.CostDifference = map.get("成本差值"); list.add(model); } } iv.inventoryList = list; }this.excelList = ExcelUtils.read(filePath);为null
根据你提供的代码,可以看出读取 Excel 文件的操作是通过调用 `ExcelUtils.read(filePath)` 方法实现的。如果该方法返回 `null`,那么就说明读取 Excel 文件失败了。
你可以尝试使用日志打印功能,输出 `filePath` 的值,然后检查该路径下是否存在名为 `inventory.xlsx` 的文件。如果文件不存在,那么读取操作肯定会失败,返回 `null`。
另外,你也可以检查一下 Excel 文件的格式是否正确。如果 Excel 文件格式不正确,读取操作同样会失败,返回 `null`。
最后,如果你仍然无法解决问题,请提供更多的错误信息或者调试信息,以便更好地帮助你解决问题。
阅读全文