ImportJava.io.*;/*** Created by CLY on 2017/7/23.*/ Public classMain { Public Static voidMain (string[] arg) {testfile (); Testfilenamefilter (); Testinputstream (); Testoutstream (); Testcopyimg (); } /*** File class: * Can represent the "name" of a particular file, or the "name" of a set of files under "one directory". */ Public Static voidtestfile () {//used as a "directory listing"File File =NewFile ("./");//current directoryString list_url[] =file.list (); for(String one_url:list_url) {System.out.println (One_url); } } /*** Dirfilter Interface: * This interface only specifies an accept () method. * We can override this method by using an anonymous inner class. * The function of this method is: Determine the incoming string file name, whether it exists in the list of incoming directories*/ Public Static voidTestfilenamefilter () {File file=NewFile ("./"); FilenameFilter FilenameFilter=NewFilenameFilter () { Public BooleanAccept (File dir, String name) { for(String file_name:dir.list ()) {if(file_name.equals (name))return true; } return false; } }; //See if the "Pom.xml" file exists under the current directory Booleanresult = filenamefilter.accept (file, "Pom.xml"); SYSTEM.OUT.PRINTLN (result); } /*** All flows are in the "memory" angle. * such as "input stream", refers to the flow of other media "input into the native memory stream (this native memory stream is the input stream)". * The user then reads the data from the "in-memory" input stream. * * Output stream related operation is: "The In-memory stream (this flow refers to the output stream)" to the flow of other media, * and what we need to do is to "write" the data into the input stream of memory, as to how it travels away we do not have to tube. */ /*** InputStream and reader. * * InputStream is a superclass of all "byte" input stream related classes. * The Read method in the InputStream interface provides "read bytes (byte)" * * Reader is the Read method in the superclass * Reader interface for all related classes that are used to read a character stream (char array or stri NG) "* * and when we operate it is naturally more smooth to manipulate characters. * Actually InputStream's Read method is generally not for developers, but for other classes. * As you can see, the entire process is: * First get the "byte stream" data to the InputStream interface type. * Then translates into "character stream" data for the reader interface type. * Last call to reader interface provided by reader method read out characters. */ Public Static voidTestinputstream () {//object that gets the local "file name"File File =NewFile ("./pom.xml")); Try { //FileInputStream is a sub-implementation class of InputStream that reads bytes from the specified file into the in-memory "inputstream (input byte stream)"FileInputStream InputStream =Newfileinputstream (file); /*** InputStreamReader is a sub-implementation class of Reader * is a bridge of bytes flowing to the character stream, "sealed" InputStream inside, * it is in a higher level way, Reader () reads one character at a time, the ASCII code of the character is read by default*/InputStreamReader Reader=NewInputStreamReader (InputStream); intch; //the ASCII code that reads one character at a time, or 1, indicates the end. while(ch = reader.read ())!=-1) {System.out.print (Char) ch); } System.out.println (); /*** BufferedReader * Another "decoration" for the previous normal InputStreamReader * provides a buffered way to read text through the ReadLine () method Can read one row at a time, high efficiency. */FileInputStream inputstream2=Newfileinputstream (file); InputStreamReader Reader2=NewInputStreamReader (INPUTSTREAM2); BufferedReader BufferedReader=NewBufferedReader (Reader2); String Line; while(line = Bufferedreader.readline ())! =NULL) {System.out.println (line); } bufferedreader.close (); Reader2.close (); Inputstream2.close (); Reader.close (); Inputstream.close (); } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } } /*** OutputStream and writer * * with the same as above * OutputStream is responsible for outputting the "in-memory output stream", * then also gives us a writer method, let's put the "Byte (byte)" Writes to the in-memory output stream. * * While writer provides writer method, let us write "character (char array or string)" to the output stream in memory. * Note the above description, the Write () method can only write data to "output stream in memory" (equivalent to cache). * The Flush () method is required to send all in-memory buffered data to the destination. */ Public Static voidTestoutstream () {//Create a new file in the current directoryFile New_file =NewFile ("./test.txt")); Try { if(!new_file.exists ()) {New_file.createnewfile (); } /*** The Write (string) method of FileWriter has been able to write a string to the output stream. * The benefits of decorating the writter by "decorating" bufferedwriter are: * Add a buffer, buffer full and then write the data to the hard disk * This greatly improves performance * if used alone FileWriter can also, * but every data you write, the hard drive has a write action, poor performance*/String out_words= "Hello World"; //True indicates that the file will be written again at the end of the file and will not be overwritten. FileWriter FileWriter =NewFileWriter (New_file,true); BufferedWriter BufferedWriter=NewBufferedWriter (FileWriter); Bufferedwriter.write (out_words); Bufferedwriter.flush (); Bufferedwriter.close (); Filewriter.close (); } Catch(IOException e) {e.printstacktrace (); } } /*** Example: * rely on byte stream to copy picture*/ Public Static voidtestcopyimg () {//file address to be copiedFile File_old =NewFile ("./old.jpg")); //New file AddressFile file_new =NewFile ("./new.jpg")); //reads a file into the input character stream, and then writes a stream of output characters from the input stream, which is then written to the destination file by a stream of output characters Try { if(!file_new.exists ()) {File_new.createnewfile (); } FileInputStream FileInputStream=NewFileInputStream (File_old); FileOutputStream FileOutputStream=NewFileOutputStream (file_new); //creating a cache byte array byteData[] =New byte[1024]; //reads 1024 bytes at a time, that is, reads into the byte array, and then writes the array to the output stream while(Fileinputstream.read (data))!=-1) {fileoutputstream.write (data); Fileoutputstream.flush (); } //close the stream in turn (first open, back off, open first off)Fileoutputstream.close (); Fileinputstream.close (); } Catch(IOException e) {e.printstacktrace (); } }}
Java IO instance