0
ファイルから読み込み、読み込んだファイルの特定のサブセットを書き込むファイルを作成するが、nullポインタの例外が発生するoutput.write(line)
と私はなぜわからないのですか?ファイルを作成してそれに書き込む(ヌルポインタ)
public void readCreateThenWriteTo(String file, String startRowCount, String totalRowCount) {
BufferedReader br = null;
File newFile = null;
BufferedWriter output = null;
StringBuilder sb = null;
int startRowCountInt = Integer.parseInt(startRowCount);
int totalRowCountInt = Integer.parseInt(totalRowCount);
try {
br = new BufferedReader(new FileReader(file));
sb = new StringBuilder();
newFile = new File("hiya.txt");
output = new BufferedWriter(new FileWriter(newFile));
String line = "";
int counter = 0;
while (line != null) {
line = br.readLine();
if (startRowCountInt <= counter && counter <= totalRowCountInt) {
System.out.println(line);
output.write(line);
}
counter++;
}
} catch (IOException e) {
// TODO Auto-generated catch block
LOGGER.info("File was not found.");
e.printStackTrace();
} finally {
// Should update to Java 7 in order to use try with resources and then this whole finally block can be removed.
try {
if (br != null) {
br.close();
}
if (output != null) {
output.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
LOGGER.info("Couldn't close BufferReader.");
e.printStackTrace();
}
}
}
ああ、私は前にその構文を見たことがありません。その構文は何を意味しますか? – robben
@robben 'line'を更新してすぐに評価しています。代入式はJavaで評価することもできます。そのため、 'a = b = c;'は法的な記述です。 – shmosel
ああ、ありがとう! – robben