Java I/O technology file operation detailed _java

Source: Internet
Author: User

In Java programming, I/O operations are implemented through the classes and interfaces in the Java.io package, so our first step is to import the package.

java.io provides a file class, which is easy to misunderstand, it represents a filename or directory name, rather than the file itself, so it is impossible to manipulate the data in the file through this class. The file class provides a sequence of functions to manipulate files: Delete files, create directories, query file sizes, and so on. If you want to manipulate the file data, you need to stream the object, which is not covered here for the time being.

The following is a package called the FileExtension class to encapsulate the various operations in the file class, and I hope that we can use the file class well, where I only provide the implementation of the DeleteFile. This example is quoted from the Java Instance Technology manual.

public class FileExtension {

 /**
 * Delete a specify file
 * @param filename:specify a file
 /   

public static void DeleteFile (string filename) {}//The function is to delete a specified existing file

 protected static void Fail (String msg) throws illegalargumentexception{
 throw new IllegalArgumentException (msg);
 }

} 

The implementation of DeleteFile is as follows:

public static void DeleteFile (String filename) {
 File File = new file (filename);
 
 if (!file.exists ())
  fail ("Delete:no such file or directory:" + filename);
 if (!file.canwrite ())
  fail ("Delete:write protected:" + filename);
 
 if (File.isdirectory ()) {
  string[] files = file.list ();
  if (Files.length > 0)
  fail ("Delete:directory not empty:" + filename);
 }
 
 Boolean success = File.delete ();
 
 if (!success)
  fail ("Delete:deletion failed");
 }

If you look at the above example in detail, then you will find that Java packaging for the file makes it easy to use, if you are interested you can add some functions, such as createdir,listdir,filesize and so on some features, which will help you.

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.