Java Programing laungage

Java Input Output Projects

Java I/O Project 1

Java Input Output Examples

Java Input Output Examples

File Input Output

The file i/o is a way of the making the platform independent code which is examine and manipulate the file. The file instances represent the file name. this class is included into the java.lang; package.

Previous Home Next

Why we are creating the object for file, which is not exist A program is used the object to parse a file name. we know that the file is created after passing the object to the constructor, such as FileWriter.

If the file is exist, a program can examine its attribute and perform the many operation on it like deletion, renaming and changing its performance.

File Name

The file object contain the name string to construct it. this object is never change in hole life of the object. the program can use the file object to obtain other version of the file name.

Suppose a program creates a file object with constructor invocation


File fl=new File("xanadu.txt"); 

In this the number of method is invoke to obtain the different version of the file name. then the program is run both operating system, on Microsoft Window System and Solaries System. and there are some method return like:

Method Invoke Return on MS Return on Solaries
ToString();xanadu.txt xanadu.txt
getName();xanadu.txtxanadu.txt
getParent();NULLNULL
getAbsolutePath();c:\java\examples\xanadu.txt/home/cafe/java/examples/xanadu.txt
getCanonicalPath();c:\java\examples\xanadu.txt /home/cafe/java/examples/xanadu.txt

Manipulating File

If a file object name is same as actual file name ,then program can use it to perform the number of the operation.this operation may be reading, writing and so on. The delete method is used to delete the file immediately but the DeleteOnExit method delete the file untill the virtual machine terminates.

The method SetLastModified() is used to sets the modification date/time for file.

Example


new File(xanadu.txt).setLastModified(new Date().getTime()); 

The renameTo() method is used to rename the file name.

Example


import java.io.File;
public class Deleteexam{
public static void main(String[] args){
try{
File file= new File("c:\shyam.log");
if(file.delete())(
System.out.println(file.getName(),+"is delete");}
else{
System.out.println("File already deleted");
}}
catch(Exception e){
e.printStackTree();
}
}
}

Previous Home Next