Example 1:
package dataInputStreamAndPrintStreamDemo; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.io.PrintStream; //Demonstrate how to read strings from the keyboard and use the DataInputStream and PrintStream class to display program execution on the screen (standard output) public class DataInputStreamAndPrintStreamDemo { public static void main(String[] args) { int count; byte input[] = new byte[256]; String InputString; // Read DataInputStream stdin = new DataInputStream(System.in); // Improve execution efficiency, almost all InputStream classes can be wrapped by the BufferedStream class to improve I/O efficiency BufferedInputStream bufin = new BufferedInputStream(stdin); // Screen output DataOutputStream stdout = new DataOutputStream(System.out);// Output the result to the screen BufferedOutputStream bufout = new BufferedOutputStream(stdout);// Improve output efficiency PrintStream p = new PrintStream(System.out);// Output the result to the screen try { if (bufin.markSupported()) { p.println("Supported stream marks: Yes");// Use PrintStream to output p.println("Input string, press [Enter].../n" + "=>"); //Make the stream be marked on the first bit (mark), and 256 bits will be retained (mark (256)) bufin.mark(256); //Read bytes and store them in the specified array count = bufin.read(input); p.println("Read in the number of characters: " + count); p.print("The string you enter is: "); // Write to the stream, just write the data to the stream, not output the data// So after that, you must use the flush() function to force the output of the data in the stream bufout.write(input, 0, count); bufout.flush();// Force output to the specified output device bufin.reset();// Move the read position to the mark, that is, the first bit in the stream bufin.read(input, 0, count); p.print("first half of the string: "); bufout.write(input, 0, count / 2); //equivalent to System.out.println(); bufout.write((int)('/n')); bufout.flush(); bufin.reset(); bufin.skip(count / 2); bufin.read(input, 0, count / 2); p.print("second half of the string: "); bufout.write(input, 0, count / 2); bufout.flush(); } else { System.out.println("Stand Stream Tag: No"); } // Close Stream p.close(); stdin.close(); bufin.close(); stdout.close(); bufout.close(); } catch (IOException E) { System.out.println("I/O error occurred!!!"); } } } } //In fact, we should be very familiar with the PrintStream class. System.out is a PrintStream class object, and the print() and println() functions it provides // can display variables of almost all data types //Routine 2: package iotest; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintStream; import java.util.logging.Level; import java.util.logging.Logger; public class IOtest { public static void main(String[] args) throws IOException { byte buf[] = new byte[255]; byte bufin[] = new byte[255]; //The data can only be sent to the file in byte format String str = "Input text:"; buf = str.getBytes(); try { FileOutputStream fout = new FileOutputStream("test.txt"); PrintStream p = new PrintStream(fout); p.println("Input text~~~~~~~"+'/n'); //Method one fout.write(buf, 0, buf.length); //Method two fout.write(buf); //Method three //Fout.flush(); //Fout.close(); System.out.println("Quick input text:"); int bytes = System.in.read(bufin, 0, 255); //Append text!!!!!!!!!!!!!!!!!! //fout = new FileOutputStream("test.txt",true); fout.write(bufin, 0, bytes); } catch (FileNotFoundException ex) { Logger.getLogger(IOtest.class.getName()).log(Level.SEVERE, null, ex); } } }result:
//Input text~~~~~~~/Input text: Input text: Chenヤfdsfdsssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss
Summarize
The above is the entire content of this article about data stream instance code for Java stream operations. I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!