1.String ioutils.tostring(InputStream input), incoming stream object, return string, multiple overloads, can be passed as required
Case:
@Testpublic void Showinfobyioutils () throws ioexception {URL url = new URL ("https://www.baidu.com"); InputStream in = URL.O Penstream (); System.out.println (in), try {System.out.println (ioutils.tostring (in)),} catch (IOException e) {e.printstacktrace ();}}
2. Use string fileutils.readfiletostring(file file, string encoding), and Fileutils.writestringtofile( File, string data, string encoding) reads and writes files:
Case:
@Testpublic void Editfile () throws IOException {String fileName = "D:/11.txt"; File File = new file (fileName); String filecontent = ""; try {filecontent = fileutils.readfiletostring (file, "UTF8");} catch (IOException e) {e.printstacktrace ();} Filecontent + = "Helloworld, Me too"; try {fileutils.writestringtofile (file, filecontent, "UTF8");} catch (IOException e) { E.printstacktrace ();}}
3. copy files using fileutils.copyfile(file srcfile, file DestFile):
Case:
@Testpublic void CopyFile () throws IOException {try {file src = new File ("D:\\11.txt"); File Dest = new file ("E://11.txt"); Fileutils.copyfile (SRC, dest);//Clear the destination file, and then paste the contents} catch (IOException e) {e.printstacktrace ();}}
4. Using ioutils.tobytearray(InputStream input) and fileutils.writebytearraytofile(file file, byte[] Data ) to download the file locally:
Case:
@Testpublic void Download () throws IOException {try {inputstream in = new URL ("Http://www.baidu.com/img/baidu_logo.gif") . OpenStream (); byte[] gif = Ioutils.tobytearray (in); Fileutils.writebytearraytofile (New file ("E:/test.gif"), GIF);//Clear the destination file, and then paste the contents} catch (IOException e) { E.printstacktrace ();}}
5. Copy the file to the specified directory using fileutils.copyfiletodirectory(file srcfile, file DestDir):
Case:
@Testpublic void Copy2dir () throws IOException {try {file Srcfile = new File ("D:\\11.txt"); File DestDir = new file ("E:\\photoshop cs6"); Fileutils.copyfiletodirectory (Srcfile, DestDir);//Copy file to specified directory} catch (IOException e) {e.printstacktrace ();}}
6. Use fileutils.copyurltofile(URL source, file destination) to copy the URL object to the specified file, which is equivalent to saving the page as:
Case:
@Testpublic void Copyurl2file () throws Exception {URL url = new URL ("http://www.163.com"); File File = new file ("e:\\163.html"); Fileutils.copyurltofile (URL, file);}
7. Use fileutils.cleandirectory(file directory) to empty the directory, using fileutils.deletedirectory(file directory) To delete a directory:
Case:
@Testpublic void Cleanordeletedeletedirectory () throws IOException {try {file Dir = new File ("E:\\test"); Fileutils.cleandirectory (dir);//Empty directory file Fileutils.deletedirectory (dir);//delete directory and directory file} catch (IOException e) { System.out.println (E.getstacktrace ());}}
Use the Fileutils and Ioutils tool classes in the Commons-io.jar package to manipulate flows and files