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{
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.