日期:2014-05-20  浏览次数:20789 次

java中怎样把System.out.println("...")中的数据保存到指定文件?
java中怎样把System.out.println( "... ")中的数据保存到指定文件?

例如   println,运行后可以输出Hellow   World,   但是这是在IDE里显示的。怎么样建立文档并将“Hello   World”保存呢?

“Hello   World”是个例子,小弟是想把程序的其他数据保存,谢谢各位,期待回答!

------解决方案--------------------
使用流吧。
------解决方案--------------------
用log4j吧

自己写的话肯定要另加东西或者自己写个函数的
------解决方案--------------------
public void StringBufferDemo() throws IOException{
File file=new File( "D:/test.txt ");//打开文件
if(!file.exists())
file.createNewFile();
FileOutputStream out=new FileOutputStream(file,true);//创建文件流
for(int i=0;i <10;i++){
StringBuffer sb=new StringBuffer();
sb.append( "Hello ");//在该文件末尾追加

}
out.close();//关闭文件
}
楼主,给分
------解决方案--------------------
发了两边,还是错的

------解决方案--------------------
在运行这个java的程序使加上一个输出的命令就可以了。例如,你的类叫做Demo.那么,就这么运行:java Demo > > c:\log.txt
------解决方案--------------------
System.setOut(PrintStream out);

Reassigns the "standard " output stream.
First, if there is a security manager, its checkPermission method is called with a RuntimePermission( "setIO ") permission to see if it 's ok to reassign the "standard " output stream.


Parameters:
out - the new standard output stream
Throws:
SecurityException - if a security manager exists and its checkPermission method doesn 't allow reassigning of the standard output stream.
Since:
JDK1.1

------解决方案--------------------
在你程序的main中加上下面的代碼:
try //重定向輸出流
{
String path = System.getProperties().getProperty( "user.dir ") + "\\log\\ " + System.currentTimeMillis() + "_log.txt ";//可以改路徑,如path= "C:\\log.txt ";
try {
new File(path).createNewFile();
} catch (IOException e) {
e.printStackTrace();
};
PrintStream ps = new PrintStream(path);
System.setOut(ps);
System.setErr(ps);
} catch (FileNotFoundException e)
{
e.printStackTrace();
}
------解决方案--------------------
cwq09mm() 说的就行了,更改out的输出流,他说的那个方法是本地方法,慎用。
------解决方案--------------------
应该用PipedInputStream,PipedOutputStream
------解决方案--------------------
是不是想做日志,用log4j。
------解决方案--------------------
System.setOut()一下就可以了
------解决方案--------------------
public static void main(String[] args){


String tempFilePath= "d:\\result.txt ";
File temp= new File( tempFilePath );
temp.mkdirs();

File f=new File(tempFilePath, "faq.xml ");
f.createNewFile();
FileWriter fw = null;
fw = new FileWriter(f);
fw.write( "aaaa\n ");
fw.write( "bb\tb ");
fw.close();
}
------解决方案--------------------