File- Example In java
import
java.io.File;
public
class
FileDemo {
public
static
void
main( String [] args ){
File file =
new
File( "C:\\CreateFile.txt"
);
System. out.println("Name
of the File: " +file.getName());
System. out.println("Name
of the File: " +file.getAbsolutePath());
System. out.println("Parent
of the File: " +file.getParent());
System. out.println("Path
of the File: " +file.getPath());
System. out.println("Can
I read File: " +file.canRead());
System. out.println("Can
I write File: " +file.canWrite());
System. out.println("Is
this file existing? " +file.exists());
System. out.println("Is
this a directory: " +file.isDirectory());
System. out.println("Is
this a file " +file.isFile());
System. out.println("Are
you hiding this file " +file.isHidden());
System. out.println("File
is last modified on: " +file.lastModified());
System. out.println("size
of the file: " +file.length()+
" Bytes");
}
}
Out put of the program for the file in this
machine:
Name of the File: CreateFile.txt
Name of the File: C:\CreateFile.txt
Parent of the File: C:\
Path of the File: C:\CreateFile.txt
Can I read File: true
Can I write File: true
Is this file existing? true
Is this a
directory:
false
Is this a file true
Are you hiding this file false
File is last modified on:
1223059505625
size of the file: 62 Bytes
List contents of a directory:
/**
*
@author
venugopal
darur
*
*/
public
class
ListFilesInDirectory {
/**
*
This
method
will
demonstrate
how
to
list
the
names
of
a
directory
*
@param
args
*/
public
static
void main(String[] args) {
//
TODO
Auto-generated method stub
//
This block will list the files in a Directory
java.io.File file =
new
File("C:\\");
if(
file.isDirectory()){
String [] files = file.list();
for(
int i=0;i<files.length;i++){
System.out.println("Name:
"+files[i]);
}
}
}
}
 
Please send comments to vgdarur.javafive@blogger.com
|