0
BufferedReaderを途中でClose
windows環境で以下のような
現在のメモリの空き容量を取得するメソッドを作成
しました。
//------------ここから
int memory = 0;
String[] command = {"cmd", "/c", "typeperf", "-sc", "1", "\"\\Memory\\Available MBytes\""};
Process pro = null;
try
{
pro = Runtime.getRuntime().exec(command);
}
catch (Exception e)
{
return 0;
}
InputStream in = pro.getInputStream();
String line;
BufferedReader br = null;
try
{
br = new BufferedReader(new InputStreamReader(in,"SJIS"));
int count = 1;
while ((line = br.readLine()) != null)
{
if(count == 3)
{
String[] str = line.split(",");
String edit = str[1].replace("\"", "");
String[] edit2 = edit.split("[.]");
memory = Integer.parseInt(edit2[0]);
break;
}
count++;
}
}
catch (IOException e)
{
e.printStackTrace();
}
catch (Exception e)
{
}
finally
{
try{
if(br != null)
{
br.close();
}
if(in != null)
{
in.close();
}
}catch(Exception e1)
{}
}
return memory;
//------------ここまで
while ((line = br.readLine()) != null)
{
処理1
}
処理1の部分で文字列を読み取り編集し
メモリの値を取っているのですが
必要な部分を取得して編集が終わったら
すぐさまループを抜け終了しています。
この部分が正しいかどうかよくわかりません。
文字列を取得し終わってもwhileのループ処理が
終わるまでは抜け出さない方がよいのでしょうか。
(途中で抜けた場合、バッファが残るか心配)