The story behind println!

print stream and buffer read stream


Usually when I print, I often use statements such as System.out.println(). In java, it actually has a special print stream and a cache read stream. The following will describe the usage of the two streams in detail, 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();


Buffered read stream converts a character input stream into a buffered character read stream with a buffer 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 image description here

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

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

insert image description here

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324062946&siteId=291194637