java中怎样把System.out.println("...")中的数据保存到指定文件?例如 println,运行后可以输出Hellow World, 但是这是在IDE里显示的。怎么样建立文档并将“Hello World”保存呢?“Hello World”是个例子,小弟是想把程序的其他数据保存,谢谢各位,期待回答!

解决方案 »

  1.   

    用log4j吧自己写的话肯定要另加东西或者自己写个函数的
      

  2.   

    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();//关闭文件
        }
    楼主,给分
      

  3.   

    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();//关闭文件
        }
    楼主,给分
      

  4.   

    在运行这个java的程序使加上一个输出的命令就可以了。例如,你的类叫做Demo.那么,就这么运行:java Demo >> c:\log.txt
      

  5.   

    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 
      

  6.   

    sorry漏掉了,out.write(sb.toString().getBytes("utf-8"));
      

  7.   

    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.write(sb.toString().getBytes("utf-8"));
    }
    out.close();//关闭文件
    }
    han!
      

  8.   

    在你程序的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();
    }
      

  9.   

    cwq09mm() 说的就行了,更改out的输出流,他说的那个方法是本地方法,慎用。
      

  10.   

    应该用PipedInputStream,PipedOutputStream
      

  11.   

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

  12.   

    import java.io.File;
    import java.io.FileWriter;
    import java.io.BufferedWriter;
    import java.io.PrintWriter;public final class LogWriter extends MyObject
    {
        /*-------Member Property--------------------------------*/
    private File  m_File  = null;
    private  FileWriter   m_FileWriter  = null;
    private  BufferedWriter  m_BufferedWriter  = null;
    private PrintWriter m_PrintWriter = null; private  String m_strFileName = null;
    private  String m_strLogsDirectory = null;
    private  String m_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>[email protected]</DESIGNER>");
    }
    } private boolean 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;
    }
    } private boolean createFileWriter()
    {
    try
    {
    m_FileWriter  = new FileWriter(m_File);
    return true;
    }
    catch(Exception e)
    {
    MyDebug.DEBUGEXCEPTION(this,"createFileWriter",e);
    return false;
    }
    } private boolean 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);
    }
    }
    }
      

  13.   

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

  14.   

    构造一个文件输出流,然后使用System类的setOut方法重置输出流即可。