Whether it is two-level data or character data (text data), the file output stream Java.io.FileOutputStream can be saved to the specified file by byte stream.
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";//Specifies the file path to which the program execution results are saved
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" is saved to the Result.txt file
}
}
How Java saves the results from the console to a file