Reprint: Java File object manipulation

Source: Internet
Author: User
Tags functions
Object Java file Object manipulation

When we do a file operation, we need to know some information about the file. The file class provides some member functions to manipulate files and obtain information about some files.

1. Create a new File object

You can use the following three methods to create a new file object:

File MyFile; MyFile = new File ("ETC/MOTD");

Or

MyFile = new File ("/etc", "MOTD"); More useful if the directory or filename are variables

Or

File Mydir = new file ("/etc"); MyFile = new File (Mydir, "MOTD");

These three methods depend on how you access the file. For example, if you use only one file in an application, the first way to create a file structure is easiest. But if you open several files in the same directory, the second or third structure is better.

2, file testing and use

Once you create a file object, you can use the following member functions to obtain file-related information:

File name: String getName ()
Path: String GetPath ()
String Getabslutepath ()
String GetParent ()
Boolean Renameto (File newName)

File Tests: Boolean exists (), Boolean CanWrite (), Boolean CanRead (), Boolean Isfile (), Boolean isdirectory (), Boolean I Sabsolute ()

General file Information L long lastmodified () L long Length ()

Directory Usage L boolean mkdir () L string[] list ()

3. File Information Acquisition Example program

Here is a standalone program that displays basic information about the file, which is transmitted via command-line arguments:

Import java.io.*; Class fileinfo{
File Filetocheck;
public static void Main (String args[]) throws ioexception{
if (args.length>0) {
for (int i=0;i<args.length;i++) {
Filetocheck = new File (args[i]);
info (filetocheck);
}
}
Else
{
System.out.println ("No file given.");
}
}

public void info (File f) throws IOException {
System.out.println ("Name:" +f.getname ());
System.out.println ("Path:" =f.getpath ());
if (f.exists ()) {
System.out.println ("File exists.");
System.out.print ((F.canread ()? "and is readable": ")); System.out.print ((F.cnawrite ()? "and is writeable": ")); System.out.println ("."); System.out.println ("File is" + f.lenght () = "bytes.");} else {System.out.println ("File does not exist.");} }



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.