我有一个"for“,其中我创建了一些扩展名为".part”的文件。文件将添加到地图中。下一个"for“用于在每个文件中写入信息。我需要一个操作,采取所有的文件后,从该映射,并将每个文件扩展名从.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);
}
}发布于 2020-11-10 19:02:01
已解决:我添加了
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"));
}https://stackoverflow.com/questions/64765507
复制相似问题