I. Flow overview
Flow is a set of ordered data series, according to the type of operation, can be divided into two types of input and output streams. The I/O stream provides a-bar channel program that can use this channel to send a sequence of bytes from the source to the destination, although the I/O stream is often related to disk file access, but the source and destination of the program can also be a keyboard mouse, memory, or display window.
Java is handled by the data flow input and output mode, the program reads the source data from the input stream that points to the source. The source can be a file, network, compressed package, or other data source.
The point of the output stream is the destination to which the data arrives, and the program passes the information to the destination by writing data to the output stream. The target of the output stream can be files, networks, compressed packets, consoles, and other data output destinations.
Two. Input/output stream I/O
1. Input stream
The InputStream class is an abstract class of byte input streams and is the parent class of all byte input streams.
A IOException exception is thrown when all methods in the class encounter an error. Here is a brief description of some of the methods in the class:
2. Input stream
The OutputStream class is an abstract class of byte input streams that represents the superclass of all classes that output a stream of bytes.
All methods in the OutputStream class return void, and an IOException exception is thrown when an error is encountered.
The writer class is an abstract class of character output streams, and the implementation of all character output classes is its subclass.
3. Features
The data is based on the flow direction of the data to determine whether the input or output stream.
Three. File construction
The file class is the only object in the IO package that represents the disk file itself. The file class defines a platform-independent way to manipulate files by invoking methods in the file class to create, delete, and rename files. The object of the file class is primarily used to obtain information about the files themselves, such as the directory where the file resides, the length of the file, the file read and write permissions, and so on. The data stream can write data to a file and the file is also the most commonly used data medium for the data flow.
1. Construction method
New file (full path)
or new file (path, filename)
2. Methods
(1) exists () judgment exists, (2) createnewfile () Create a new file, if the file already exists, will not overwrite the original file, (3) GetName get the file name; (4) Delete () remove; (5) Renameto (File object) Modify the path and file name.
Structure of File:
Import java.io.*; Public classTestfile { Public Static voidMain (string[] args) {//build the file instance and point to one of the files on the hard diskFile File =NewFile ("D:\\test.txt"); File=NewFile ("d:\\","Test1.txt"); //determine if a file exists if(File.exists ()) {System. out. println (File.getname () +"file already exists"); System. out. println (File.getabsolutepath ()); //renamingFile.renameto (NewFile ("G:\\text2.txt")); Try { if(File.createnewfile ()) {System. out. println ("Create file successfully"); } Else{System. out. println ("failed to create file"); } } Catch(IOException e) {System. out. println (E.getmessage ()); E.printstacktrace (); } //if (File.delete ())// {//System.out.println ("delete succeeded");// }//Else// {//System.out.println ("delete failed");// } } Else{System. out. println (File.getname () +"file does not exist"); //Create Try { if(File.createnewfile ()) {System. out. println ("Create file successfully"); } Else{System. out. println ("failed to create file"); } } Catch(IOException e) {System. out. println (E.getmessage ()); E.printstacktrace (); } } }}File
File input/output stream
While the program is running, most of the data is in memory and disappears when the program ends or shuts down. If you need to persist the data, you can use the file input and output stream to make a connection to the specified file and save the required data to the file permanently.
1. Byte input/output stream
Both the FileInputStream class and the FileOutputStream class are used to manipulate disk files. You can use the FileInputStream class if the user's file read requirements are relatively simple. The class inherits from the InputStream class. The FileInputStream class corresponds to the FileOutputStream class and provides basic file write capability. The FileOutputStream class is a subclass of the OutputStream class.
The common construction methods of the FileInputStream class are as follows:
FileInputStream (String name)
FileInputStream (File file)
The first construction method creates a FileInputStream object with the given filename name, and the second constructs a FileInputStream object using the file object. The first construction method is relatively straightforward, but the second construction method allows for further analysis of the file before it is connected to the input stream.
The FileInputStream class is constructed with the same parameters as the FileOutputStream class, and when you create a FileOutputStream object, you can specify a file name that does not exist, but this file cannot be a file that has been opened by another program.
Note: Although the Java language automatically closes all open streams at the end of a program, it is still a good practice to explicitly close any open streams after the flow is exhausted. An open stream has the potential to exhaust system resources, depending on the platform and implementation. If the open stream is not closed, these resources may not be able to be reached when another program tries to open another stream.
FileInputStream class and FileOutputStream class method code:
Package Com.hanqi;import java.io.*;p ublic class TestFile1 {public static void main (string[] args) {Fil E f1=new File ("D:\\test.txt"); if (f1.exists ()) {System.out.println ("File already exists"); Input/output d//Output text to file on hard disk try {//constructor party Method 1. Incoming file instance 2. Incoming file name FileOutputStream fo=new fileoutputstream (F1); String str= "Everybody good"; Write as a byte array byte[] B=str.getbytes (); Fo.write (b);//write Fo.close ();//Close output stream, release file Reads the text from the hard disk File//Constructs the method, indicates from which file reads into FileInputStream fi=new FILEINPUTST Ream (F1); Used to load the read data, specify a default size beforehand byte[] c=new byte[1024]; The return value indicates the length of the read-in data//incoming reference type int i=fi.read (c); Converts a string of the specified number of bits string Str1=new string (c,0,i); SYSTEM.OUT.PRINTLN ("read-in Content length =" +str1.length ()); SYSTEM.OUT.PRINTLN ("read-in content =" +str1); } catch (FileNotFoundException e) {System.out.println ("The file to be exported does not exist"); E.printstacktrace (); } catch (IOException e) {//TODO auto-generated catch block E.printstacktrace (); }} else {try {if (F1.createnewfile ()) { System.out.println ("File creation succeeded"); }} catch (IOException e) {e.printstacktrace (); } } }}FileInputStream, FileOutputStream 1
Package Com.hanqi;import java.io.*;p ublic class TestFile2 {public static void Main (string[] args) { try { c4/>//Overwrite write //true append write fileoutputstream fos=new fileoutputstream ("D:\\test.txt", true); String str= "\ n a good mood! "; Fos.write (Str.getbytes ()); Fos.close (); FileInputStream fis=new FileInputStream ("D:\\test.txt"); Byte[] B=new byte[200]; int I=fis.read (b); String Str1=new string (b,0,i); SYSTEM.OUT.PRINTLN ("Read content:" +STR1); Fis.close (); } catch (Exception e) { //TODO auto-generated catch block e.printstacktrace (); }}}
FileInputStream, FileOutputStream 2
2. Character input and output stream
There is little point in using the FileOutputStream class to write data to a file and to read the contents from a file using the FileInputStream class, which means that all two classes provide only a way to read a byte or byte array. Since Chinese characters occupy two bytes in the file, if you use a byte stream, it may be garbled to read poorly. This behavior can be avoided by using character stream FileReader or FileWriter classes.
FileReader, filewriter character stream corresponds to FileInputStream, FileOutputStream. The FileReader stream reads the file sequentially, as long as the stream is not closed. Each time the read () method is called, the rest of the content in the source is read sequentially until the end of the source or stream is closed.
Package Com.hanqi;import java.io.*;p ublic class TestFile3 {public static void Main (string[] args) { try { C3/>filereader fr=new FileReader ("D:\\test.txt"); Char[] C=new char[200]; int I=fr.read (c); String Str=new string (c,0,i); System.out.println ("read:" +str); Fr.close (); Write FileWriter fw=new FileWriter ("D:\\test.txt", true); Fw.write ("\ n new additions"); Fw.close (); } catch (Exception e) { //TODO auto-generated catch block e.printstacktrace (); }}}
FileReader, FileWriter
Input/output stream (I/O)