小编Per*_*rcy的帖子

如何从ByteBuffer中获取连续切片?

我有一个包含大文件(100 MB)的ByteBuffer:

    java.nio.ByteBuffer byteBuffer = ByteBuffer.wrap(multipartFile.getBytes());
    writeChannel.write(byteBuffer);
    writeChannel.closeFinally();
Run Code Online (Sandbox Code Playgroud)

我一次只能合法地写1 MB writeChannel.

如何切片ByteBuffer的内容,一次只写1 MB的切片到writeChannel

java bytebuffer

5
推荐指数
1
解决办法
8548
查看次数

如何避免在解析CSV行中的空位置时触发ArrayIndexOutOfBoundsException?

String[] values = line.split(",");

Long locId = Long.parseLong(replaceQuotes(values[0]));
String country = replaceQuotes(values[1]);
String region = replaceQuotes(values[2]);
String city = replaceQuotes(values[3]);
String postalCode = replaceQuotes(values[4]);
String latitude = replaceQuotes(values[5]);
String longitude = replaceQuotes(values[6]);
String metroCode = replaceQuotes(values[7]);
String areaCode = replaceQuotes(values[8]);

//...

public String replaceQuotes(String txt){
    txt = txt.replaceAll("\"", "");
    return txt;
}
Run Code Online (Sandbox Code Playgroud)

我正在使用上面的代码用这种格式的数据解析CSV:

828,"US","IL","Melrose Park","60160",41.9050,-87.8641,602,708
Run Code Online (Sandbox Code Playgroud)

但是,当我遇到一系列数据时,如下所示我得到了 java.lang.ArrayIndexOutOfBoundsException: 7

1,"O1","","","",0.0000,0.0000,,
Run Code Online (Sandbox Code Playgroud)

这是否意味着我甚至试图访问该值时values[7],会抛出异常?

如果是这样,我如何解析文本行的那个位置不包含数据的行?

java csv exception-handling string-parsing indexoutofboundsexception

1
推荐指数
1
解决办法
1934
查看次数