IO Overview: The IO stream is used to process data transfer between the devices Java is streaming to the object that Java is used to manipulate the flow of objects in the IO packet stream by operation data are divided into two types: byte stream and character stream (encoded table) flow by flow into: input stream, output stream IO stream commonly used base class The basic operations of the stream in the Java.io package are: The abstract base class of the byte stream and the byte stream byte stream: InputStream: Flow outputstream into the program: the flow of the outflow program output data in the byte stream is mainly done using the OutputStream class, The input uses the InputStream class; the abstract base class of the character stream: Reader:writer: The output in character flow is mainly done by using Writer class, and the input mainly uses Reader class; Note: The subclass names derived from these four classes are the suffixes of the parent class name, such as: Parent class: InputStream-----Subclass: FileInputStream: Parent class: Reader------Subclass: FileReader in Java IO The main operating procedures are as follows: 1: Use the file class to open one of the files 2: Specify the location to output by a byte stream or a subclass of a character stream; 3: Read/write operation 4: Turn off input/output detailed byte stream-(outputstream)Byte stream mainly operates data of type OutputStream, the main operation class is the class of classes and InputStream.
1: Byte output stream according to API documentation: Public abstract class
OutputStreamExtends object implements Closeable, Flushable discovers that the OutputStream class belongs to an abstract class, and to use this class, it is necessary to instantiate objects with subclasses;
Common methods in the 2:outputstream class: Write one byte of data to the data stream: public abstract void
Write(int b) throws IOException writes a byte array to the data stream: public void
Write(byte[] b) throws IOException writes a byte array of a specified range to the data stream: public void
Write(byte[] B,int off,int len) throws IOException Refresh buffer: public void
Flush() throws IOException shutdown data stream: public void
Close() throws IOException
3: Construction method using the FileOutputStream subclass: Public
FileOutputStream(file file) The throws filenotfoundexception operation must receive an instance of the file class, indicating the path to be output;
Example 01: Requirements: Writing a string to a file//public byte[] getBytes (): Encodes this string into a byte sequence using the platform's default character set and stores the result in a new byte array; package cn.itcast03;/* * Writes one byte of data to the data stream: * Public abstract void Write (int b) throws IOException * writes a byte array to the data stream: * public void Write (byte[] b) thro WS IOException * writes a specified range of byte arrays to the data stream: * public void Write (byte[] b, int off,int len) throws IOException * Refresh buffer: * Public V OID Flush () throws IOException * Close Data flow: * public void Close () throws IOException */import Java.io.file;import java.io.FileOu Tputstream;import Java.io.ioexception;import Java.io.outputstream;public class FileOutputStream01 {public static void Main (string[] args) throws IOException {//Declares file object file F = new file ("A.txt"); Instantiate the parent class object by subclasses outputstream out = new FileOutputStream (f); Writes a String s = "I love JAVA"; byte[] bytes = S.getbytes (); Writes a byte array to the data stream: Out.write (bytes); Writes a specified range of byte arrays to the data stream: Out.write (bytes,1,5); Writes a byte of data to the data stream Out.write (' B '); Out.close (); }} instance 02:package cn.itcast03;import java.io.file;import java.io.fileoutputstream;import java.io.ioexception;//Append new content Public FileOutputStream (String Name,boolean append) throws filenotfoundexception//if the value of append is set to True, Indicates that the content is appended to the end of the file; public class FileOutStream03 {public static void main (string[] args) throws IOException { File File = new file ("B.txt"); FileOutputStream fos = new FileOutputStream (file,true); String s = "Hello world"; byte[] bytes = S.getbytes (); for (int i = 0; i < bytes.length; i++) {fos.write (bytes[i]); } fos.close (); }}
Detailed byte stream-(InputStream)View API Documentation: Public abstract class
InputStreamExtends Object implements closeable InputStream class: The size of the Access file: public int
available() throws IOException shutdown input stream: public void
Close() throws IOException reads a byte content and reads it digitally (the next byte of data is read from the input stream. Return
0
To
255
Within the range of
int
The byte value. If no bytes are available since the end of the stream has been reached, the return value
-1
) Public abstract int
Read() throws IOException reads the contents into a byte array and returns the number of read-in public int
Read(byte[] b) throws ioexception
Instance 01:package cn.itcast04;/* * Public abstract Int. read () throws IOException * public int read (byte[] b) throws IOException * /import java.io.file;import Java.io.fileinputstream;import java.io.ioexception;public class FileInputStreamDemo01 {p Ublic static void Main (string[] args) throws IOException {/*//File f = new file ("G:" +file.separator+ "Javat Est "+file.separator+" test01.txt "); File F = new file ("A.txt"); FileInputStream fis = new FileInputStream (f); int b; while ((B=fis.read ())!=-1) {System.out.println ((char) b); } fis.close (); */System. Out.println ("=============================================="); File F = new file ("A.txt"); FileInputStream Fis2 = new FileInputStream (f); /*//Read all content into the array byte[] bytes =new byte[1024]; Used to record the number of bytes read fis2.read (bytes); Fis2.close (); System.out.println (New String (bytes)); Problem: The array length is 1024, but the read array length is only 17 bytes, so there will be 1007 useless space to the string; Fix error is as follows: *//* * byte[] bytes = new byte[1024]; int Len; while ((Len=fis2.read (bytes))!=-1) {string s = new String (bytes,0, Len); System.out.println (s); } fis2.close (); The problem arises: Although the above specifies a byte array of the sample, but the program still opens up a lot of useless space, the above program does not fundamentally solve the problem. WORKAROUND: Use the Length () method provided by file to get the file size */byte[] bytes = new byte[(int) f.length ()]; int Len; If the value of Len is not-1, the file is not finished reading, while ((Len=fis2.read (bytes))!=-1) {string s = new String (bytes,0, Len); System. Out.println (s); } fis2.close (); }}
Javaio (03) byte stream--fileoutputstream