Files
• A File
object is used to obtain or manipulate the information associated with a disk
file, such as the permissions, time, date, and directory path, and to navigate
subdirectory hierarchies.
• constructors
used to create File objects:
File(String
directoryPath)
File(String
directoryPath, String filename)
File(File
dirObj, String filename)
File(URI
uriObj)
Example:
import java.io.File;
class FileDemo {
public static void main(String args[]) { File f1 =
new File("/ip/test");
System.out.println
("File Name: " + f1.getName());
System.out.println
("Path: " + f1.getPath());
System.out.println
("Abs Path: " + f1.getAbsolutePath());
System.out.println
("Parent: " + f1.getParent());
System.out.println
(f1.exists() ? "exists" : "does not exist");
System.out.println
(f1.canWrite() ? "is writeable" : "is not writeable");
System.out.println (f1.canRead() ? "is readable" : "is not readable");
System.out.println ("is " + (f1.isDirectory() ? "" :
"not" + " a directory")); System.out.println (f1.isFile() ?
"is normal file" : "might be a named pipe");
System.out.println (f1.isAbsolute() ? "is absolute" : "is not
absolute"); System.out.println ("File last modified: " +
f1.lastModified()); System.out.println ("File size: " + f1.length() +
" Bytes");
}
}
File
Name: test
Path:
/ip/test Abs
Path:
/ip/test
Parent:
/ip
exists
is
writeable is readable
is not a
directory is normal file
is absolute
File last
modified: 812465204000
File
size: 695 Bytes
getName( ) returns the name of the file,
getParent( ) returns the name of the
parent directory, and exists( )
returns true if the file exists, false if it does not.
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2026 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.