“This is the 12th day of my participation in the August More Text Challenge. For details, see: August More Text Challenge.”
The vast sea of thousands of thousands, thank you for this second you see here. Hope my article is helpful to you!
Wish you in the future, keep love, go to the mountains and seas!
Senior stream I/O
Yesterday we learned about conversion streams in advanced streams, and in the larger system of IO streams, there are some advanced streams waiting to be unlocked.
So without further discussion, today we’re going to look at one of these advanced streams — print streams
Printing flow
I don’t know if you noticed, but when we print on the console, we call the print() method and the println() method, and they all come from the Java.io.printStream class, which is also an IO stream, so it’s easy to print values of various data types. Is a convenient way to output.
classification
The print stream has only the output stream, which is divided into:
-
Byte printStream: printStream.
-
Character print stream: printWriter.
In the specific use of both methods, the basic similar.
Features of the print stream
- Only the destination, not the data source.
- You can manipulate any type of data.
- If automatic refresh is enabled, in the call
println()
Method, can be newline and refresh. - You can manipulate text files directly.
PrintStream
1. Construction method
-
Public PrintStream(String fileName) : creates a new PrintStream with the specified fileName.
For example, the code is as follows:
PrintStream ps = new PrintStream("e:\demo\ps.txt"); Copy the code
2. Print to a file
We also often see system.out.println (), which is printStream, but its direction is System specified. It is printed on the console. However, since we are streaming objects, we can play with another feature that outputs data to a specified text file.
public class PrintStreamDemo { public static void main(String[] args) throws FileNotFoundException { // Call the System print stream, console directly output 97 system.out.println (97); Ps = new PrintStream("e:\demo\ps.txt"); // Set the output stream to ps.txt system.setout (ps); // Call the System print stream,ps.txt output 97 system.out.println (97); }}Copy the code
You can see that the console only prints a 97, so where is the 97 printed? Look at the ps.txt file, and you can see that the 97 is printed in the file.
3. Copy file cases
public class PrintStreamDemo2 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("e:\demo\ps.txt"));
PrintStream ps = new PrintStream("e:\democopy\psCopy.txt");
String line;
while ((line = br.readLine()) != null) {
ps.println(line);
}
br.close();
ps.close();
}
}
Copy the code
Program execution result:
PrintWriter
1. Construction method
-
Public PrintWriter(String fileName) : creates a new print stream with the specified fileName.
For example, the code is as follows:
PrintWriter pw = new PrintWriter("e:\demo\pw.txt"); Copy the code
2. Copy file cases
public class PrintWriterDemo {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("e:\demo\ps.txt"));
PrintWriter pw = new PrintWriter("e:\democopy\pwCopy.txt");
String line;
while ((line = br.readLine()) != null) {
pw.println(line);
}
br.close();
pw.close();
}
}
Copy the code
Program execution result:
The nature of the standard output stream
-
How do output statements work and how to output data using character streams
Look directly at a piece of code first:
Public class SystemOutDemo {public static void main(String[] args) {public class SystemOutDemo {public static void main(String[] args) { //public final static PrintStream out = null; System.out.println("helloworld"); PrintStream ps = system.out; ps.println("helloworld"); }} Program execution results: printed on the console: helloWorld HelloWorldCopy the code
-
Nature:
There is a public final static PrintStream out = null under the System class that can return a PrintStream object.
So an output statement is essentially an I/O stream operation that outputs data to the console.
conclusion
I believe that you have a certain understanding of the IO stream advanced streams in the print stream class, looking forward to the next chapter of advanced streams – serialization teaching!
Of course, there are many streams waiting to watch together next time! Welcome to the next chapter!
So far, the world is closed for today, good night! Although this article is over, BUT I still, never finished. I will try to keep writing articles. The coming days are long, and the horse is slow!
Thank you for seeing this! May you be young and have no regrets!
Note: If there are any mistakes and suggestions, please leave a message! If this article is also helpful to you, I hope you give a lovely and kind attention, thank you very much!