当前位置: 动力学知识库 > 问答 > 编程问答 >

android - how to copy the console output into a file in java?

问题描述:

package prgms;

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.FileWriter;

import java.io.IOException;

import java.io.InputStreamReader;

class new_experi {

public static void main(String[] args) throws IOException {

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

BufferedWriter out = new BufferedWriter(new FileWriter("C://Users//sihi//Desktop//output.txt"));

try

{

String inputLine = null;

do {

inputLine = in.readLine();

out.write(inputLine);

out.newLine();

} while (!inputLine.equalsIgnoreCase("eof"));

System.out.print("Write Successful");

} catch (IOException e1) {

System.out.println("Error during reading/writing");

} finally {

out.close();

in.close();

}

}

}

i am getting output but the last line i.e "write successfully" is not copied to the file.

网友答案:

Instead of

System.out.print("Write Successful");

Write

out.write("Write Successful");

And if you are trying to log, you can use log4j.

网友答案:

System.out.print("") will output into Console,u should use out.write("") instead.

分享给朋友:
您可能感兴趣的文章:
随机阅读: