Java Tour (24)--i/o stream, character stream, filewriter,ioexception, file continuation, FileReader, small exercise
The Java tour is also written more than 20, we finally reached the I/O today. If you beginner, do not understand the IO stream, you can look at slowly after the trip, but you work for a while you will find that the flow of the use of the scene and the technical point is very tough, we are bound to grasp this knowledge point, if you think the API is more clamor, see the video is not the essence, reading can not see the essentials, You follow my Java journey and explore it together!
I. I/O overview
I/O full name: Input output, meaning of inputs and outputs
- IO streams are used to process data transfer between devices
- Java operates on data in a flow-through way
- Java is used to manipulate the flow of objects are in the IO package
- There are two types of flow operations: byte stream, character stream
- Flow is divided into: input stream, output stream
The operation of the data, in fact, is file files, we can go online to steal a picture to describe our large series of all the knowledge points
Images from the Web
As you can see, they are all subclasses derived from these four classes, but their suffixes are the four
Two. FileWriter
Let's start with the character stream, definitely from the subclass object, and we'll start with the file operation!
- Requirement: Create a file on the hard disk and write the data
So how do we do that? He's more of a constructor, we look at examples
PackageCom.lgl.hellojava;ImportJava.io.FileWriter;ImportJava.io.IOException; Public class Hellojjava { Public Static void Main(string[] args) {/** * Requirements: Create a file on the hard disk and write the data */ //One is initialized and must have the file to be manipulated //If you do not specify a location, create it in the same directory //If a file with the same name exists under the directory, overwrite Try{FileWriter FileWriter =NewFileWriter ("Test.txt");//write data to memoryFilewriter.write ("ABCDE");//Refresh the buffer of the stream //Filewriter.flush (); //Close stream is flushed before it is closed, and flush is the difference between flush refresh and flow can continue to workFilewriter.close (); }Catch(IOException e) {//TODO auto-generated catch blockE.printstacktrace (); } }}
This will allow you to see the generated files in the root directory of our project.
I say it again in the vernacular, in fact, is to create filewriter, he has no empty constructor, you create a file, you can pass the filename or path, and then Wirter write the data, so you can not see, you need to refresh, refresh is to refresh the buffer, you now see, throw the exception, and close, It will refresh before it is closed, but this stream is useless, analyze it according to your own scene
Three. IOException,
Let's see how to deal with the exception of Io, IO exception is roughly three, one is IO exception, one is not found file exception, there is no object exception, we are more rigorous wording
PackageCom.lgl.hellojava;ImportJava.io.FileWriter;ImportJava.io.IOException; Public class Hellojjava { Public Static void Main(string[] args) {FileWriter FileWriter =NULL;Try{FileWriter =NewFileWriter ("Demo.txt"); }Catch(IOException e) {//TODO auto-generated catch blockE.printstacktrace (); }finally{Try{if(FileWriter! =NULL) {filewriter.close (); } }Catch(IOException e) {//TODO auto-generated catch blockE.printstacktrace (); } } }}
Four. File continuation
We know that the file will be overwritten if it exists, but we do not want to, we want to continue in the original data, how to do?
PackageCom.lgl.hellojava;ImportJava.io.FileWriter;ImportJava.io.IOException; Public class Hellojjava { Public Static void Main(string[] args) {Try{//Parameter 2 means no overwrite of existing files, support for continuationFileWriter FileWriter =NewFileWriter ("Demo.txt",true); Filewriter.write ("Hello"); Filewriter.close (); }Catch(IOException e) {//TODO auto-generated catch blockE.printstacktrace (); } }}
When the parameter is constructed, it is set to true to continue writing the file.
Five. FileReader
Now that we've written it, we're going to read it.
PackageCom.lgl.hellojava;ImportJava.io.FileNotFoundException;ImportJava.io.FileReader;ImportJava.io.IOException; Public class Hellojjava { Public Static void Main(string[] args) {Try{//Create a file to read the stream object, and specify the name of the file association, to ensure that the file exists, //If not present, the exceptionFileReader FileReader =NewFileReader ("Demo.txt");//Read a single character, automatically read down intcd = Filereader.read (); System.out.println ((Char) (CD);//Print all intCH =0; while(ch = filereader.read ())! =-1) {System.out.println (CH); } filereader.close (); }Catch(FileNotFoundException e) {//TODO auto-generated catch blockE.printstacktrace (); }Catch(IOException e) {//TODO auto-generated catch blockE.printstacktrace (); } }}
So that it can be read in bytes, we can also store the read characters in the array
PackageCom.lgl.hellojava;ImportJava.io.FileNotFoundException;ImportJava.io.FileReader;ImportJava.io.IOException; Public class Hellojjava { Public Static void Main(string[] args) {Try{FileReader FileReader =NewFileReader ("Demo.txt");Char[] buf =New Char[3];intnum = Filereader.read (BUF); System.out.println ("num:"+ num +NewString (BUF)); }Catch(FileNotFoundException e) {//TODO auto-generated catch blockE.printstacktrace (); }Catch(IOException e) {//TODO auto-generated catch blockE.printstacktrace (); } }}
OK, I read it.
Six. Little Practice
The reading of our character stream and some small operations are a little bit more, let's end this space with a little exercise.
- Requirement: Read a. java file and print it out
OK, actually this is relatively simple, we look at the code
PackageCom.lgl.hellojava;ImportJava.io.FileNotFoundException;ImportJava.io.FileReader;ImportJava.io.IOException; Public class Hellojjava { Public Static void Main(string[] args) {Try{FileReader FileReader =NewFileReader ("Single.java");Char[] cs =New Char[1024x768];intnum =0; while(num = Filereader.read (cs))! =-1) {System.out.println (NewString (CS,0, num)); } filereader.close (); }Catch(FileNotFoundException e) {//TODO auto-generated catch blockE.printstacktrace (); }Catch(IOException e) {//TODO auto-generated catch blockE.printstacktrace (); } }}
is not simple, after reading to directly exist in the array, print out
OK, here we get to know a little bit of Io, but this is not enough, we should continue to drill down, we continue to follow the IO, please look forward to!
Welcome Dabigatran: 555974449, we explore together!
Java Tour (24)--i/o stream, character stream, filewriter,ioexception, file continuation, FileReader, small exercise