Java 使用BufferedReader与Scanner解析CSV文件

Java 使用BufferedReader与Scanner解析CSV文件,java,Java,团队,我必须逐行解析文件,在单行中,我用“,”分隔。 第一个字符串是Name,第二个字符串是count。最后,我必须显示键和计数 比如说 Peter,2 Smith,3 Peter,3 Smith,5 我应该扮演彼得5和史密斯8 所以我在选择BufferedReader还是Scanner时感到困惑。通过了。我提出了这两种方法。我想了解你的担忧 方法1:使用缓冲读取器 private HashMap<String, MutableLong> readFile(File file)

团队,我必须逐行解析文件,在单行中,我用“,”分隔。 第一个字符串是Name,第二个字符串是count。最后,我必须显示键和计数 比如说

Peter,2 
Smith,3
Peter,3
Smith,5
我应该扮演彼得5和史密斯8

所以我在选择BufferedReader还是Scanner时感到困惑。通过了。我提出了这两种方法。我想了解你的担忧

方法1:使用缓冲读取器

private HashMap<String, MutableLong> readFile(File file) throws IOException {
        final HashMap<String, MutableLong> keyHolder = new HashMap<>();
        try (BufferedReader br = new BufferedReader(new InputStreamReader(
                new FileInputStream(file), "UTF-8"))) {
            for (String line; (line = br.readLine()) != null;) {
                // processing the line.
                final String[] keyContents = line
                        .split(KeyCountExam.COMMA_DELIMETER);
                if (keyContents.length == 2) {
                    final String keyName = keyContents[0];
                    final long count = Long.parseLong(keyContents[1]);
                    final MutableLong keyCount = keyHolder.get(keyName);
                    if (keyCount != null) {
                        keyCount.add(count);
                        keyHolder.put(keyName, keyCount);
                    } else {
                        keyHolder.put(keyName, new MutableLong(count));
                    }
                }

            }
        }
        return keyHolder;
    }

private static final String COMMA_DELIMETER = ",";
    private static volatile Pattern commaPattern = Pattern
            .compile(COMMA_DELIMETER);
我的问题是。我已经通过了hasNext扫描仪,切换模式对我来说没有坏处。我相信,从Java7来看,Scanner-do有限的缓冲区足以容纳这种文件

是否有任何一种方法比方法1更适合方法2,或者除此之外,我们还有其他选择吗。我只是为了测试而做的。显然,方法1中的相同代码将在此处替换。在approach 1中使用split将创建多个字符串实例。这可以通过扫描字符序列来避免(我是对的)

private HashMap<String, BigInteger> readFileScanner(File file)
            throws IOException {
        final HashMap<String, BigInteger> keyHolder = new HashMap<>();
        try (Scanner br = new Scanner(file, "UTF-8")) {
            while (br.hasNext()) {
                br.useDelimiter(commaPattern);
                System.out.println(br.next());
                System.out.println(br.next());
                br.useDelimiter(linePattern);
            }
        }
        return keyHolder;
    }
private HashMap readFileScanner(文件)
抛出IOException{
final HashMap keyHolder=新HashMap();
try(Scanner br=新扫描仪(文件“UTF-8”)){
while(br.hasNext()){
br.使用分隔符(COMMAPTERN);
System.out.println(br.next());
System.out.println(br.next());
br.使用分隔符(线型);
}
}
返回钥匙夹;
}

方法3:使用OpenCSV?我不能使用其他api,实际上我刚刚在Java中重新创建了MutableLong,为什么不能使用其他api?请注意,既然您使用Java 7,您应该使用
Files.newBufferedReader()
创建您的
BufferedReader
谢谢,我将使用Files.newBufferedReader。任何其他错误/可以通过其他方式完成
private HashMap<String, BigInteger> readFileScanner(File file)
            throws IOException {
        final HashMap<String, BigInteger> keyHolder = new HashMap<>();
        try (Scanner br = new Scanner(file, "UTF-8")) {
            while (br.hasNext()) {
                br.useDelimiter(commaPattern);
                System.out.println(br.next());
                System.out.println(br.next());
                br.useDelimiter(linePattern);
            }
        }
        return keyHolder;
    }