Java's IO class operations mainly include the following categories
1, the use of the file class.
2, byte operation flow: OutputStream, InputStream
3, character operation Flow: Reader, Writer
4. Serialization of objects: Serializable
(1) File class Java code
public class File extends Object implements Serizliable comparable<file>
From the definition, the file class is a direct subclass of object, and it inherits the comparable interface to sort the array.
The operations of the file class include file creation, deletion, renaming, getting paths, creation times, and so on, and the following are commonly used functions for file operations.
Operation of the file class:
(1) Create the file, note that file.separator can solve the problem across the operating system.
The following example is the creation of a file that is deleted if the file exists, otherwise the file is created. Java Code public class filedemo1 { public static void main (String[] args) { File File = new file ("D:" + File.separator + "test.txt"); if (file.exists ()) { file.delete (); } else { try { file.createnewfile (); } catch ( IOException e) { // todo auto-generated catch block e.printstacktrace (); } } } }
(2) The type function of the file
File.isfile (); Judge is not a file
File.isdirectory ()//To determine whether the directory
(3) listing the contents of the catalog
Pulbic string[] List ()//list all file names and directory names
Public file[] Listfiles ();//list all files and directories
(2) byte operation Flow (Btyle)
(1) Byte output stream OutputStream
Java code public class filedemo1 { public static void main (String[] args) { File file = new file ("D:" + File.separator + "test.txt");//Specify the file to be manipulated outputstream out=null;//defines a byte stream output object try { //out= new fileoutputstream (File,true)//Whether byte append function out= new fileoutputstream ( file)//Gets the actual byte stream output object, content coverage } catch ( filenotfoundexception e) { e.printStackTrace (); } string info= "Hello";//What to enter byte[] b=info.getbytes ()//convert character to byte array try { out.write (b); } catch (ioexception e) { e.printstacktrace (); } try { Out.close (); } catch (ioexception e) &NBsp { e.printstacktrace ();  } } }
(2) byte input stream InputStream
Java Code public class filedemo1 { public static void main (String[] args) { File File = new file ("D:" + File.separator + "test.txt");//Specify the file to manipulate inputstream in=null;//definition byte Stream input object try { //out= new fileoutputstream (File,true)//Whether byte append function in= new fileinputstream (file);// Gets the actual byte stream input object } catch ( filenotfoundexception e) { e.printstacktrace (); } int len=0;//input array length byte[] b= new byte[1024];//open space, read content //byte[] b=new byte[(int) file.length ()];//open space based on file size try { len=in.read (b);//Read } catch (ioexception e1) { e1.printstacktrace (); } try { In.close (); } catch (ioexception e) { e.printstacktrace (); } system.out.println (new string (B,0,len)); } }
(3) Character output stream write
Java Code public class filedemo1 { public static void main (String[] args) { File File = new file ("D:" + File.separator + "test.txt");// specifies the file to be manipulated Writer write = null;// define character output flow try { write = new filewriter (file); } catch (ioexception e) { e.printstacktrace (); } String infor = "Hello,heiehiehieh"; try { write.write (infor); } catch (ioexception e) { e.printstacktrace (); } try { Write.close (); } catch (ioexception e) { e.printstacktrace ();  } } }
(4) character input stream reader
Java code public class filedemo1 { public static void main (String[] args) { File file = new file ("D:" + File.separator + "test.txt");// Specify the files to be manipulated Reader read = null;// Define character input flow try { read = new filereader (file); } catch (ioexception e) { e.printstacktrace (); } string infor = "Hello,heiehiehieh"; char[] b=new char[1024];//Set the length of the character try { int len=read.read (b); } catch (ioexception e) { e.printstacktrace (); } try { Read.close (); } catch (ioexception e) { e.printstacktrace (); } } }
(5) The difference between the byte stream and the character streams (emphasis)
The byte stream has no buffer, is output directly, and the character streams are output to the buffer. Therefore, when the byte stream does not invoke the Colse () method at the time of the output, the message is output, and the message is output only when the close () method is invoked to turn off the buffer. To want the character stream to output information when it is not closed, you need to manually invoke the flush () method.
(6) Conversion flow: In IO There is a class of conversion flow, the stream of bytes into the character stream, while the character can be converted into a byte stream.
OutputStreamWriter (OutStream out): J outputs the stream of bytes in character streams.
InputStreamReader (InputStream in): Converts a stream of bytes into the character streams.
(7) Print stream PrintStream
When you require output information in an operation, you can use PrintStream for output, which includes Printwrite and Printreader
(3) serialization of objects
object serialization refers to the conversion of an object into binary byte streams that can be saved as files.
The operation of saving an object in a file is called an object's serialization operation.
The operation of recovering an object from a file is called a deserialization operation.
An object must inherit serizliable if it is to be serialized. When implementing serialization, it requires objectourputstream completion, and ObjectInputStream when deserialization is required.
Transient keyword: After a variable is declared as transient, the variable is not serializable.
(4) Memory stream
In the project development process, sometimes want to generate only temporary files, the information output in memory, the memory flow, the basic method of memory flow is as follows:
Java code public class filedemo1 { public static void main (String[] args) { string infor = "Hello"; // all content to input in memory inputstream input = new bytearrayinputstream (Infor.getBytes ( )); // all memory content by OutputStream output outputstream out = new bytearrayoutputstream (); int temp = 0; try { while ((Temp = input.read ()) ! = -1) { char c = character.touppercase ((char) temp); Out.write (c);//output from memory, all content stored in Bytearrayoutputstream &NBSP;&NBSP;&NBSP;&NBSP;&NBSP} } catch ( ioexception e) { E.printstacktrace (); } try { Input.close (); } catch (ioexception e) { e.printstacktrace (); } try { Out.close (); } catch (ioexception e) { e.printstacktrace (); } system.out.println (Out.tostring ()); } }
(5) System class support for IO
(6) Cache read