Io operation of the file:
byte stream:
input byte stream:
InputStream the base class for all input byte streams, abstract class.
FileInputStream reads the input byte stream of the file.
Bufferedinputstream buffer input byte stream. The interior of the class is a 8kb byte array maintained to improve the efficiency of reading files.
output byte stream:
OutputStream the base class for all output byte streams, abstract class.
The output byte stream of the FileOutputStream file.
Bufferedoutputstream buffered output byte stream. The interior of the class is a 8kb byte array maintained to improve the efficiency of writing files.
when to use a byte stream:
The data read does not need to be encoded or decoded to use a byte stream. Example: Picture data.
character Stream:
Character stream = character Stream + encode (decode)
input Character Stream:
Reader the base class for all input character streams, abstract class.
FileReader input character stream for reading file characters
The BufferedReader extension function (readline) maintains a character array of length 8192;
output character stream:
Writer the base class for all output character streams, abstract class.
FileWriter output character stream for output file characters
BufferedWriter buffered output character stream expansion function (newline);
when to use a character stream:
The number of bytes read needs to be converted into character data, when a character stream is used;
Conversion Flow:
Enter the conversion stream for the byte stream:
InputStreamReader
Conversion stream for output byte stream:
OutputStreamWriter
The role of the conversion stream;
1. The corresponding byte stream can be converted into a character flow using
2. Can specify the code table to read and write the data of the file;
FileReader, FileWriter These two classes by default use GBK encoded table, you can not specify the code table to read and write file data.
Practice:
Packagecom.java.base;ImportJava.io.BufferedReader;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;ImportJava.io.FileReader;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.InputStreamReader;ImportJava.io.OutputStreamWriter;/* byte stream: input byte stream: InputStream the base class for all input byte streams, abstract class. FileInputStream reads the input byte stream of the file. Bufferedinputstream buffer input byte stream. The interior of the class is a 8kb byte array maintained to improve the efficiency of reading files. Output byte stream: OutputStream the base class for all output byte streams, abstract class. The output byte stream of the FileOutputStream file. Bufferedoutputstream buffered output byte stream. The interior of the class is a 8kb byte array maintained to improve the efficiency of writing files. When to use a byte stream: The data read does not need to be encoded or decoded to use a byte stream. Example: Picture data. Character stream: Character stream = character Stream + encode (decode) input character stream: Reader all input character stream base class, abstract class. The input character stream BufferedReader extension function (readline) of the FileReader read file character maintains a character array of length 8192; the output character stream: The base class for all of the output character streams of Writer, Abstract class. FileWriter output character output character stream bufferedwriter buffered output character stream expansion function (newline); When to use a character stream: The number of bytes read needs to be converted to character data, and the character stream is used; Stream: The conversion stream of the input byte stream: The InputStreamReader of the output byte stream: The role of the outputstreamwriter conversion stream; 1. The corresponding byte stream can be converted into a character flow using 2. You can specify the code table to read and write the data of the file; FileReader, filewriter these two classes default to using GBK encoded tables, you cannot specify the code table to read and write file data. */ Public classIoexercise { Public Static voidMain (string[] args)throwsException {//Testinput (); //WriteFile ();ReadFile (); } Public Static voidTestinput ()throwsioexception{InputStream in=system.in; //int content = In.read ();//read one byte of data at a time//you need to convert the byte stream into a character flow usingInputStreamReader InputStreamReader =NewInputStreamReader (in); BufferedReader BufferedReader=NewBufferedReader (InputStreamReader); System.out.println (Bufferedreader.readline ()); } Public Static voidReadFile ()throwsexception{//The default use of GBK encoded table, so read garbled//FileReader filereader = new FileReader ("E:\\test\\a.txt");//int content = 0;//While (content = Filereader.read ())! =-1) {//System.out.print ((char) content);// }//Filereader.close (); //creating input data channels for files and programsFileInputStream FileInputStream =NewFileInputStream ("E:\\test\\a.txt"); InputStreamReader InputStreamReader=NewInputStreamReader (FileInputStream, "Utf-8"); intContent = 0; while(content = Inputstreamreader.read ())! =-1) {System.out.println (Char) content); } fileinputstream.close (); } //Utf-8 writes the data to the file. Public Static voidWriteFile ()throwsexception{//creating data channels for files and programsFileOutputStream FileOutputStream =NewFileOutputStream ("E:\\test\\a.txt"); //Create a conversion of output byte stream Liu and specify code table to write dataOutputStreamWriter OutputStreamWriter =NewOutputStreamWriter (FileOutputStream, "Utf-8"); Outputstreamwriter.write ("Good for everyone."); //Close ResourceOutputstreamwriter.close (); } }
/*
* Way one: Inherit thread
* 1. Customize a class to inherit the thread.
* 2. Rewrite the thread's Run method to define the task of the custom thread on the Run method.
* 3. Create the object of the thread subclass, and call the Start method to start another thread.
*
* Mode two: Implement Runnable interface
* 1. Customize a class to implement a runnable interface.
* 2. Implement the Run method in the Runnable interface and define the custom thread's task code within the Run method.
* 3. Create an object of the Runnable implementation class.
* 4. Create the thread object and pass the Runnable implementation class object as a parameter.
* 5. Call the start thread of the thread object.
*
* Thread Safety:
* 1. Synchronizing code blocks:
* Synchronized (lock) {
* Code that needs to be synchronized
* }
* 2. Synchronization function
* Modifier Synchronize returns a value type function name (parameter list: ){
*
* }
* The root cause of the emergence:
* 1. There must be two or more than two threads sharing a resource.
* 2. The code that operates the shared resource must have more than two sentences or more.
*
*/
Practice:
Packagecom.java.base;/** Way One: Inherit thread * 1. Customize a class to inherit the thread. * 2. Rewrite the thread's Run method to define the task of the custom thread on the Run method. * 3. Create the object of the thread subclass, and call the Start method to start another thread. * * Mode two: Implement Runnable interface * 1. Customize a class to implement the Runnable interface. * 2. Implement the Run method in the Runnable interface and define the custom thread's task code within the Run method. * 3. Create an object of the Runnable implementation class. * 4. Create the thread object and pass the Runnable implementation class object as a parameter. * 5. Call the start thread of the thread object. * Thread Safety: * 1. Sync code block: * Synchronized (LOCK) {* Code required to synchronize *} * 2. sync function * Modifier Synchronize returns a value type function name (parameter list: {* *} * The root cause: * 1. There must be two or more than two threads sharing a resource. * 2. The code that operates the shared resource must have more than two sentences or more. * */classAnotherthreadImplementsrunnable{@Override Public voidrun () { for(inti = 0; I < 100; i++) {System.out.println (Thread.CurrentThread (). GetName ()+ ": " +i); } }}classMyThreadextendsthread{ PublicMyThread (String name) {Super(name); } @Override Public voidrun () { for(inti = 0; I < 100; i++) {System.out.println (Thread.CurrentThread (). GetName ()+ ": " +i); } }} Public classThreadexercise { Public Static voidMain (string[] args) {MyThread MyThread=NewMyThread ("Threads that Inherit thread"); Mythread.start (); for(inti = 0; I < 100; i++) {System.out.println (Thread.CurrentThread (). GetName ()+ ": " +i); } anotherthread Anotherthread=NewAnotherthread (); Thread Thread=NewThread (Anotherthread, "threads that implement the Runnable interface"); Thread.Start (); }}
Java IO and threading exercises