zoukankan      html  css  js  c++  java
  • Java如何将控制台上的结果保存到文件

    无论是二进制数据还是字符数据(文本数据),都可以用文件输出流java.io.FileOutputStream,以字节流的方式保存到指定文件。

    package test;

    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;

    class LogWrite{
    private static boolean fileLog = true;
    private static String logFileName = "E:/workspace/Java/result/result.txt";//指定程序执行结果保存的文件路径
    public static OutputStream getOutputStream() throws IOException{
    if(fileLog)
    {
    File file = new File(logFileName);
    if(!file.exists())
    file.createNewFile();
    return new FileOutputStream(file, true);
    }
    else
    {
    return System.out;
    }
    }
    public static void log(String info) throws IOException{
    OutputStream out = getOutputStream();
    out.write(info.getBytes("utf-8"));
    }

    }
    public class Test1 {

    public static void main(String[] args) throws IOException {
    // TODO Auto-generated method stub
    LogWrite.log("hello world");//"hello world"会保存到result.txt文件中
    }

    }

  • 相关阅读:
    RQNOJ 1 明明的随机数
    poj1284
    poj1061
    51nod1305
    51nod 1344
    poj2240
    poj1860
    使用SwitchToThisWindow时不切换问题
    c#拷贝整个文件夹到指定文件夹下(非递归)
    IniHelper
  • 原文地址:https://www.cnblogs.com/wanglin2016/p/5375345.html
Copyright © 2011-2022 走看看