Java 操作后获取所有文件并重命名它们
我有一个“for”,在这里我创建了一些扩展名为“.part”的文件。这些文件将添加到地图中。下一个“for”用于在每个文件中写入信息。我需要一个操作,从该映射中获取所有文件,并将每个文件扩展名从.part重命名为.txt。我把我的代码放在下面 映射变量Java 操作后获取所有文件并重命名它们,java,dictionary,data-structures,hybris,Java,Dictionary,Data Structures,Hybris,我有一个“for”,在这里我创建了一些扩展名为“.part”的文件。这些文件将添加到地图中。下一个“for”用于在每个文件中写入信息。我需要一个操作,从该映射中获取所有文件,并将每个文件扩展名从.part重命名为.txt。我把我的代码放在下面 映射变量 final Map<String, FileWriter> fileMap = new HashMap<>(); 写入每个文件 for (final ProductModel product : products
final Map<String, FileWriter> fileMap = new HashMap<>();
写入每个文件
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);
}
}
}
}
}
}
}
答案:我补充道
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"));
}
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"));
}