The story behind println!

Print stream and cache read stream


When I usually print, I often use statements such as System.out.println(). In Java, it actually specializes in the printing stream and the cache reading stream. The following will specifically describe the usage of the two streams, and then give You can use printing to collect logs.

Byte output (print stream)

//字节输出 (打印流)
PrintStream printStream =new PrintStream("D://c.txt");
printStream.println("我是哆啦A浪");
printStream.println("我是哆啦A浪");
printStream.println("我是哆啦A浪");

PrintWriter printWriter =new PrintWriter("D://c.txt");
printWriter.println("我的是发顺丰");
printWriter.flush();
printWriter.close();

Convert byte stream to print stream

//字节输出 (打印流)
FileOutputStream fileOutputStream =new FileOutputStream("D://c.txt");
PrintWriter printWriter1 =new PrintWriter(fileOutputStream);
printWriter1.println("我");
printWriter1.println("是");
printWriter1.println("你");
printWriter1.flush();


Cached read stream converts the character input stream into a cached character read stream with cache that can be read one line at a time

//缓存读取流 将字符输入流 转换为带缓存 可以一次读一行的缓存字符读取流FileReader fileReader =new FileReader("D://c.txt");BufferedReader bufferedReader =new 
FileReader fileReader =new FileReader("D://c.txt")
;BufferedReader bufferedReader =new BufferedReader(fileReader);
String text =bufferedReader.readLine();
System.out.println(text);
bufferedReader.close();


Collect exception logs

Now that you have learned how to use the print stream, let's use it for daily collection.

try {
    
       
String s =null;  
s.toString();
}catch (Exception e){
    
      
PrintWriter printWriter =new 
PrintWriter("D://c.txt");   
SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy-MM-dd HH:mm");    printWriter.println(simpleDateFormat.format(new Date()));    
e.printStackTrace(printWriter);    
printWriter.close();
}

The effect is as follows:
Insert picture description here

Note: There is a special class library for log collection in the later period, and the above code is for learning only

When you see this, don't be stingy with the likes in your hand, give a free like and follow! If you have any questions, you can contact Xiaolang: [email protected], or private message.

Insert picture description here

Guess you like

Origin blog.csdn.net/AzirBoDa/article/details/113604282