Java.io Package: Provides system input and output via data flow, serialization, and file system
Below to understand what is IO stream?
The IO stream is used to process data transfer between devices. The Java operation of data is divided into the input stream and the output stream by the way of flow, divided into byte stream and character stream according to Operation data.
--------------------------------------------------------------------------------------------------------------- ----------------------
The following describes the character stream class, the base class of known character streams is Reader, Writer
Known subclasses: BufferedReader--------buffered stream--------BufferedWriter
CharArrayReader--------Character array flow--------Chararraywriter
FilterReader--------Filter Flow--------Fileterwriter
InputStreamReader--------Conversion Stream--------Inpustreamwriter
Pipedreader--------Pipe Flow--------PipedWriter
StringReader--------String Stream--------StringWriter
FileReader--------file stream--------FileWriter------------------------------------------------------- ------------------------------------------------------------------------------
The following is a read demo of the file through the FileReader file stream:
Import java.io.*;
Class Readerwriter
{/
* is known to have a Test text file under the current file, written by the FileWriter file character stream *
/public static void main (string[] args) throws IOException {
/* 1. Using IO streams will cause a Java exception system to be caught, and the more common exceptions IOException need to be captured (not dealt with first, thrown away)
2. When you create a FileReader object, you need to bind a file object that does not exist and it throws java.io.FileNotFoundException (the system cannot find the file specified.) )
*
/FileReader fr = new FileReader ("11.txt");
char[] buf = new char[1024]; Defines a character buffer,
int len = 0; Used to record file length while
(len = Fr.read (BUF))// read data through read, and to deposit the read data into the character buffer,
{ // After reading, read returns the remaining length of the file, recorded with Len, to determine if the file has been read.
System.out.println (New String (buf, 0, Len)); Prints the data that is stored every time.
}
Fr.close (); Because the FileReader is called the window resource, the resource needs to be freed through the Close method. Because the resources are limited,
}
}
The following is a presentation of the file through the FileWriter file stream:
Import java.io.*;
Class Readerwriter
{/
* is known to have a Test text file under the current file, written by the FileWriter file character stream *
/public static void main (string[] args) throws IOException {
/* 1. Using IO streams will cause a common exception IOException in the Java exception system, which needs to be captured (not dealt with, thrown out)
2. When you create a FileWriter object, you need to bind a file object, the file does not exist, the file object is created under the current file path, and the current file object is overwritten,
3. You can create a filewriter, tell it not to overwrite, you want to continue to write the file example: New FileWriter ("Test", true);
*
/FileWriter FW = new FileWriter ("test.txt");
for (int i = 1; i<6; i++)
{
fw.write ("+i+" write data \ r \ n);//write data by writing method, note that the data he writes is in a buffer, not in the destination file.
Fw.flush (); The flush method is required to flush the buffer, so that the data is written to the destination file in hundreds of minutes.
}
/*
Here it is important to note that the flush method is also called at the bottom of the Close method, which makes a mistake, anyway close will call the Flush method,
so I don't have to write the flush method at all. This is wrong. Because flush is an instant refresh, which guarantees the validity of the data, if a power outage occurs during the
write process, the program does not execute to the Close method, and the previous write data is lost.
So here is suggested, write once, refresh once.
*
/Fw.close (); Because the filewriter is called the window resource, the resource needs to be freed through the Close method. Because the resources are limited,
}
}
Using the IO stream will cause a common exception in the Java exception System IOException
The following demonstrates common IO flow exception handling:
Import java.io.*;
Class Readerwriter {/* for IO stream capture processing, we need to separate them for try...catch processing.
Each kind of anomaly is different in the way it is. 1. Handling of File Objects 2. Processing of read-write Data 3.
Turns off resource processing on a file object.
*/public static void main (string[] args) {write ();
Read ();
The public static void read () {/* below demonstrates catching exception handling for reading a stream of characters */filereader FR = null;
try {fr = new FileReader ("test.txt"); } catch (FileNotFoundException e) {throw new RuntimeException ("The file does not exist.
"); }
char[] buf = new char[1024];
int len = 0;
try {while (len = Fr.read (buf))!=-1) {System.out.println (new String (buf, 0, Len)); }} catch (IOException e) {throw new RuntimeException ("file read error.
"); }
Finally {try {if (fr!=null) fr.close (); } catch (IOException e) {throw new RuntimeException ("file read stream shutdown failed.
"); }
}
}
public static void Write () {/* below demonstrates the capture exception handling for writing a stream of characters */FileWriter FW = NULL;
try {fw = new FileWriter ("Test.txt", true); } catch (IOException e) {throw new runtimeexceptIon ("Path error.
"); }
try {fw.write ("write Data");
Fw.flush (); } catch (IOException e) {throw new RuntimeException ("Data write error.
"); }
Finally {try {if (fw!=null) fw.close (); } catch (IOException e) {throw new RuntimeException ("File write stream shutdown failed. "); }
}
}
}