Directories in CLIPS Here’s the translation of the Go code example to Java, formatted in Markdown suitable for Hugo:
Our first program demonstrates working with directories in the file system.
import java.io.File ;
import java.io.IOException ;
import java.nio.file.Files ;
import java.nio.file.Path ;
import java.nio.file.Paths ;
public class Directories {
private static void check ( IOException e ) {
if ( e != null ) {
throw new RuntimeException ( e );
}
}
public static void main ( String [] args ) {
// Create a new sub-directory in the current working directory.
File subdir = new File ( "subdir" );
boolean created = subdir . mkdir ();
if ( ! created ) {
System . err . println ( "Failed to create directory" );
return ;
}
// When creating temporary directories, it's good practice to delete them when done.
// We'll use try-with-resources to ensure cleanup.
try {
// Helper function to create a new empty file.
createEmptyFile ( "subdir/file1" );
// We can create a hierarchy of directories, including parents.
// This is similar to the command-line `mkdir -p`.
File parentChild = new File ( "subdir/parent/child" );
created = parentChild . mkdirs ();
if ( ! created ) {
System . err . println ( "Failed to create directory hierarchy" );
return ;
}
createEmptyFile ( "subdir/parent/file2" );
createEmptyFile ( "subdir/parent/file3" );
createEmptyFile ( "subdir/parent/child/file4" );
// List directory contents
File parentDir = new File ( "subdir/parent" );
System . out . println ( "Listing subdir/parent" );
File [] files = parentDir . listFiles ();
if ( files != null ) {
for ( File file : files ) {
System . out . println ( " " + file . getName () + " " + file . isDirectory ());
}
}
// Change the current working directory
System . setProperty ( "user.dir" , "subdir/parent/child" );
// Now we'll see the contents of `subdir/parent/child` when listing the current directory.
File currentDir = new File ( "." );
System . out . println ( "Listing subdir/parent/child" );
files = currentDir . listFiles ();
if ( files != null ) {
for ( File file : files ) {
System . out . println ( " " + file . getName () + " " + file . isDirectory ());
}
}
// Change back to where we started
System . setProperty ( "user.dir" , "../../.." );
// We can also visit a directory recursively, including all its sub-directories.
System . out . println ( "Visiting subdir" );
Files . walkFileTree ( Paths . get ( "subdir" ), new SimpleFileVisitor < Path > () {
@Override
public FileVisitResult visitFile ( Path file , BasicFileAttributes attrs ) throws IOException {
System . out . println ( " " + file + " " + attrs . isDirectory ());
return FileVisitResult . CONTINUE ;
}
@Override
public FileVisitResult preVisitDirectory ( Path dir , BasicFileAttributes attrs ) throws IOException {
System . out . println ( " " + dir + " " + attrs . isDirectory ());
return FileVisitResult . CONTINUE ;
}
});
} finally {
// Clean up: delete the temporary directory and all its contents
deleteDirectory ( new File ( "subdir" ));
}
}
private static void createEmptyFile ( String name ) throws IOException {
Files . write ( Paths . get ( name ), new byte [ 0 ] );
}
private static void deleteDirectory ( File directory ) {
File [] files = directory . listFiles ();
if ( files != null ) {
for ( File file : files ) {
if ( file . isDirectory ()) {
deleteDirectory ( file );
} else {
file . delete ();
}
}
}
directory . delete ();
}
}
This Java program demonstrates various operations with directories:
It creates a new subdirectory in the current working directory. It creates a hierarchy of directories. It creates empty files in different directories. It lists the contents of directories. It changes the current working directory. It recursively visits all files and directories in a directory tree. The program uses the File
class for most operations, and the newer Files
and Path
classes from the java.nio.file
package for some operations like creating empty files and walking the directory tree.
Note that Java doesn’t have a direct equivalent to Go’s defer
statement, so we use a try-finally
block to ensure cleanup of the temporary directory.
To run this program, save it as Directories.java
, compile it with javac Directories.java
, and then run it with java Directories
.
$ javac Directories.java
$ java Directories
Listing subdir/parent
child true
file2 false
file3 false
Listing subdir/parent/child
file4 false
Visiting subdir
subdir true
subdir/file1 false
subdir/parent true
subdir/parent/child true
subdir/parent/child/file4 false
subdir/parent/file2 false
subdir/parent/file3 false
This output shows the directory structure created and visited by the program.