Sunday, 5 April 2015

Tutorial On File Class In Java

File class does not.It deals directly with files and files system.A file object is used yo manipulate information associated with disk file,such as date,time and directory path its constructor are shown below.
File(String directoryPath)
File(String directoryPath,String filename)
File(File dirObj,String filename)
File(URL uriObj)

File has many methods that can obtain the properties of file as use in this tutorial

CODING:

import java.io.File;
class filedemo
{
static void p(String s)
{
System.out.println(s);
}
public static void main(String args[])
{
File f1 = new File("Pro java");
p("File Name:"+f1.getName());
p("Path:"+f1.getPath());
p("Abs Path:"+f1.getAbsolutePath());
p("Parent:"+f1.getParent());
p(f1.exists()?"exists":"does not exist:");
p(f1.canWrite()?"is writable":"is not writable");
p(f1.canRead()?"is readable":"is not readable");
p("is "+(f1.isDirectory() ? "not a directory" : "directory"));
p(f1.isFile() ? "is normal file" : "might be a named pipe");
p(f1.isAbsolute() ? "is absolute" : "is not absolute");
p("File last modified:"+f1.lastModified());
p("File size:"+f1.length() + "Bytes");
}
}

OUTPUT:


No comments:

Post a Comment