[英]Skip over lines in a csv if exception is thrown Java Stream
我正在使用Java 8 Stream類讀取大約500Mb的.csv文件,幾乎所有數據的格式都相同,除了找到的2個實例。 我在ArrayList中存儲的每個對象有52行,然后將它們添加到HashMap中,以便我可以基於鍵訪問它們。 我使用HashMap使用不同的類為每個對象創建一個excel文件,然后在創建文件后立即清除列表,然后移至另一個對象。 問題在於,當它到達其中包含較少數字的行時,excel創建類試圖從不存在的索引中獲取數字,從而拋出NullPointerException。 如果拋出NullPointerException,是否有跳過這些行的方法? 我知道,如果出現此問題,我必須跳過52行。
try
{
final String regex = "\\d*\\.?\\d+";
Stream<String> lines = Files.lines( file, StandardCharsets.UTF_8 );
for( String line : (Iterable<String>) lines.skip(currentLine)::iterator ){
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(line.substring(0));
while (matcher.find()) {
testPop.add(Double.parseDouble(matcher.group(0)));
}
currentLine++;
if(currentLine%52==0) {
for(int i =0;i<52;i++) {
int date=4+29*i;
int a=13+29*i;
int b=6+29*i;
int c=15+29*i;
int d=16+29*i;
int e=8+29*i;
int f=17+29*i;
int g=14+29*i;
int h=7+29*i;
WeeklyCalculations.put(Integer.parseInt(String.valueOf((int)((testPop.get(date))/1))),new Calculations(testPop.get(a),3,1,testPop.get(b),testPop.get(c),testPop.get(d),testPop.get(e),testPop.get(f),testPop.get(g),testPop.get(h),testPop.get(date),WeeklyCalculations));
}
findZeroStockOuts();
ExcelCreator x = new ExcelCreator(WeeklyCalculations,String.valueOf(((int)(testPop.get(1)/1))),String.valueOf(((int)(testPop.get(2)/1))), noStockouts, stockOuts);
x.createExcel();
testPop.clear();
WeeklyCalculations.clear();
counter++;
System.out.println(counter + "/" + "67101 - "+TimeUnit.SECONDS.convert(System.nanoTime(), TimeUnit.NANOSECONDS));
}
}
} catch (IOException ioe){
ioe.printStackTrace();
}
catch(NullPointerException x) {
readToExcel(currentLine+52);
}
我能夠在循環中跳過它們,但是考慮到大約350萬行,這極大地降低了速度,並且每次迭代后都必須跳過所有這些。 有一種有效的方法可以做到這一點嗎?
速度慢的原因是因為您從頭開始反復讀取文件。 在下面的塊中填寫代碼:
final String regex = "\\d*\\.?\\d+";
final Pattern pattern = Pattern.compile(regex);
try (Stream<String> lines = Files.lines(file, StandardCharsets.UTF_8)) {
final Iterator<String> iter = lines.iterator();
for (int currentLine = 1; iter.hasNext(); currentLine++) {
String line = iter.next();
final Matcher matcher = pattern.matcher(line); // No reason do: line.substring(0)
while (matcher.find()) {
// testPop.add(Double.parseDouble(matcher.group(0)));
}
try {
if (currentLine % 52 == 0) {
for (int i = 0; i < 52; i++) {
// TODO
}
}
// TODO:
} catch (IOException ioe) {
ioe.printStackTrace();
while (currentLine % 52 != 0 && iter.hasNext()) {
iter.next();
currentLine++;
}
} catch (NullPointerException x) {
// readToExcel(currentLine + 52);
while (currentLine % 52 != 0 && iter.hasNext()) {
iter.next();
currentLine++;
}
}
}
}
final String regex = "\\d*\\.?\\d+";
final Pattern pattern = Pattern.compile(regex);
try (StreamEx<String> stream = StreamEx.ofLines(file, StandardCharsets.UTF_8)) {
stream.splitToList(52).filter(l -> l.size() == 52).forEach(lines -> {
lines.stream().forEach(line -> {
final Matcher matcher = pattern.matcher(line); // No reason do: line.substring(0)
while (matcher.find()) {
// testPop.add(Double.parseDouble(matcher.group(0)));
}
});
try {
// TODO:
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (NullPointerException x) {
// readToExcel(currentLine + 52);
}
});
} catch (IOException e) {
e.printStackTrace();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.