Print stream and cache read stream


Println () : println() : println() : println() : println() : println() : println()

Byte output (print stream)

// Byte output (print stream)
PrintStream printStream =new PrintStream("D://c.txt");
printStream.println("I am Doralang.");
printStream.println("I am Doralang.");
printStream.println("I am Doralang.");

PrintWriter printWriter =new PrintWriter("D://c.txt");
printWriter.println("Mine is SF Express.");
printWriter.flush();
printWriter.close();
Copy the code

Turn a byte stream into a print stream

// Byte output (print stream)
FileOutputStream fileOutputStream =new FileOutputStream("D://c.txt");
PrintWriter printWriter1 =new PrintWriter(fileOutputStream);
printWriter1.println("我");
printWriter1.println("Yes");
printWriter1.println("You");
printWriter1.flush();


Copy the code

Cache read stream converts a character input stream to a cache 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();


Copy the code

Collecting Exception Logs

Now that you know how to use print streams, let’s use them for your 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();
}
Copy the code

The effect is as follows:

Note: there is a library dedicated to log collection later, and the code above is for learning only

See here, don’t be stingy hand like, give a free like and attention! If you have any questions, you can contact Xiao Lang :[email protected], or private message.