* Copy of text files can be achieved using FileReader and FileWriter.
* For non-text files (video files, audio files, pictures), only the byte stream can be used!
1 @Test2 Public voidTestfilereader () {3FileReader FR =NULL;4 Try {5File File =NewFile ("Dbcp.txt");6FR =Newfilereader (file);7 Char[] C =New Char[24];8 intLen;9 while(len = Fr.read (c))! =-1){TenString str =NewString (c, 0, Len); One System.out.print (str); A } -}Catch(IOException e) { - //TODO auto-generated Catch block the e.printstacktrace (); -}finally{ - if(FR! =NULL){ - Try { + fr.close (); -}Catch(IOException e) { + //TODO auto-generated Catch block A e.printstacktrace (); at } - } - } - -}
1 @Test2 Public voidTestfilereaderwriter () {3 //1. Input stream corresponding file src must exist, otherwise throw an exception. The output stream corresponding to the file dest can not exist, the execution process will be automatically created4FileReader FR =NULL;5FileWriter FW =NULL;6 Try{7 //cannot implement a copy of a non-text file8 //file src = new file ("C:\\users\\shkstart\\desktop\\1.jpg");9 //file Dest = new file ("C:\\users\\shkstart\\desktop\\3.jpg");TenFile src =NewFile ("Dbcp.txt"); OneFile dest =NewFile ("Dbcp1.txt"); A //2. -FR =NewFileReader (SRC); -FW =NewFileWriter (dest); the //3. - Char[] C =New Char[24]; - intLen; - while(len = Fr.read (c))! =-1){ +Fw.write (c, 0, Len); - } +}Catch(Exception e) { A e.printstacktrace (); at}finally{ - if(FW! =NULL){ - Try { - fw.close (); -}Catch(IOException e) { - //TODO auto-generated Catch block in e.printstacktrace (); - } to } + if(FR! =NULL){ - Try { the fr.close (); *}Catch(IOException e) { $ //TODO auto-generated Catch blockPanax Notoginseng e.printstacktrace (); - } the } + } A}
Java Learning--file stream FileReader and FileWriter applications