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();
}
------解决方案--------------------
import java.io.File;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.PrintWriter;
public final class LogWriter extends MyObject
{
/*-------Member Property--------------------------------*/
privateFile m_File = null;
private FileWriter m_FileWriter = null;
private BufferedWriter m_BufferedWriter = null;
privatePrintWriterm_PrintWriter= null;
private Stringm_strFileName= null;
private Stringm_strLogsDirectory= null;
private Stringm_strBackupDirectory= null;
private LogWriter()
{
}
public LogWriter(ENTITY entDebugSetting)
{
m_strFileName = entDebugSetting.getTextProperty( "LOGFILENAME ");
String TS = DateTimeHelper.getSecondTS();
m_strFileName = m_strFileName.replaceAll( "#DATETIME# ",TS);
m_strFileName = m_strFileName.replaceAll( "#APPLICATION# ",MyObject.JAVA_APPLICATION_NAME);
m_strLogsDirectory = entDebugSetting.getTextProperty( "LOGS ");
m_strBackupDirectory = entDebugSetting.getTextProperty( "LOGS_BACKUP ");
if(createFile(m_strLogsDirectory+m_strFileName))
if(createFileWriter())
if(createBufferedWriter())
createPrintWriter();
if(m_PrintWriter!=null)
{
println( " <?xml version= ' "
+entDebugSetting.getTextProperty( "XMLVERSION ")
+ " ' encoding= ' "
+entDebugSetting.getTextProperty( "XMLENCODING ")
+ " '?> ");
println( " <MYLOG> ");
println(this.dump());
println( " <APPLICATION> "+MyObject.JAVA_APPLICATION_NAME+ " </APPLICATION> ");
println( " <TIME> "+TS+ " </TIME> ");
println( " <GENERATOR> gdut.cims.LogWriter </GENERATOR> ");
println( " <DESIGNER> victor.woo@163.net </DESIGNER> ");
}
}
privateboolean createFile(String filename)
{
try
{
m_File = new File(filename);
if(!m_File.exists())
return m_File.createNewFile();
return true;
}
catch(Exception e)
{
MyDebug.DEBUGEXCEPTION(this, "createFile ",e);
return false;
}
}
privateboolean createFileWriter()
{
try
{
m_FileWriter = new FileWriter(m_File);
return true;
}
catch(Exception e)
{
MyDebug.DEBUGEXCEPTION(this, "createFileWriter ",e);
return false;
}
}
privateboolean createBufferedWriter()
{
try
{
m_BufferedWriter = new BufferedWriter(m_FileWriter);
return true;
}
catch(Exception e)
{
MyDebug.DEBUGEXCEPTION(this, "createBufferedWriter ",e);
return false;
}
}
protected PrintWriter getPrintWriter()
{
return m_PrintWriter;
}
private boolean createPrintWriter()
{
try
{
m_PrintWriter = new PrintWriter(m_BufferedWriter,true);//autoflush
return true;
}
catch(Exception e)
{
MyDebug.DEBUGEXCEPTION(this, "createPrintWriter ",e);
return false;
}
}
protected void destructor()
{
super.MyObjectDestructor();
println( " </MYLOG> ");
try
{
if(m_PrintWriter!=null)
m_PrintWriter.close();
if(m_BufferedWriter!=null)
m_BufferedWriter.close();
if(m_FileWriter!=null)
m_FileWriter.close();
if(m_File!=null)
{
//Move File
File backup = new File(m_strBackupDirectory+m_strFileName);
if(!backup.getParentFile().exists())
backup.getParentFile().mkdir();
m_File.renameTo(backup);
m_File.delete();
backup = null;
}
}
catch(Exception e)
{
MyDebug.DEBUGEXCEPTION(this, "destructor() ",e);
}
m_File = null;
m_FileWriter = null;
m_BufferedWriter = null;
m_PrintWriter= null;
}
protected String dump()
{
return " <LogWriter> "
+ "\n\t <FILENAME> "+m_strLogsDirectory+m_strFileName+ " </FILENAME> "
+ "\n </LogWriter> ";
}
public void println(String aLine)
{
if(m_PrintWriter!=null)
{
m_PrintWriter.println(aLine);
}
}
}
[解决办法]
mport java.io.*;
class testio{
public static void main(String[] args) throws Exception{
//读取输入流
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print( "请输入你要保存的字符: ");
//读取一个文本行
String str=br.readLine();
//创建一个新的File对象
File f=new File( "testio.txt ");
//创建一个新的文件testio.txt
f.createNewFile();
//打印刚才读取的字符
System.out.println(str);
//创建输出流
FileOutputStream fos=new FileOutputStream( "testio.txt ");
//写到文本
fos.write(str.getBytes());
//关闭输出流
fos.close();
}
}
[解决办法]
构造一个文件输出流,然后使用System类的setOut方法重置输出流即可。