Thursday, February 19, 2009

java File

File:
A File Object is an abstract representstion of name of a file or directory.

File f=new File(“abc.txt”);
Ststem.out.println(f.exits()); ------------>output: false
F represents a file abc.txt only
File f=new File(“Laxman”);
F represents a diretory ‘Laxman’.
.java file object represents both files and diretory(unix terminology).

Constructors of the File class:
--> File f=new File (String name)
create a file instance that represents the name of the file or directory.
--> File f =new File(String diretory,string name)
creates a file instance that represents a diretory or file name in the specified directory.
---> File f=new File(File dir,String name)
create a new file instance that represents a file or diretory name in the specified directory.
If the specified is not exists we will find an exception saying IOException system can’t find the specified diretory.
Methods of the File class:
1.boolean exists()
return true if the file or diretory exists,otherwise false.
2.boolean createNewFile()
returns true if it will create a new file otherwise false.
3.boolean mkdir()
create the new diretory and returns true if the directory already present returns false.
4.boolean isFile()
return true if the file object represents File
5.boolean isDirectory()
Inorder to check whether the file object represents,physical file name or directory.
6.String[] list()
returns the files/directory name present in the directory.
7.boolean delete()
delete the file or directory represented by file object.
8.boolean renameTo(File destination)
1)Write a program(WAP) to create a file named with “xyz.txt” in the current directory.
2)WAP for createing a directory laxman123 in the current working directory and in that create a file named with file1.txt.
3.WAP a program to list all the files and directories present in directory laxman123.
File Example :
1. Class Sample
{
public static void main(String add[])
{
File f=new File(“xyz.txt”);
If (!f.exists())

f.createNewFile();

System.out.println(f.exists());
}
}
2. class Sample
{
public static void main(String aff[])
{
File f=new File(“laxman.txt”);
If(!f.exists())
f.mkdir();
File f1=new File(“laxman”,”laxman txt”);
F1.createNewFile();
System.out.println(f1.exists());
}
}
3. Class Samle
{
public static void main(String ah[])
{
File f=new File (“laxman123”); ------------->already exists
String[] s=f.list();
for(String s1:s)
{
System.out.println(s1);
}
}
With the same name there may be a chance of a file or a directory .From java code creating the file followed by creating the directory with the same name allowed and File object represents a file only.
After creating the directory ,if we are creating file with tha same name we will get RTE saying IOException:Access is denied
File f=new File(“dir1”,”file1.txt”);
This file object represents a file named with file1.txt which will present in the directory
f.writeNewFile();
If the directory is not present then f.createNewFile() results a RTE saying IOException:The system can not find the path specified.
If we call list() method on java file object which represents a physical file instead of directory.it just simply return null.
If applied on directory it returns the content of the directory.

1 comments:

Unknown said...

Nicely described

Post a Comment