Use the Fileutils and Ioutils tool classes in the Commons-io.jar package to manipulate flows and files

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.