Common classes
1, Node stream type
| Type |
Character Stream |
BYTE stream |
| File (Files) |
FileReader
FileWriter |
FileInputStream
FileOutputStream |
| Memory Array |
CharArrayReader
Chararraywriter |
Bytearrayinputstream
Bytearrayoutputstream |
| Memory String |
StringReader
StringWriter |
--- |
| Pipe (pipeline, thread) |
Pipedreader
PipedWriter |
PipedInputStream
PipedOutputStream |
accessing files
FileInputStream and FileOutputStream inherit from OutputStream and OutputStream, respectively, and are used to input and output bytes into the file.
FileInputStream and FileOutputStream are constructed by:
fileinputstream:fileinputstream (file file) creates a FileInputStream by opening a connection to an actual File, the FIL e Named by the file object file system. FileInputStream (FileDescriptor fdobj) creates a fileinputstream by using the file descriptor Fdobj, which represents an Exi Sting connection to a actual file in the file system. FileInputStream (String name) creates a fileinputstream by opening a connection to an actual file, the file named by the PAT H name name in the file system. Outputstream:fileoutputstream (file file) creates a file output stream to write to the file represented by the specified Fil E object. FileOutputStream (file file,Booleanappend) Creates a file output stream to write to the file represented by the specified file object. FileOutputStream (FileDescriptor fdobj) creates a file output stream to write to the specified file descriptor, which repres Ents an existing connection to a actual file in the file system. FileOutputStream (String name) creates a file output stream to write to the file with the specified name. FileOutputStream (String name,Booleanappend) Creates a file output stream to write to the file with the specified name.
The FileInputStream and FileOutputStream classes support the data read and write methods provided by their parent classes InputStream and OutputStream.
Attention:
The FileInputStream and FileOutputStream are instantiated with the Ty-catch statement, because FileNotFoundException can be thrown.
The Try-catch statement is also used when reading and writing data, as it is possible to throw IOException.
Example:
①,fileinputstream & FileOutputStream
FileInputStream:
ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;Importjava.io.IOException; Public classTest { Public Static voidMain (string[] args) {intb = 0; FileInputStream FIS=NULL; Try{FIS=NewFileInputStream ("D:\\share\\java\\io\\testfileinputstream.java"); } Catch(FileNotFoundException e) {System.out.println ("The specified file cannot be found"); System.exit (-1); } Try { Longnum = 0; while((b = Fis.read ())! =-1) {System.out.print (Char) b); Num++; } fis.close (); System.out.println ("Total read" + num + "bytes"); } Catch(IOException e) {System.out.println ("File read error"); System.exit (-1); } }}
Output: for Chinese garbled, because it is a byte of a byte read, so only half of the Chinese reading
ImportJava.io.*; Public classTestfileinputstream { Public Static voidMain (string[] args) {intb = 0; FileInputStream in=NULL; Try{ in=NewFileInputStream ("D:\\share\\java\\io\\testfileinputstream.java"); } Catch(FileNotFoundException e) {System.out.println ("Õò²»µ½ö¸¶¨îä¼þ"); System.exit (-1); } Try { Longnum = 0; while((B=in.read ())!=-1) {System.out.print (Char) b); Num++; } in.close (); System.out.println (); System.out.println ("¹²¶áè¡áë" +num+ "¸öxö½ú"); } Catch(IOException E1) {System.out.println ("Îä¼þ¶áè¡´íîó"); System.exit (-1); }}} read 700 bytes in total
FileOutputStream
Public Static voidMain (string[] args) {intb = 0; FileInputStream FIS=NULL; FileOutputStream Fos=NULL; Try{FIS=NewFileInputStream ("D:/share/java/helloworld.java"); FOS=NewFileOutputStream ("D:/share/java/io/hw.java")//Do not back automatically create a new file while((b = Fis.read ())! =-1) {fos.write (b); } fis.close (); Fos.close (); } Catch(FileNotFoundException e) {System.out.println ("The specified file cannot be found"); System.exit (-1); } Catch(IOException e) {System.out.println ("File copy Error"); System.exit (-1); } System.out.println ("File is copied"); }
②,filereader & FileWriter
FileReader:
Public Static voidMain (string[] args) {intc = 0; FileReader FR=NULL; Try{FR=NewFileReader ("D:\\share\\java\\io\\testfilereader.java"); while((c = Fr.read ())! = 1) {System.out.print (Char) c); } fr.close (); } Catch(FileNotFoundException e) {System.out.println ("The specified file cannot be found"); } Catch(IOException e) {System.out.println ("Error reading file"); } }
FileWriter (i)
Public Static voidMain (string[] args) {FileWriter fw=NULL; Try{FW=NewFileWriter (NewFile ("D:\\unicode.dat")); for(inti = 0; I <= 50000; i++) {//char max is 65535 fw.write (i); } fw.close (); } Catch(IOException e) {e.printstacktrace (); System.out.println ("File Write Error"); System.exit (-1); } }
Result: The contents of the Unicode.dat file in D disk:
FileWriter (b)
Public Static void throws Exception { new filereader ("D:/java/io/testfilewriter2.java"); New FileWriter ("D:/java/io/testfilewriter2.bak"); int b; while ((b = Fr.read ())! =-1) { fw.write (b); } Fr.close (); Fw.close (); }
Java IO (ii)