操作后获取所有文件并重命名

问题描述

我有一个“ 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"));
    }