如何将控制台输出复制到java中的文件中?

时间:2014-11-26 04:54:08

标签: java android

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

    }

}

我正在获得输出但是最后一行,即"写成功"未复制到文件中。

2 个答案:

答案 0 :(得分:0)

而不是

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

out.write("Write Successful");

如果您尝试登录,可以使用log4j

答案 1 :(得分:0)

System.out.print("")将输出到控制台,你应该使用out.write("")代替。