如何解决操作后获取所有文件并重命名
我有一个“ for”,在其中创建一些扩展名为“ .part”的文件。这些文件将添加到地图。下一个“ for”用于在每个文件中写入信息。我需要一个操作,该操作从该Map中获取所有文件,并将每个文件扩展名从.part重命名为.txt。我让我的代码在下面。
地图变量
final Map<String,FileWriter> fileMap = new HashMap<>();
在每个位置创建文件
for (final arabesqueDlvLocationModel locatie : localitati) {
final File file;
if (BooleanUtils.isTrue(judete.get(locatie.getCounty()))) {
file = new File(folderPath + "/Feed-facebook_" + locatie.getCounty() + "_" + locatie.getLocation() + ".part");
} else {
file = new File(folderPath + "/Feed-facebook_" + locatie.getCounty() + ".part");
}
try {
final FileWriter fileWriter = new FileWriter(file,false);
fileWriter.append(HEADER).append(NEW_LINE_SEParaTOR);
fileMap.put(locatie.getCode(),fileWriter);
} catch (final IOException e) {
e.printstacktrace();
}
}
写入每个文件
for (final ProductModel product : products) {
String productimageUrl = embrace(getProductimageUrl(product));
if (validProductForFeed(product,productimageUrl,ignoredProducts,ignoredCategories)) {
final String productCode = embrace(product.getCode());
final String productName = embrace(getProductName(product));
final String productDescription = embrace(getProductDescription(product));
final String productCategoryName = embrace(getCategoryName(product));
final String availability = embrace("in stock");
final String condition = embrace("new");
final String productUrl = embrace(getProductUrl(product));
final String productBrand = embrace(getBrand(product));
final String productGoogleCategory = embrace(getGoogleCategory(product));
final StringBuilder strB = new StringBuilder();
strB.append(productCode).append(SEMICOLON);
strB.append(productName).append(SEMICOLON);
strB.append(productDescription).append(SEMICOLON);
strB.append(productCategoryName).append(SEMICOLON);
strB.append(productUrl).append(SEMICOLON);
strB.append(productimageUrl).append(SEMICOLON);
strB.append(condition).append(SEMICOLON);
strB.append(availability).append(SEMICOLON);
for (final arabesquePlantModel plant : defaultPlants) {
final List<arabesqueDlvLocationModel> locationList = plantMap.get(plant.getName());
final List<Priceinformation> priceinformationList = getPriceService().getPriceinformationsForProduct(product);
final Map<String,String> priceMap = getPriceMap(product,priceinformationList,plant);
if (!priceMap.isEmpty()) {
final String productDefaultPrice = embrace(getProductPrice(priceMap,DEFAULT));
final String productPromoPrice = embrace(getProductPrice(priceMap,PROMO));
final Long highestStock = arabesqueCommerceStockService.getStockLevelForProductSalesUnit(product,plant);
final boolean inStock = highestStock >= 1;
if (inStock) {
for (final arabesqueDlvLocationModel location : locationList) {
final StringBuilder strBuilder = new StringBuilder(strB.toString());
strBuilder.append(productDefaultPrice).append(SEMICOLON);
strBuilder.append(productPromoPrice).append(SEMICOLON);
strBuilder.append(productBrand).append(SEMICOLON);
strBuilder.append(productGoogleCategory);
try {
fileMap.get(location.getCode()).append(strBuilder).append(NEW_LINE_SEParaTOR);
} catch (final IOException e) {
LOG.error(e.getMessage(),e);
}
}
}
}
}
}
}
for (final FileWriter fileWriter : fileMap.values()) {
try {
fileWriter.close();
} catch (final IOException e) {
LOG.error(e.getMessage(),e);
}
}
解决方法
已解决:我添加了
final File dir = new File(folderPath);
for(File file : dir.listFiles()){
int index = file.getName().lastIndexOf('.');
String nameWithoutExtension = file.getName().substring(0,index);
file.renameTo(new File(folderPath + "/" + nameWithoutExtension + ".csv"));
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。