Java File System - Directory

Java Conceptuel Diagram

About

File System - Directory in Java

Management

Creating

// Create a path Object
Path path  = Paths.get("./myDir");
// a directory
Files.createDirectory(path);
File dir = new File("/path/To/Dir");
if (!dir.exists()) {
	dir.mkdir();
}

Delete a directory recursively

static public void deleteDirectory(File path) 
    {
        if (path == null)
            return;
        if (path.exists())
        {
            for(File f : path.listFiles())
            {
                if(f.isDirectory()) 
                {
                    deleteDirectory(f);
                    f.delete();
                }
                else
                {
                    f.delete();
                }
            }
            path.delete();
        }
    }

Looping through the child of a directory

  • With nio. newDirectoryStream opens a directory, returns a DirectoryStream to iterate over all entries in the directory. The elements returned by the directory stream's iterator are of type Path
DirectoryStream<Path> directoryStream = Files.newDirectoryStream(dir);
int numberOfSubDir = 0;
int numberOfSubFile = 0;
for (Path path : directoryStream) {
    if (Files.isDirectory(path)){
        numberOfSubDir++;
    } else {
        numberOfSubFile++;
    }
}
File dir = new File("/path/To/Dir");
for(File f : dir.listFiles())
{
	if(f.isDirectory()) 
	{
		deleteDirectory(f);
		f.delete();
	}
	else
	{
		f.delete();
	}
}

Meta information

Path myDirectory = Paths.get("D:\\temp");
System.out.println("The directory exists ?: "+Files.exists(myDirectory));
System.out.println("The directory size in bytes is: "+Files.size(myDirectory));
System.out.println("The directory is a regular file? : "+Files.isRegularFile(myDirectory));
System.out.println("The directory is readable? : "+Files.isReadable(myDirectory));
System.out.println("The directory is executable? : "+Files.isExecutable(myDirectory));
The directory exists ?: true
The directory size in bytes is: 8192
The directory is a regular file? : false
The directory is readable? : true
The directory is executable? : true

Equality

Path myDirectory = Paths.get("D:\\temp");
System.out.println("The directory are equals? : "+Files.isSameFile(myDirectory, myDirectory));
true

Current User Directory

The current user directory is is named by the system property user.dir, and is typically the directory in which the Java virtual machine was invoked.





Discover More
Java Conceptuel Diagram
Java - (Nio|New I/O) Package

The Java NIO model is the last and second IO and File System mechanism of Java. It's available since Java 7. Before, the IO model was based on . In NIO, you define a file or a directory by creating...
Java Conceptuel Diagram
Java - File System - Path

in Java. The path is unique identifier of a file (directory) In nio, a Path instance represents a file or directory but doesn't access the file system (for instance in order to verify that a particular...
Java Conceptuel Diagram
Java - IO (Input/Output) - (File System|File|Console)

in Java: Ie File System Operations on file and directory Input / Output (IO) Operations (Input/Output - Read/Write) with stream Java has modeled a file system and IO operations: until Java...
Java Conceptuel Diagram
Java - Java IO

Java IO is the first IO model of Java. It provides for system input and output through data streams, serialization and the file system. See java/io/package-summaryjava.io. With the advent of Java 7, the...



Share this page:
Follow us:
Task Runner