Jes*_*ssy 62 java console file-io
我试图使用此代码建议(http://www.daniweb.com/forums/thread23883.html#)将控制台输出写入txt文件但是我没有成功.怎么了?
try {
//create a buffered reader that connects to the console, we use it so we can read lines
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
//read a line from the console
String lineFromInput = in.readLine();
//create an print writer for writing to a file
PrintWriter out = new PrintWriter(new FileWriter("output.txt"));
//output to the file a line
out.println(lineFromInput);
//close the file (VERY IMPORTANT!)
out.close();
}
catch(IOException e1) {
System.out.println("Error during reading/writing");
}
Run Code Online (Sandbox Code Playgroud)
Ste*_*n C 117
你需要做这样的事情:
PrintStream out = new PrintStream(new FileOutputStream("output.txt"));
System.setOut(out);
Run Code Online (Sandbox Code Playgroud)
第二个陈述是关键.它将所谓的"final" System.out属性的值更改为提供的PrintStream值.
有类似的方法(setIn和setErr)用于改变标准输入和错误流; java.lang.System有关详细信息,请参阅javadocs.
上面的更一般的版本是这样的:
PrintStream out = new PrintStream(
new FileOutputStream("output.txt", append), autoFlush);
System.setOut(out);
Run Code Online (Sandbox Code Playgroud)
如果append是true,则流将附加到现有文件而不是截断它.如果autoflush是true,则只要写入字节数组,就会刷新输出缓冲区,println调用其中一个方法,或者\n写入a.
我想补充一点,使用Log4j,Logback或标准Java java.util.logging子系统等日志记录子系统通常更好.它们通过运行时配置文件提供细粒度的日志记录控制,支持滚动日志文件,提供系统日志记录等.
或者,如果您没有"记录",那么请考虑:
out作为参数传递的流而不是写入System.out.更改System.out可能会导致JVM中的其他代码出现令人讨厌的意外,而这些代码并不期望这种情况发生.
小智 31
无需编写任何代码,只需在控制台上的cmd中编写即可:
javac myFile.java
java ClassName > a.txt
Run Code Online (Sandbox Code Playgroud)
输出数据存储在a.txt文件中.
use*_*421 23
要保留控制台输出,即写入文件并将其显示在控制台上,您可以使用如下类:
public class TeePrintStream extends PrintStream {
private final PrintStream second;
public TeePrintStream(OutputStream main, PrintStream second) {
super(main);
this.second = second;
}
/**
* Closes the main stream.
* The second stream is just flushed but <b>not</b> closed.
* @see java.io.PrintStream#close()
*/
@Override
public void close() {
// just for documentation
super.close();
}
@Override
public void flush() {
super.flush();
second.flush();
}
@Override
public void write(byte[] buf, int off, int len) {
super.write(buf, off, len);
second.write(buf, off, len);
}
@Override
public void write(int b) {
super.write(b);
second.write(b);
}
@Override
public void write(byte[] b) throws IOException {
super.write(b);
second.write(b);
}
}
Run Code Online (Sandbox Code Playgroud)
并用于:
FileOutputStream file = new FileOutputStream("test.txt");
TeePrintStream tee = new TeePrintStream(file, System.out);
System.setOut(tee);
Run Code Online (Sandbox Code Playgroud)
(只是一个想法,不完整)
Boz*_*zho 11
创建以下方法:
public class Logger {
public static void log(String message) {
PrintWriter out = new PrintWriter(new FileWriter("output.txt", true), true);
out.write(message);
out.close();
}
}
Run Code Online (Sandbox Code Playgroud)
(我没有在上面的类中包含正确的IO处理,它不会编译 - 自己动手.还要考虑配置文件名.注意"true"参数.这意味着不会重新创建文件每次调用方法)
然后而不是System.out.println(str)打电话Logger.log(str)
这种手动方法不是优选的.使用日志框架 - slf4j,log4j,commons-logging等等