In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "the example analysis of the principle of Java print stream". In the daily operation, I believe that many people have doubts about the analysis of the principle of Java print stream. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts of "principle example analysis of Java print stream". Next, please follow the editor to study!
Usually we print the output in the console, which is completed by calling the print method and the println method, both of which come from the java.io.PrintStream class, which can easily print values of various data types, which is a convenient way to output data.
PrintStream class
The PrintStream class, which adds functionality to other output streams, enables them to easily print various data value representation formats.
The characteristics of the PrintStream class:
Only responsible for data output, not responsible for data reading.
Unlike other output streams, PrintStream never throws an IOException exception.
Unique methods: print (), println ()
Construction method
PrintStream (String fileName) / / creates a new print stream with the specified file name.
Methods inherited from the parent class
PrintStream extends OutputStream
1. Public abstract void write (int b) throws IOException;// will output the specified byte stream. 2. Public void write (byte b []) throws IOException {...}; / / writes b.length bytes from the specified byte array to this output stream. 3. Public void write (byte b [], int off, int len) throws IOException {...}; / / writes len bytes from the specified byte array and outputs to this output stream starting with the offset off. 4. Public void flush () throws IOException {}; / / refresh this output stream and force any buffered output bytes to be written out. 5. Public void close () throws IOException {}; / / close this output stream and release any system resources associated with this stream.
Description:
If you use the write method that inherits the parent class to write the data, the coding table is queried when the data is queried.
If you use your own unique print method, println method to write data, then the written data will be output as is.
Example: write data to an empty print.txt file
Import java.io.FileNotFoundException;import java.io.PrintStream;public class DemoPrintStream {public static void main (String [] args) throws FileNotFoundException {/ / create the PrintStream object, pass in the output path, the path needs to exist, otherwise the FileNotFoundException exception PrintStream ps = new PrintStream ("/ Users/liyihua/IdeaProjects/Study/src/view/study/demo37/print") will be thrown; / / use the write method of the parent class OutputStream to write the data ps.write (97) / / use your own unique method to write data ps.print (97); / / release resource ps.close ();}}
The print.txt file is as follows:
Data an is written using the write method in the parent class. Data 97 is written using its own unique method.
Exercise: change the destination of the output statement
Analysis: output statement, the default is output in the console, using the System.setOut method, you can change the destination of the output statement.
Static void setOut (PrintStream out) / / changes the destination of the output statement to the destination of the print stream passed in the parameter. Parameter: PrintStream out: print stream object
Code implementation:
Import java.io.FileNotFoundException;import java.io.PrintStream;public class DemoSetOut {public static void main (String [] args) throws FileNotFoundException {/ / output System.out.println on the console ("I output it on the console!") ; / / output PrintStream ps = new PrintStream ("/ Users/liyihua/IdeaProjects/Study/src/view/study/demo37/setOut"); System.setOut (ps); System.out.println ("I exported it in the setOut.txt file!") in an empty setOut.txt file ; / / release resource ps.close ();}}
Console output:
I output in the console!
Contents of setOut.txt file:
I output it in the setOut.txt file!
At this point, the study of "Java print stream principle example analysis" is over. I hope to be able to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.