Files in Java

The java.io.File  Represents files and directory names. This class is used for creating files and directories, searching for files, deleting, modifying, etc.

The Java File object  Represents the current file/directory on disk.

Java.io.File

The constructors to create the File object are:

The following syntax creates a new instance of File from the parent path and the child path.
File(File parent. String Threads);

The following syntax creates a File instance by converting the abstract path name.
File(String namePath)

The following syntax creates a File instance by converting the URI to an abstract pathname.
File(URI uri)

Methods of java.io.File

Once the File object is created, several methods that can be used to manipulate the instance File:

Public String getName()
retrieves the file/directory name.

Public String getParent()
Returns the names of the paths of the parent, otherwise null if the path name does not have a parent directory.

Public File getParentFile()
Returns the abstract path name of the parent, or null if the path name does not have a parent directory.

Public String getPath()
Convert the abstract path name to a path name string.

Public boolean isAbsolute ()
Returns true if the path is absolute.

Public String getAbsolutePath()
Returns the absolute path.

Public boolean exists()
Returns True if the file/directory exists.

Public boolean isDirectory()
Returns True if the File object is a directory.

Public boolean isFile()
Returns True if the File object is a file.

Public long lastModified()
Returns the last modified date in milliseconds, or 0L if the file doesn't exist.

Public boolean canRead()
Returns true if the file can be read.

Public boolean canWrite()
Returns true if the file is ready to be edited.

Public boolean createTempFile(String name, String suffix)
Creates a default temporary file.

Public boolean createNewFile() throws IOException
Creates an empty file automatically, named with the name of the abstract path. It returns True if the file is created successfully, otherwise false, if there is another file with the same name.

Public bolean delete()
Deletes the file or directory.

Public bolean deleteOnExit()
Deletes the file or directory when the JVM.

Public String[] list()
get a list of file and directory names.

Public File[] listFiles()
get a list of the abstract path names of the files in their directory.

Public boolean mkdir()
Creates a new directory. Returns true if the directory is created otherwise false.

Public boolean renameTo(File dest)
Rename a file.

Example

import java.io.File; 

public class Test {
public static void main(String[] args) {

File file = null;
String[] names = {"test1.txt", "test2"};
try{
// for each box in the array
for(String name: names)
{
// create a new file
file = new File(name);

// true if the file is executable
boolean bool = file.canExecute();

System.out.println("Absolute Path:" + file.getAbsolutePath());
System.out.println("Does it exist?" + file.exists());
System.out.println("Name: " + file.getName());
System.out.println("Is this a directory?" + file.isDirectory());
System.out.println("is an executable: "+ bool);

//display contents if the file is a folder
if (file.isDirectory() ) {
System.out.println("contents of the directory ");
File files[] = file.listFiles();
//Loop that goes through the
for(File f: files){
if (f.isDirectory())
System.out.println(" ["+f.getName()+"]");
else
System.out.println(" "+f.getName());
}
}
}
}catch(Exception e){
// if Input/Output errors appear
e.printStackTrace();
}
}
}
Output

Absolute path:C:\Users\VAIO\workspace\File\test1.txt
Does it exist? true
Name: test1.txt
Is this a directory? false
is an executable: true

Absolute path:C:\Users\VAIO\workspace\File\test2
Does it exist? true
Name: test2
Is this a directory? true
is an executable: true
contents of the
Android directory - The Ultimate Guide Www.biblio-scientifique.com.pdf
Graphical Interface Java.pdf
sunset.jpg

Display the contents of a folder in java

This example displays the listed files of a directory that is the root. To get the schema of the full tree, you need to create a recursive method that traverses the tree in depth and width to a predefined level.

Iterable root = FileSystems.getDefault().getRootDirectories(); 
for(Path path: root)
{
System.out.println(path);
//to list a directory, use the DirectoryStream
try(DirectoryStream list = Files.newDirectoryStream(path,"*.txt")){
int i = 0;
for(Path name: list)
{
System.out.println("\t\t" + ((Files.isDirectory(name) ? name+"/" : name)));
i++;
if(i%4 == 0)System.out.println("\n");
}
}
catch (IOException e) {
e.printStackTrace();
}
}
References
jenkov: Java IO: File
jmdoudoux.fr: NIO
Java Doc: Class File