QR Code for this Page
java io File Class

java io File Class

java.io.File Class

File Class in Java

The File class manipulates disk files. Instances of the File class are immutable; that is, once created, the abstract pathname represented by a File object will never change. The File class provides methods for :

  • Listing directories
  • Querying file attributes
  • Renaming and Deleting files

Following Java File class example shows, How to print file names using java? How to print directory names using java? How to get files and directories using java? How to get files and directories from given path using java?

Print the files and directories names of given path using Java

/* Print the files and directories names of given path using Java Example */
/* Save with file name FileExample.java */

public class FileExample
{
	public static void main(String args[])
	{
		//java.io.File DECLARATION
		java.io.File f;

		String filenames[];

		//java.io.File OBJECT CREATION
		f = new java.io.File("d:/");

		//GETTING THE FILES AND DIRECTORY NAMES OF D:\
		filenames = f.list();

		//PRINT THE FILES AND DIRECTORY NAMES
		for(int i=0;i<filenames.length; i++)
		{
			System.out.println((i+1)+"  "+filenames[i]);
		}
	}
}

How to print only files names using java? How to get only files from given path using java?

Print only files names of given path using Java

/* Print only files names of given path using Java Example */
/* Save with file name FileExample2.java */

public class FileExample2
{
	public static void main(String args[])
	{
		//java.io.File DECLARATION
		java.io.File f;

		java.io.File files[];

		//java.io.File OBJECT CREATION
		f = new java.io.File("d:/");

		//GETTING THE FILES AND DIRECTORIES OF D:\
		files = f.listFiles();

		//PRINT THE FILE NAMES
		int count = 1;
		for(int i=0;i<files.length; i++)
		{
			if(files[i].isFile())
			{
				System.out.println((count++)+"  "+files[i].getName());
			}
		}
		System.out.println("\n     Total "+ count +" File(s) Found");
	}
}

How to print only directory names using java? How to get only directories from given path using java?

Print only directory names of given path using Java

/* Print only directory names of given path using Java Example */
/* Save with file name FileExample3.java */

public class FileExample3
{
	public static void main(String args[])
	{
		//java.io.File DECLARATION
		java.io.File f;

		java.io.File files[];

		//java.io.File OBJECT CREATION
		f = new java.io.File("d:/");

		//GETTING THE FILES AND DIRECTORIES OF D:\
		files = f.listFiles();

		//PRINT THE FILE NAMES
		int count = 1;
		for(int i=0;i<files.length; i++)
		{
			if(files[i].isDirectory ())
			{
				System.out.println((count++)+"  "+files[i].getName());
			}
		}
		System.out.println("\n     Total "+ count +" Dir(s) Found");
	}
}

How to print available file system root names using java? How to get file system roots using java? How to get available drives using java?

Print available file system root names using Java

/* Print available file system root names using Java Example */
/* Save with file name FileExample4.java */

import java.io.File;

public class FileExample4
{
	public static void main(String args[])
	{
		// File DECLARATION
		File f;

		File files[];

		//File OBJECT CREATION
		f = new File("d:/");

		//GETTING THE AVAILABLE FILE SYSTEM ROOTS
		files = f.listRoots();

		//PRINT THE FILE NAMES
		for(int i=0;i<files.length; i++)
		{
				System.out.println(files[i].getPath());
		}
	}
}

How to print available space in a drive using java? How to print used space in a drive using java? How to print total space of a drive using java?

Print available, used and total space of given Drive using Java

/* Print available space, used space and total space of given Drive using Java Example */
/* Save with file name FileExample5.java */

import java.io.File;

public class FileExample5
{
	public static void main(String args[])
	{
		long totalspace = 0, freespace = 0, usedspace = 0;
		// File DECLARATION
		File f;

		//File OBJECT CREATION
		f = new File("d:/");

		totalspace = f.getTotalSpace();

		freespace = f.getFreeSpace();

		usedspace = (totalspace - freespace);

		System.out.println("Used Space : "+usedspace);
		System.out.println("Free Space : "+freespace);
		System.out.println("Total Space : "+totalspace);
	}
}

How to set File Read Only using java

/* How to set File Read Only using java Example */
/* Save with file name FileExample6.java */

import java.io.File;

public class FileExample6
{
	public static void main(String args[])
	{
		// File DECLARATION
		File f;

		//File OBJECT CREATION
		f = new File("FileExample6.java");

		if(f.setReadOnly())
			System.out.println("File is Successfully changed to Read Only");
		else
			System.out.println("File Read Only is Unsuccessful");
	}
}

How to remove File Read Only Attribute using Java

/* How to remove File Read Only Attribute using Java Example */
/* Save with file name FileExample7.java */

import java.io.File;

public class FileExample7
{
	public static void main(String args[])
	{
		// File DECLARATION
		File f;

		//File OBJECT CREATION
		f = new File("FileExample6.java");

		if(f.setWritable(true))
			System.out.println("File is Successfully changed to Writable");
		else
			System.out.println("File Writable is Unsuccessful");
	}
}

How to get File path and convert it to URI and URL using Java

/* How to get File path and convert it to 
   URI and URL using Java Example */
/* Save with file name FileExample8.java */

import java.io.File;

public class FileExample8
{
	public static void main(String args[])
	{
		// File DECLARATION
		File f;

		try
		{
			//File OBJECT CREATION
			f = new File("FileExample8.java");
			System.out.println("File Path : "+f.getPath());
			System.out.println("File URI : "+f.toURI());
			System.out.println("File URL : "+f.toURI().toURL());
		}
		catch(Exception ex)
		{
			System.out.println("Problem Occurred : "+ex);
		}
	}
}

How to create Directory using Java

/* How to create Directory using Java Example */
/* Save with file name FileExample9.java */

import java.io.File;

public class FileExample9
{
	public static void main(String args[])
	{
		// File DECLARATION
		File f;

		try
		{
			//File OBJECT CREATION
			f = new File("mydirectory");
			if(f.mkdir())
				System.out.println("Directory Created");
			else
				System.out.println("Directory Not Created");

		}
		catch(Exception ex)
		{
			System.out.println("Problem Occurred : "+ex);
		}
	}
}

How to create Directory and sub Directories using Java

/* How to create Directory and sub Directories using Java Example */
/* Save with file name FileExample10.java */

import java.io.File;

public class FileExample10
{
	public static void main(String args[])
	{
		// File DECLARATION
		File f;

		try
		{
			//File OBJECT CREATION
			f = new File("myparent/mychild");
			if(f.mkdirs())
				System.out.println("Directories Created");
			else
				System.out.println("Directories Not Created");

		}
		catch(Exception ex)
		{
			System.out.println("Problem Occurred : "+ex);
		}
	}
}

How to create an Empty File using Java

/* How to create an Empty File using Java Example */
/* Save with file name FileExample11.java */

import java.io.File;

public class FileExample11
{
	public static void main(String args[])
	{
		// File DECLARATION
		File f;

		try
		{
			//File OBJECT CREATION
			f = new File("myemptyfile.txt");
			if(f.createNewFile())
				System.out.println("Empty File Created");
			else
				System.out.println("Empty File Not Created");

		}
		catch(Exception ex)
		{
			System.out.println("Problem Occurred : "+ex);
		}
	}
}