Java stream operation data stream instance code
- 2020-12-10 00:42:27
- OfStack
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;
// Demonstrates how to read a string from the keyboard and use it DataInputStream,PrintStream Class displays 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 the keyboard
DataInputStream stdin = new DataInputStream(System.in);
// Improve execution efficiency, almost all of them InputStream Classes can be BufferedStream Such coated ( wrap ) to improve I/O The efficiency of
BufferedInputStream bufin = new BufferedInputStream(stdin);
// The screen output
DataOutputStream stdout = new DataOutputStream(System.out);// Output the results to the screen
BufferedOutputStream bufout = new BufferedOutputStream(stdout);// Improve output efficiency
PrintStream p = new PrintStream(System.out);// Output the results to the screen
try {
if (bufin.markSupported()) {
p.println(" Support for streaming tag: Yes ");// use PrintStream The output
p.println(" Enter the string and click [ Enter 】 ...\n" + "=>");
// Make the flow in the control 1 The ones place is labeled ( mark ) and will keep it 256 A ( mark ( 256 ))
bufin.mark(256);
// The read bytes are stored in the specified array
count = bufin.read(input);
p.println(" Number of characters read: " + count);
p.print(" The string you enter is: ");
// Write to stream. You just write data to stream, not output data
// So it must be used later flush The () function forces the output of 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 tag, which is the first in the stream 1 position
bufin.read(input, 0, count);
p.print(" The first half of the string: ");
bufout.write(input, 0, count / 2);
// The equivalent of System.out.println();
bufout.write((int)('\n'));
bufout.flush();
bufin.reset();
bufin.skip(count / 2);
bufin.read(input, 0, count / 2);
p.print(" The second half of the string: ");
bufout.write(input, 0, count / 2);
bufout.flush();
} else {
System.out.println(" String stream token: No ");
}
// Close the stream
p.close();
stdin.close();
bufin.close();
stdout.close();
bufout.close();
} catch (IOException E) {
System.out.println(" happen I/O Wrong!! ");
}
}
}
// In fact, we have PrintStream I should be familiar with the class, System.out is 1 a PrintStream Class object, which provides print() and println () function
// Variables of almost all data types can be displayed
// 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]; // Can only use byte The format feeds the data into the file
String str = " Input text :";
buf = str.getBytes();
try {
FileOutputStream fout = new FileOutputStream("test.txt");
PrintStream p = new PrintStream(fout);
p.println(" Input text ~~~~~~~"+'\n'); // way 1
fout.write(buf, 0, buf.length); // way 2
fout.write(buf); // way 3
//fout.flush();
//fout.close();
System.out.println(" Quick input :");
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);
}
}
}
Results:
// Input text ~~~~~~~
// Input text : Input text : � � � zichen ヤ � fdsfdssssssssssssssssssssssssssss
conclusion
That's the end of this article's data flow example code for Java flow operations, and I hope you found it helpful. Interested friends can continue to refer to other related topics in this site, if there is any deficiency, welcome to comment out. Thank you for your support!