2017-08-23 14 views
0

インストールされているプログラムのリストを取得した後、それらをすべてpro.txtファイルに書き出しています。.txtファイルの読み取り中に各出力後に1行が欠けている

String filename="C:\\Users\\Zeeshan\\Desktop\\pro.txt"; 
FileWriter fileWriter =new FileWriter(filename); 
BufferedWriter br =new BufferedWriter(fileWriter); 
String command = "powershell.exe \"Get-ItemProperty HKLM:\\Software\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\* | Select-Object DisplayName | Format-Table –AutoSize\""; 
Process p = Runtime.getRuntime().exec(command); 
p.getOutputStream().close(); 
BufferedReader input =new BufferedReader(new InputStreamReader(p.getInputStream())); 
String line; 
int number=0; 
while ((input.readLine()) != null) 
{ 
    line=input.readLine(); 
    br.write(++number+"."+line); 
    br.newLine(); 
} 
br.close(); 
input.close(); 

しかし、私はそれは常に、各出力の後に1行を逃し、バッファリーダーを使用してそのファイルを読み込むしようとしています。

FileReader fr=new FileReader(filename); 
BufferedReader br1=new BufferedReader(fr); 
String line1; 
while(br1.readLine()!=null) 
{ 
    line1=br1.readLine(); 
    System.out.println(line1); 
} 
br1.close(); 

私の出力はこの形式のようです。最後にnullが表示される理由も教えてください。

2.                
4.Google Chrome             
6.RuntimePack 15.7.22           
8.Scan               
10.                
12.                
14.4500K710_Software_Min           
16.Apple Software Update           
18.Microsoft Visual C++ 2015 x86 Additional Runtime - 14.0.24215 
20.WebReg              
22.Update for Microsoft .NET Framework 4.6.1 (KB4014606)   
24.Adobe Refresh Manager           
26.Microsoft Visual C++ 2015 x86 Minimum Runtime - 14.0.24215 
28.4500K710              
30.Apple Application Support (32-bit)       
32.                
34.                
36.BufferChm              
null 

答えて

4

あなたはすべてのループに2つの行を読み込む:

while((line1= br1.readLine())!=null) 
{ 
    System.out.println(line1); 
} 
+0

ええ、uとはい、ありがとうございます。 –

+0

@zeeshannisarあなたは歓迎 – Jens

+0

私はこの問題を解決としてマークできますか? –

1

while(br1.readLine()!=null) //first 
{ 
    line1=br1.readLine(); //second 
    System.out.println(line1); 
} 

変更は、ループを継続するために、一時的なブール変数を作成します。

Boolean continueLoop = true; 

while (continueLoop) { 
    line = input.readLine(); 
    if(line == null) { 
     continueLoop = false; // break; 
    } 
    br.write(++number+"."+line); 
    br.newLine(); 
} 
+1

@元 - 私の修正をありがとう。これは私にとってちょっとしたスタートです。 –

関連する問題