我编写了这段代码来将二进制数据转换为 ascii ,现在我想将控制台结果写入文本文件 output.txt 。它运行但问题是它将第一行打印到控制台并开始从第二行将输出写入文本文件,换句话说,它跳过了 fisr 行!
public static void main(String args[])
{
try{
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream("textfile.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
String input = br.readLine();
String output = "";
for(int i = 0; i <= input.length() - 8; i+=8)
{
int k = Integer.parseInt(input.substring(i, i+8), 2);
output += (char) k;
}
System.out.println("string: " + output);
orgStream = System.out;
fileStream = new PrintStream(new FileOutputStream("d:/output.txt",true));
// Redirecting console output to file
System.setOut(fileStream);
}
//Close the input stream
in.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
}
这些行负责将结果写入 output.txt:
System.out.println("string: " + output);
orgStream = System.out;
fileStream = new PrintStream(new FileOutputStream("d:/output.txt",true));
// Redirecting console output to file
System.setOut(fileStream);
如何在 eclipse 中保存输出以便能够再次使用它?现在我保存在D盘