Examples of common input/output flows in java and examples of java input/output flows
Byte input stream:
1. FileInputStream
Purpose: Obtain input bytes from files in the file system. It is often used to read original byte streams such as images and sounds. to read the audio stream, consider using FileReader.
For more information about constructors and common methods, see the API documentation. The Chinese version of API is already available on the Internet. I am an E-literate and cannot afford to hurt.
Here is the most common method:
read(byte[] b, int off, int len)
Maximum number of input streamslen
Bytes of data is read into a byte array.
-> Off: the offset in the B-byte array.
TIPS: array offset, for example, a [1, 2, 3, 4, 5] array. By default, the first array should point to a [0]. If the offset is 2, it will point to a [1].
Case:
Import java. io. fileInputStream; import java. io. fileNotFoundException; import java. io. IOException; public class Test {/*** @ param args */public static void main (String [] args) {// TODO Auto-generated method stubtry {int B; // create the input stream FileInputStream readfile = new FileInputStream ("E: \ test.txt"); byte buffer [] = new byte [2500]; // create a byte array // read bytes from the input stream and store them in the buffer array. The maximum length is 2500 bytes. The returned value B is the actual read length B = readfile. read (buffer, 1, 2000); // 1 is the starting offset of the buffer array String str = new String (buffer, 0, B, "Default"); System. out. println (str); System. out. println (B); readfile. close ();} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke. printStackTrace ();} catch (IOException e) {// TODO Auto-generated catch blocke. printStackTrace ();}}}
There are still many frequently used stream operations to be continued, which will be supplemented later. it's very late. it's time to go to bed. I can do it every day !!
How can I understand the input and output streams in java? I have never understood the principle.
The byte stream processing unit is two bytes of Unicode characters, operating characters, character arrays or strings respectively, while the byte stream processing unit is one byte, operating byte and byte array. Therefore, the compaction stream is a character that is converted from a Java virtual machine to a Unicode Character in two bytes. Therefore, it is highly supported by many languages! For audio files, images, and songs, use text to reduce the cost. For Chinese (text) files, use text to improve the streaming speed.
All files are stored in bytes. What is stored on the disk is not the character of the file, but the characters are first encoded into bytes, and then stored on the disk. When reading a file (especially a text file), it is also a byte read to form a byte sequence.
Byte streams can be used for any type of objects, including binary objects. The byte stream can only process characters or strings. 2. A byte stream provides the ability to process any type of IO operations, but it cannot directly process Unicode characters, while a bytes stream can.
Use of java input/output streams
Import java. io .*;
Public class pipel {
Char c [] = new char [100];
Public void change (){
For (int I = 0; I <6; I ++ ){
If (c [I] = 'A '){
C [I] = 'a ';
}
}
}
Public pipel (){}
Public void connection (){
Try {
File f = new File ("text1.txt"); File fc = new File ("text2.txt ");
FileInputStream in = new FileInputStream (f );
InputStreamReader ir = new InputStreamReader (in );
Ir. read (c );
} Catch (Exception e) {e. printStackTrace ();}
}
Public void push (){
Try {
File fc = new File ("text2.txt ");
FileOutputStream out = new FileOutputStream (fc );
OutputStreamWriter ow = new OutputStreamWriter (out );
Ow. write (c );
Ow. close ();
} Catch (Exception e) {e. printStackTrace ();}
}
Public static void main (String [] args ){
Pipel k = new pipel ();
K. connection ();
K. change ();
K. push ();
}
}