Java File Operations

Source: Internet
Author: User

 

Java provides hundreds of classes for file operations.

 

Related classes such as outputstreamwriter, writer, bufferedoutputstream, fileoutputstream, fileinputstream, reader, and inputstreamreader are commonly used.

 

First, make a comparison: For text file operations, we generally use the filereader and bufferedreader classes; instead of the file, that is, when operating binary files, we use inputstream and related classes, but for stream operations, there are many applications in remote interface rmi or communication business processing.

 

Fileoutputstream and bufferedoutputstream perform file write operations. When operating large files, bufferedoutputstream is much more efficient than fileoutputstream.

Of course, bufferedoutputstream inherits from filteroutputstream, so its functions are quite rich. In addition, we use the filteroutputstream class in many applications, such as zip, jar packages, rmi classes and interfaces, and crypto provides encryption classes and interfaces.

 

There are also many useful classes in the file operation class, such as the file stream description class filedescriptor.

 

When there is a special requirement, we can inherit the above interface or extend the above class to implement special services.

 

 

Java code

Package com. xiva. demo; import java. io. bufferedoutputstream; import java. io. bufferedreader; import java. io. bufferedwriter; import java. io. file; import java. io. fileoutputstream; import java. io. filereader; import java. io. filewriter; import java. io. ioexception; import java. io. inputstream; import java. io. outputstream; import java. io. outputstreamwriter; import java. io. reader; import java. io. writer; import java. util. SC Anner; import org. junit. test; public class filedemo {/***** @ description delete file * @ author xiva */@ test public void deletefile () {file = new file ("src/_tian.jpg"); boolean res = false; if (file. exists () {res = file. delete ();} if (res) {system. out. println ("delete success! ");} Else {system. out. println (" delete fail! ") ;}}/***** @ Description file rename * @ author xiva */@ test public void renamefile () {file = new file ("src/_tian.jpg"); boolean res = false; if (file. exists () & amp; file. isfile () {res = file. renameto (new file ("src/tian.jpg");} if (res) {system. out. println ("copy success! ");} Else {system. out. println (" copy fail! ") ;}}/***** @ Description reads text file content * @ author xiva * @ throws ioexception */@ test public void readfile () throws ioexception {inputstream is = filedemo. class. getclassloader (). getresourceasstream ("dbcpconfig. properties "); // file = new file (" src/content.txt "); // filereader freader = new filereader (file ); // stream encoding is Byte encoding // reader isreader = new inputstreamreader (is, "UTF-8"); // bufferedreader buffer = New bufferedreader (isreader); // string linestr; // linestr = buffer. readline (); // while (linestr! = Null) {// system. out. println (linestr); // linestr = buffer. readline (); //} // more convenient methods are provided for the callback class. The most important function is the function that facilitates text file operations. Functions SC = new callback (is, "UTF-8"); while (SC. hasnextline () {system. out. println (SC. nextline ();} is. close ();}/***** @ description filewriter Write File * @ author xiva * @ throws ioexception */@ test public void writefile () throws ioexception {file = new file ("src/test.txt"); if (! File. exists () {file. createnewfile () ;}// reader isreader = new filereader (file); // bufferedreader buffer = new bufferedreader (isreader); // string linestr; // linestr = buffer. readline (); Using SC = new using (file, "UTF-8"); stringbuffer sbuffer = new stringbuffer (); while (SC. hasnextline () {sbuffer. append (SC. nextline ();} // append if true, then bytes will be written to the end of the file rat Her than the beginning filewriter fw = new filewriter (file, true); bufferedwriter bw = new bufferedwriter (fw); bw. append (sbuffer. tostring (); bw. write ("I'm xiva, I'm xiva"); bw. newline (); bw. write ("I'm njit. shot_go, I'm shot_go "); // string message =" I'm njit. shot_go, I am shot_go "; // for (char mc: message. tochararray () {// bw. append (mc); //} bw. flush (); bw. close (); fw. close ();}/***** @ description fileoutpu Tstream Write file * @ author xiva * @ throws ioexception */@ test public void writefosfile () throws ioexception {file = new file ("src/test.txt"); if (! File. exists () {file. createnewfile ();} // append if true, then bytes will be written to the end of the file rather than the beginning fileoutputstream fos = new fileoutputstream (file, true ); bufferedoutputstream bos = new bufferedoutputstream (fos); for (int I = 0; I <100000; I ++) {string message = "I'm njit. shot_go, I am shot_go "; bos. write (message. getbytes (); bos. flush (); bos. write ("\ r \ n ". getbytes ();} bos. close (); fos. close ();}/***** @ description file list * @ author xiva * @ param rootfile */public void listdictionary (file rootfile) {file [] files = rootfile. listfiles (); for (file: files) {if (file. isdirectory () {system. out. println ("add to tree node"); listdictionary (file);} else {system. out. println (file. getpath () ;}}@ test public void testlistdictionary () {file rootfile = new file ("e:" + file. separatorchar + "liber"); listdictionary (rootfile );}}

The operations on files are summarized as follows:

1. delete a file or folder

2. Create a file or folder

3. Read text or binary files

4. Write text or binary files (for binary files, I think they will be used in image processing and audio/video processing)

5. rename a file, traverse a file, move a file, etc.

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.