java--Print Flow, Commons-io

Source: Internet
Author: User

One, the print flow

1. Overview of the print flow

The print stream adds the ability to output data so that they can easily print various data value representations .

The print flow is categorized according to the flow:

(1) byte print stream PrintStream

(2) character print stream PrintWriter

Method:

void print (String str): Outputs any type of data,

void println(String str): Outputs any type of data, automatically writes a line-break operation

2. Code Demo:

1 //How does the print stream output2      Public Static voidMETHOD5 ()throwsioexception{3         //Clear Data Purpose4FileOutputStream fos=NewFileOutputStream ("E:\\test\\test.txt");5PrintStream ps=NewPrintStream (FOS);6Ps.print ("Zhang San");7Ps.println ("Hello");8Ps.println (true);9         //Freeing ResourcesTen ps.close (); One     } A     //Print stream Copy file -      Public Static voidMethod6 ()throwsioexception{ -         //Clear Data Source theFileReader fr=NewFileReader ("E:\\test\\test.txt"); -BufferedReader br=NewBufferedReader (FR); -         //Clear Data Purpose -FileWriter fw=NewFileWriter ("E:\\test\\pig.txt"); +PrintWriter pw=NewPrintWriter (FW,true);//Auto Refresh -         //Copy +String line=NULL; A          while((Line=br.readline ())! =NULL){ atPw.println (line);//Refresh + line break -         } -         //Freeing Resources - br.close (); - pw.close (); -}
Second, Commons-io1. Guide Package

class files in third-party jar packages that are added to Classpath can be used in projects

Create lib folder

Copy the Commons-io.jar to the lib folder

Right click on commons-io.jar,build path→Add to build path

2.FilenameUtils

This tool class is used to deal with file name (Translator Note: include file path), he can easily solve different operating system file names specification of different issues

Common methods:

GetExtension (String path): Gets the file name extension;

GetName (): Gets the file name;

Isextension (String filename,string ext): Determine If filename is the ext suffix name;

Code implementation:

1  Public Static voidMethod7 () {2         //get a file name extension3String name=filenameutils.getextension ("E:\\test");4 System.out.println (name);5         //get a file name6String filename=filenameutils.getname ("E:\\test\\test.txt");7 System.out.println (filename);8         //determine whether a file ends with what9         BooleanFlag=filenameutils.isextension ("Aaa.java", "Java");Ten System.out.println (flag); One}

3.FileUtils

Provides methods for file operations (moving files, reading files, checking whether files exist, and so on).

Common methods:

Readfiletostring (File file): reads the contents of the files and returns a string;

Writestringtofile (file file,string content): writes contents to file;

Copydirectorytodirectory (File srcdir,file destDir), folder replication

CopyFile (file Srcfile,file destfile);

Code implementation:

1 //Fileutils class2      Public Static voidMethod8 ()throwsioexception{3         //Copy Folder4Fileutils.copydirectory (NewFile ("E:\\test"),NewFile ("E:\\test2"));5         //Copying Files6Fileutils.copyfile (NewFile ("E:\\test\\test.txt"),NewFile ("E:\\test\\test5.txt"));7}

java--Print Flow, Commons-io

Related Article

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.