ファイルが正しく書き込まれたかどうか、つまり最後に EOF があるかどうかを確認する方法はありますか? ファイルを取得し、それらを非常に大きなファイルにマージしてから、それを使用して統計を取得するプログラムがあるため、私はそれを求めています。ポイントは、ファイルの終わりを認識しないため、2 番目の部分が終了しないことです。
コードの関連部分は次のとおりです: (重要な理由で投稿できないため、コード全体を要求しないでください)
FileWriter file=null;
PrintWriter pw = null;
String pathToRead=null;
InputStreamReader isr = null;
BufferedReader br = null ;
FileInputStream fis = null ;
TestJFileChooser d=new TestJFileChooser();
int c=1;
String line=null;
....
//here i select the files
selectedFile=new File(pathToRead);
//here I get one buffer reader for each file got with listFiles()
for(File file_sel:app){
if (file_sel.getName().startsWith("gtou")){
System.out.println(file_sel.getName());
fis = null;
try {
fis = new FileInputStream(file_sel);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
isr=new InputStreamReader(fis);
br=new BufferedReader(isr);
map.put(i, br);
num_file++;
i++;
}
}
//then I select the output file and open a print writer for it
fileToWrite=new File(pathToRead);
try {
file = new FileWriter(fileToWrite);
pw= new PrintWriter(file);
} catch (IOException e1) {
e1.printStackTrace();
}
//merging part
....
line=br.readLine();
while(line!=null){
System.out.println("line is:"+line);
....
line=br.readLine();
}
//end of merging ....
pw.flush();
pw.close();
try {
if (file!=null) file.close();
fis.close();
isr.close();
br.close();
for(int fi=0;fi<num_file;fi++){
br2=map.get(fi);
br2.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
so.kill();
Runtime r=Runtime.getRuntime();
r.gc();
//this is a popup that comes out
GlitchSquad gli=new GlitchSquad("Completed");
問題は、出力として次のようになることです。
line is: null ;
line is: null ;
line is: null ;
など そして、「完了」ポップアップに到達しない =(
コントロール ライン!=null が機能しないため、null とは何かを正確に理解できません。また、そのnullを文字列として使用しようとしました..しかし何も..ストリームを閉じる方法に問題があると思いましたが、今ではコードは正しいようです..しかし、それを止める方法はまだありません..
提案?前もって感謝します!
psストリームに焦点を当てるために要約されたバージョンです..変数は正しく宣言されており、インポートなどについても同じです
編集:コードが更新されました