zeeshan nisar
zeeshan nisar

Reputation: 573

Missing one line after each output while reading a .txt file

After getting a list of installed programs I am writing all of them to a pro.txt file.

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();

But when I am trying to read that file using buffered reader it always misses one line after each output.

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();

My output is like in this form Please also guide me why null is appearing at the end.

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

Upvotes: 0

Views: 84

Answers (2)

Dipraj Shahane
Dipraj Shahane

Reputation: 13

Create a temporary Boolean variable to continue the loop:

Boolean continueLoop = true;

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

Upvotes: 1

Jens
Jens

Reputation: 69470

You read two lines in every loop:

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

Change to

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

Upvotes: 4

Related Questions