Differences between byte stream and byte stream
To output one piece of binary data to a device one by one, or read one piece of binary data from a device one by one, no matter what the input/output device is, we need to perform these operations in a unified manner and describe them in an abstract way. This abstract description method is named IO stream, and the corresponding abstract classes are OutputStream and InputStream, different implementation classes represent different input and output devices. They all operate on bytes. In applications, it is often necessary to output or read a piece of text with full characters. Can I use Word throttling? Everything in the computer eventually exists in a binary byte format. For the "China" character, first obtain the corresponding byte and then write the byte to the output stream. When reading, the first thing we read is the byte. But to display it as a character, we need to convert the byte into a character. Due to this wide range of needs, people have specialized in providing the packaging class of the volume stream. The underlying device always only accepts byte data. Sometimes, to write a string to the underlying device, you need to convert the string into bytes before writing it. The hidden stream is the packaging of byte streams, while the hidden stream directly accepts strings. It internally converts strings into bytes and then writes them to the underlying device, this provides a little convenience for writing or reading strings to IO settings. When converting a character to a byte, pay attention to the encoding problem. Because the conversion of a string into a byte array is actually a certain encoding byte form of the character, reading is also the opposite principle. Code example of the relationship between byte stream and byte stream: import java. io. bufferedReader; import java. io. fileInputStream; import java. io. fileOutputStream; import java. io. fileReader; import java. io. fileWriter; import java. io. inputStreamReader; import java. io. printWriter; public class IOTest {public static void main (String [] args) throws Exception {String str = "Chinese";/* FileOutputStream fos = new FileOutputStream ("1.txt"); fos. write (str. getBytes ("UTF-8"); fos. close (); * // * FileWriter fw = new FileWriter ("1.txt"); fw. write (str); fw. close (); */PrintWriter pw = new PrintWriter ("1.txt"," UTF-8 "); pw. write (str); pw. close ();/* FileReader fr = new FileReader ("1.txt"); char [] buf = new char [1024]; int len = fr. read (buf); String myStr = new String (buf, 0, len); System. out. println (myStr); * // * FileInputStream fr = new FileInputStream ("1.txt"); byte [] buf = new byte [1024]; int len = fr. read (buf); String myStr = new String (buf, 0, len, "UTF-8"); System. out. println (myStr); */BufferedReader br = new BufferedReader (new InputStreamReader (new FileInputStream ("1.txt"); String myStr = br. readLine (); br. close (); System. out. println (myStr );}}