Java program demonstrating the usage of File Class in java.io package

//Java Program demonstrating the File Class in java.io package

FileDemo.java


import java.io.*;

class FileDemo
{
	public static void main(String[] args)
	{
		try{
			File f = new File("Hello.txt");
			
			System.out.println("File Class Methods in Java\n");
			
			System.out.println("File Exists : " +f.exists());
			System.out.println("File Name : " +f.getName());	
			System.out.println("File Parent Name : " +f.getParent());
			System.out.println("Is it File : " +f.isFile());
			System.out.println("Is it Directory : " +f.isDirectory());
			System.out.println("Is File Writable : "+f.canWrite());
			System.out.println("Is File Readable : "+f.canRead());	
			System.out.println("Does it have Absolute Path : " +f.isAbsolute());
			System.out.println("Path of File : "+f.getPath());
			System.out.println("Absolute Path of File : "+f.getAbsolutePath());
			System.out.println("File last modified : " +f.lastModified());
			System.out.println("File size : " +f.length()+ " bytes" );
			System.out.println("Is File Hidden: " +f.isHidden() );
			
			//System.out.println("Make File Readonly: " +f.setReadOnly() );
			//File f1 = new File("Hello2.txt");
			//System.out.println("Rename the File: " +f.renameTo(f1) );
			//System.out.println("Delete the File: " +f.delete() );
			
			System.out.println();
			
			 //For creating a file use createNewFile method
			/* File f2 = new File("File1.txt");
			System.out.println(f2.createNewFile());//if file already exists then file won't be created and  it will return false
			System.out.println(f2.exists());
		
			//Getting Directory Details
			File f1 = new File("Dir1");
			System.out.print("Directory Exists: ");
			System.out.println(f1.exists());
			System.out.print("Making Directory: ");
			System.out.println(f1.mkdir());
			*/
			//Proper way to create a file
			File f3 = new File("File3.txt");
			
			if(!f3.exists()){
				
				boolean b1 = f3.createNewFile();
				if(b1==true){
					System.out.println("File created successfully");
				}				
			}
			else
			{
				System.out.println("File already exists");				
			}
						
			//Proper way to create a directory
			File f4 = new File("Dir4");
			
			if(!f4.exists()){
				
				boolean b1 = f4.mkdir();
				if(b1==true){
					System.out.println("Directory created successfully");
				}				
			}
			else
			{
				System.out.println("Directory already exists");				
			}			

		}//try
		
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
}

Snapshot of the o/p

First o/p when the File and Directory are successfully created

2nd Time when the File and Directory already exists and are not created

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s