From 7e1307259ef932fe8d8e4309ec288d3558fce79c Mon Sep 17 00:00:00 2001 From: Peter Wu Date: Sat, 3 May 2014 17:53:38 +0200 Subject: DataWriter: better detect errors in reading data Now print the faulty line and line number. Using BufferedReader instead of Scanner also gave me a hint of the error, so let's stick to it. --- src/io/DataWriter.java | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/io/DataWriter.java b/src/io/DataWriter.java index ebbd129..b4cf4d5 100644 --- a/src/io/DataWriter.java +++ b/src/io/DataWriter.java @@ -1,14 +1,15 @@ package io; +import java.io.BufferedReader; import java.io.Closeable; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.io.InputStreamReader; import java.io.OutputStream; import java.util.HashSet; -import java.util.Scanner; import java.util.Set; import java.util.logging.Level; import java.util.logging.Logger; @@ -114,21 +115,27 @@ public class DataWriter implements ResultListener, Closeable { */ private void readIds(Set idSet, Store store) throws IOException { InputStream is = null; + String line = null; + long lineno = 1; try { is = store.getInputStream(); - Scanner reader = new Scanner(is); + BufferedReader reader = new BufferedReader(new InputStreamReader(is)); // parse each line into a JSONObject, read the id and add it to // the set of ids. - while (reader.hasNext()) { - JSONObject obj = new JSONObject(reader.nextLine()); + while ((line = reader.readLine()) != null) { + JSONObject obj = new JSONObject(line); long id = obj.getLong("id"); idSet.add(id); + lineno++; } } catch (FileNotFoundException ex) { // ignore, file will be created if necessary. - } catch (JSONException ex) { + } catch (JSONException | IOException ex) { + if (line != null) { + getLogger().log(Level.INFO, "Last line: " + line); + } getLogger().log(Level.WARNING, store.getFileName() - + ": Corrupt file?", ex); + + ": error occurred in file at line " + lineno, ex); throw new IOException(ex); } finally { IOUtils.closeQuietly(is); -- cgit v1.2.1