I wrote a method that prepares SQL insert statements and stores them in a text file. At this point it gets the job done, but I have a catch block that is a total eyesore. If I could somehow close a BufferedReader without implicitly calling flush that would be perfect. Is that possible? If yes, how?
As preparation for populating a master table with data on kanji(Japanese characters) for the memorization application I am making, I am making a list of all the characters. The data source is KANJIDIC2, a UTF-8 encoded xml file with data on 13k+ characters. The original idea was to include all the characters in the source file, but for some reason 300-or-so characters throw a java.nio.charset.MalformedInputException
when I try to write them to my output file.
I decided to give up on those characters since they're not essential or anything, but I couldn't find a smooth way to close my BufferedReader after the exception above.
File outputFile = new File("C:\\Users\\tobbelobb\\Documents\\kanjilist.bsv");
try {
BufferedWriter bw = Files.newBufferedWriter(outputFile.toPath(), StandardCharsets.UTF_8);
for (Kanji nextKanji : kanjiList) {
try {
StringBuilder sb = new StringBuilder();
// sb.append stuff from list of objects...
bw.write(sb.toString());
bw.newLine();
bw.flush();
} catch (MalformedInputException ex) {
// Ungracefully swallow the exception.
ex.printStackTrace();
bw = Files.newBufferedWriter(outputFile.toPath(), StandardCharsets.UTF_8, StandardOpenOption.APPEND);
}
}
bw.close();
} catch (Exception ex) {
ex.printStackTrace();
}
I looked for a method to dispose of my BufferedReader object in the catch block, but the only one I could find is close()
, which throws that same MalformedInputException
again, and in the process writes an incomplete line of text to my file.