首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >操作后获取所有文件并重命名

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

Stack Overflow用户
提问于 2020-11-10 16:41:07
回答 1查看 31关注 0票数 0

我有一个"for“,其中我创建了一些扩展名为".part”的文件。文件将添加到地图中。下一个"for“用于在每个文件中写入信息。我需要一个操作,采取所有的文件后,从该映射,并将每个文件扩展名从.part重命名为.txt。我让我的代码在下面。

映射变量

代码语言:javascript
复制
final Map<String, FileWriter> fileMap = new HashMap<>();

为每个位置创建文件

代码语言:javascript
复制
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();
        }
    }

写入每个文件

代码语言:javascript
复制
    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);
                            }
                        }
                    }
                }
            }
        }
    }

在这里,我想我需要重命名所有的文件

代码语言:javascript
复制
    for (final FileWriter fileWriter : fileMap.values()) {

        try {
            fileWriter.close();
        } catch (final IOException e) {
            LOG.error(e.getMessage(), e);
        }
    }
EN

回答 1

Stack Overflow用户

发布于 2020-11-10 19:02:01

已解决:我添加了

代码语言:javascript
复制
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"));
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64765507

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档