读取 .txt 文件时每次输出后缺少一行
Missing one line after each output while reading a .txt file
获得已安装程序的列表后,我将它们全部写入 [=19=] 文件。
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();
但是当我尝试使用缓冲 reader 读取该文件时,它总是在每次输出后漏掉一行。
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
你每次循环读两行:
while(br1.readLine()!=null) //first
{
line1=br1.readLine(); //second
System.out.println(line1);
}
改为
while((line1= br1.readLine())!=null)
{
System.out.println(line1);
}
创建一个临时布尔变量以继续循环:
Boolean continueLoop = true;
while (continueLoop) {
line = input.readLine();
if(line == null) {
continueLoop = false; // break;
}
br.write(++number+"."+line);
br.newLine();
}
获得已安装程序的列表后,我将它们全部写入 [=19=] 文件。
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();
但是当我尝试使用缓冲 reader 读取该文件时,它总是在每次输出后漏掉一行。
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
你每次循环读两行:
while(br1.readLine()!=null) //first
{
line1=br1.readLine(); //second
System.out.println(line1);
}
改为
while((line1= br1.readLine())!=null)
{
System.out.println(line1);
}
创建一个临时布尔变量以继续循环:
Boolean continueLoop = true;
while (continueLoop) {
line = input.readLine();
if(line == null) {
continueLoop = false; // break;
}
br.write(++number+"."+line);
br.newLine();
}