微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Java迭代器正在跳过半个元素

如何解决Java迭代器正在跳过半个元素

我写了一个小脚本,用 java 从 CSV 中读取。它需要一个 CSV,并将一些值从 CSV 推送到一个 HashMap。我的 CSV 有 110 条记录(没有标头的 109 条记录)但是我得到了一个包含 54 个值的 HashMap。当我调试时,我可以看到在每次迭代时,我的 CSV 中的一行被跳过。

这是代码

package **CENSORED**.utils;

import com.day.cq.dam.api.Asset;
import com.day.cq.dam.api.Rendition;
import com.day.text.csv.Csv;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.*;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;

public class DateFormatUtils {

  private static String dateFormatCsvPath = "/content/dam/csv/country_date_format.csv";

  public static String getDateFormatByLocale(Locale Locale,ResourceResolver resourceResolver) {
    Resource res = resourceResolver.getResource(dateFormatCsvPath);
    Asset asset = res.adaptTo(Asset.class);
    Rendition rendition = asset.getoriginal();
    InputStream is = rendition.adaptTo(InputStream.class);
    HashMap<String,String> localetoFormat = new HashMap<String,String>();
    Csv csv = new Csv();

    try {
      Iterator<String[]> rowIterator = csv.read(is,StandardCharsets.UTF_8.name());
      while (rowIterator.hasNext()) {
        String[] row = rowIterator.next();
        String country = row[1];
        String locale = row[4];
        String dateFormat = row[6];
        localetoFormat.put(locale.toLowerCase() + "_" + country.toLowerCase(),dateFormat);
      }
    } catch (IOException e) {
      e.printstacktrace();
    }

  }
}

这是我调试的一些截图

在第一次迭代时,我的 CSV 的第 2 行被添加到我的哈希图中。标题已被跳过。

enter image description here

在第二次迭代时,第 5 行被添加到我的哈希图中,但第 3-4 行没有。

enter image description here

在第三次迭代时,第 8 行被添加到我的 hasmap,但第 6-7 行没有。

enter image description here

最后,我的哈希图中有 53 个元素,而我期望有 109 个。

enter image description here

这也是我的 CSV 示例:

ISO 3166 Country Code,ISO639-2 Country Code,Country,ISO 3166 Country Code,ISO639-2 Lang,Language,Date Format
ALB,AL,Albania,sqi,sq,Albanian,yyyy-MM-dd
ARE,AE,United arab Emirates,ara,ar,arabic,dd/MM/yyyy
ARG,AR,Argentina,spa,es,Spanish,dd/MM/yyyy
AUS,AU,Australia,eng,en,English,d/MM/yyyy
AUT,AT,Austria,deu,de,German,dd.MM.yyyy
BEL,BE,Belgium,fra,fr,french,d/MM/yyyy
BEL,nld,nl,Dutch,d/MM/yyyy
BGR,BG,Bulgaria,bul,bg,Bulgarian,yyyy-M-d
BHR,BH,Bahrain,dd/MM/yyyy
BIH,BA,Bosnia and Herzegovina,srp,sr,Serbian,yyyy-MM-dd
BLR,BY,Belarus,bel,be,Belarusian,d.M.yyyy
BOL,BO,Bolivia,dd-MM-yyyy
BRA,BR,Brazil,por,pt,Portuguese,dd/MM/yyyy
CAN,CA,Canada,yyyy-MM-dd
CAN,dd/MM/yyyy

最后一个屏幕截图显示我的 CSV 在他们的行中有正确的 EOL

enter image description here

这是 csv.read() 函数,Adobe 为 AEM 制作的类:

    public Iterator<String[]> read(InputStream in,String charset) throws IOException {
        if (charset == null) {
            charset = System.getProperty("file.encoding");
        }

        InputStream in = new BufferedInputStream(in,4096);
        this.input = new InputStreamReader(in,charset);
        return this.read();
    }

解决方法

我最终选择了另一种解决方案,因为我无法使用这个解决方案。为了永久,我正在为一个 AEM 项目开发它;我决定利用 ACS-common 中的通用列表项来获取包含我需要的所有值的字典,而不是从 CSV 中读取。正如@Artistotle 所说,读者肯定有问题,所以我建议不要使用 com.day.text.csv.Csv;

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。