FindBugs 抱怨(理所当然地),我没有为我的文件指定编码。此后我更改了代码,但错误仍然存在,我不知道为什么。
有问题的代码:
private void readGndFile() throws IOException {
try (final BufferedReader reader = Files.newBufferedReader(gndFile.toPath(), Charset.forName("utf-8"))) {
readGndFile(reader); // Findbugs: Found reliance on default encoding
}
}
附送方式:
private void readGndFile(final BufferedReader reader) throws IOException {
while (true) {
String line = reader.readLine();
while (!isGndEntryHeader(line))
line = reader.readLine();
if (isEndOfFile(line))
break;
List<String> gndEntryParts = readGndEntry(reader);
if (gndEntryParts == null)
break;
DataObject dataObject = tryConvertGndEntry(gndEntryParts);
if (dataObject != null)
dataObjects.add(dataObject);
}
}
我正在使用带有 FindBugs 0.9.97 的 IntelliJ Idea 12
如果需要更多代码:
private boolean isGndEntryHeader(final String line) {
final String gndHeaderMask = "<http://d-nb.info/gnd/[0-9]+-([0-9]|[a-zA-Z])>";
return (line != null && !line.matches(gndHeaderMask));
}
private boolean isEndOfFile(final String line) {
return (line == null);
}
private List<String> readGndEntry(final BufferedReader reader) throws IOException {
List<String> gndEntry = new ArrayList<>();
String line = reader.readLine();
if (line == null)
return null;
else
line = line.trim();
while (line != null && !line.isEmpty()) {
gndEntry.add(line.trim());
line = reader.readLine();
}
if (line == null && gndEntry.size() == 0)
return null;
return gndEntry;
}
protected abstract DataObject tryConvertGndEntry(List<String> gndEntryParts);