Java – File class

Previous
Next

java.io.File:

  • It is belongs to java.io package.
  • It is used to perform all File and Directory operations in Java app.

public class File extends Object implements Serializable, Comparable<File>

Checking the specified File is present or not:

public boolean exists()

Tests whether the file or directory denoted by this abstract pathname exists.

Returns:

true if and only if the file or directory denoted by this abstract pathname exists; false otherwise

Code:

import java.io.File ;
class Test 
{
	public static void main(String[] args) 
	{
		String path = "d:/check.txt";
		File obj = new File(path);
		if(obj.exists())
		{
			System.out.println(path + " is present");
		}
		else
		{
			System.out.println(path + " is not present");
		}
	}
}

Create new File if it is not present in the specified path:

public boolean createNewFile() throws IOException

Atomically creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist.

Code:

import java.io.File ;
import java.io.IOException;
class Test 
{
	public static void main(String[] args) throws IOException
	{
		String path = "d:/check.txt";
		File obj = new File(path);
		if(obj.exists())
		{
			System.out.println(path + " is present, no need to create");
		}
		else
		{
			System.out.println(path + " is not present");
			obj.createNewFile();
			System.out.println("File created...");
		}
	}
}
  • In the above case, we created new file in d:/ local drive.
  • If we try to create the file in OS located drive(for example c:/), it raises exception as that drive is secured.
import java.io.File ;
import java.io.IOException;
class Test 
{
	public static void main(String[] args) throws IOException
	{
		String path = "c:/check.txt";
		File obj = new File(path);
		if(obj.exists())
		{
			System.out.println(path + " is present, no need to create");
		}
		else
		{
			System.out.println(path + " is not present");
			obj.createNewFile();
			System.out.println("File created...");
		}
	}
}

c:/check.txt is not present
Exception in thread "main" java.io.IOException: Access is denied
        at java.io.WinNTFileSystem.createFileExclusively(Native Method)
        at java.io.File.createNewFile(File.java:1012)
        at Test.main(Test.java:16)

Create Directory:

  • Files having extensions(.txt, .doc, .java, .class…..)
  • Directories don’t have extensions.
  • File class object represents both directory and file.
  • File class identifies whether it is File or Directory with its extension.

public boolean mkdir()

Creates the directory named by this abstract pathname.

Returns: true if and only if the directory was created; false otherwise

import java.io.File ;
import java.io.IOException;
class Test 
{
	public static void main(String[] args) throws IOException
	{
		String path = "c:/users/srinivas/desktop/abc";
		File obj = new File(path);
		if(obj.exists())
		{
			System.out.println(path + " is present, no need to create");
		}
		else
		{
			System.out.println(path + " is not present");
			obj.mkdir();
			System.out.println(path + " created...");
		}
	}
}

Path representation:

  • Different operating Systems using different slash symbols in path representation.
  • Java application is platform independent. Hence the path should be independent.
  • We make the path independent to OS, we must create as follows
import java.io.File ;
import java.io.IOException;
class Test 
{
	public static void main(String[] args) throws IOException
	{
		String p1 = "c:/users/srinivas/desktop/abc";  
		System.out.println(p1);

		String p3 = "c://users//srinivas//desktop//abc";  
		System.out.println(p3);

		String p4 = "c:\\users\\srinivas\\desktop\\abc";  
		System.out.println(p4);
	}
}

Error path representation:

import java.io.File ;
import java.io.IOException;
class Test 
{
	public static void main(String[] args) throws IOException
	{
		// It is not allowed in windows :
		String p2 = "c:\users\srinivas\desktop\abc";  
		System.out.println(p2);
	}
}

Create sub directories:

public boolean mkdirs()

Creates the directory named by this abstract pathname, including any necessary but nonexistent parent directories. Note that if this operation fails it may have succeeded in creating some of the necessary parent directories.

Returns: true if and only if the directory was created, along with all necessary parent directories; false otherwise

import java.io.File ;
import java.io.IOException;
class Test 
{
	public static void main(String[] args) throws IOException
	{
		String path = "c://users//srinivas//desktop//abc//xyz//pqr//lmn";
		File obj = new File(path);
		if(obj.exists())
		{
			System.out.println(path + " is present, no need to create");
		}
		else
		{
			System.out.println(path + " is not present");
			obj.mkdirs();
			System.out.println(path + " created...");
		}
	}
}

separator:

  • It is static field belongs to File class.
  • It is used to constructor the path dynamically.
import java.io.File ;
import java.io.IOException;
class Test 
{
	public static void main(String[] args) throws IOException
	{
		String path = "c:"+File.separator+"users"+File.separator+"srinivas"+File.separator+"desktop"+File.separator+"abc";
		System.out.println("Path is : " + path);
		File obj = new File(path);
		if(obj.exists())
		{
			System.out.println(path + " is present, no need to create");
		}
		else
		{
			System.out.println(path + " is not present");
		}
	}
}

public String[] list()

Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname.

import java.io.File ;
class Test 
{
	public static void main(String[] args)
	{
		String path = "c:/users/srinivas/desktop/abc";
		File obj = new File(path);
		if(obj.exists())
		{
			System.out.println("List of files in specified folder :");
			String arr[ ] = obj.list();
			for (int i=0 ; i<arr.length ; i++)
			{
				System.out.println(arr[i]);
			}
		}
		else
		{
			System.out.println(path + " is not present");
		}
	}
}

public boolean isFile()

Tests whether the file denoted by this abstract pathname is a normal file.

public boolean isDirectory()

Tests whether the file denoted by this abstract pathname is a directory.

import java.io.File ;
class Test 
{
	public static void main(String[] args)
	{
		String path1 = "c:/users/srinivas/desktop/abc";
		File obj1 = new File(path1);
		if(obj1.isFile())
			System.out.println(path1 + " is File");
		else
			System.out.println(path1 + " is Directory");

		String path2 = "c:/users/srinivas/desktop/xyz.txt";
		File obj2 = new File(path2);
		if(obj2.isFile())
			System.out.println(path2 + " is File");
		else
			System.out.println(path2 + " is Directory");
	}
}

public boolean isHidden()

Tests whether the file named by this abstract pathname is a hidden file.

import java.io.File ;
class Test 
{
	public static void main(String[] args)
	{
		String path = "c:/users/srinivas/desktop/xyz.txt";
		File obj = new File(path);
		if(obj.isHidden())
			System.out.println("Specified file is in hidden mode");
		else
			System.out.println("Specified file is not hidden");
	}
}
Previous
Next

Add Comment

Courses Enquiry Form