java.io.File Class

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:

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?

java.io.File class Example
/* Following example print the files and directories names of given path. */
/* 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?

java.io.File class Example 2
/* Following example print only files names of given path. */
/* 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?

java.io.File class Example 3
/* Following example print only directory names of given path. */
/* 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?

java.io.File class Example 4
/* Following example print available file system root names. */
/* 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?

java.io.File class Example 5
/* Following example print available space, used space and total space of given Drive. */
/* 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);
	}
}
                        
java.io.File class Example 6
/* Following example shows how to set File Read Only using java. */
/* 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");
	}
}
                        
java.io.File class Example 7
/* Following example shows how to remove File Read Only Attribute using java. */
/* 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");
	}
}
                        
java.io.File class Example 8
/* How to get File path and convert it to URI and URL using java. */
/* 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);
		}
	}
}
                        
java.io.File class Example 9
/* How to create Directory using java. */
/* 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);
		}
	}
}
                        
java.io.File class Example 10
/* How to create Directory and sub Directories using java. */
/* 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);
		}
	}
}
                        
java.io.File class Example 11
/* How to create an Empty File using java. */
/* 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);
		}
	}
}