package org.kodejava.io;
import java.io.File;
public class EmptyDirCheck {
public static void main(String[] args) {
File file = new File("D:/Downloads");
// Check to see if the object represent a directory.
if (file.isDirectory()) {
// Get list of file in the directory. When its length is not zero
// the folder is not empty.
String[] files = file.list();
if (files != null && files.length > 0) {
System.out.println(file.getPath() + " is not empty!");
}
}
}
}
Tag Archives: File
How do I create a human-readable file size?
This example demonstrates how to create a human-readable file size using the FileUtils class of the Apache Commons IO library. The byteCountToDisplaySize() method take the file size in bytes and return a human-readable display of the file size in bytes, kilobytes, megabytes, gigabytes, etc.
package org.kodejava.commons.io;
import org.apache.commons.io.FileUtils;
import java.io.File;
public class ReadableFileSize {
public static void main(String[] args) {
File file = new File("D:/Downloads/jdk-17_windows-x64_bin.exe");
long size = file.length();
String display = FileUtils.byteCountToDisplaySize(size);
System.out.println("Name = " + file.getName());
System.out.println("Size = " + size);
System.out.println("Display = " + display);
}
}
Here are the results of our program:
Name = jdk-17_windows-x64_bin.exe
Size = 159373640
Display = 151 MB
Maven Dependencies
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.14.0</version>
</dependency>
How do I copy file?
This example demonstrates how to copy file using the Java IO library. Here we will use the java.io.FileInputStream and it’s tandem the java.io.FileOutputStream class.
package org.kodejava.io;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileCopyDemo {
public static void main(String[] args) {
// Create an instance of source and destination files
File source = new File("source.pdf");
File destination = new File("target.pdf");
try (FileInputStream fis = new FileInputStream(source);
FileOutputStream fos = new FileOutputStream(destination)) {
// Define the size of our buffer for buffering file data
byte[] buffer = new byte[4096];
int read;
while ((read = fis.read(buffer)) != -1) {
fos.write(buffer, 0, read);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
How do I read file line by line using java.util.Scanner class?
Here is a compact way to read file line by line using the java.util.Scanner class.
package org.kodejava.util;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ScannerReadFile {
public static void main(String[] args) {
// Create an instance of File for data.txt file.
File file = new File("README.md");
try {
// Create a new Scanner object which will read the data
// from the file passed in. To check if there are more
// line to read from it, we call the scanner.hasNextLine()
// method. We then read line one by one till all lines
// is read.
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
System.out.println(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
How do I create a directories recursively?
The code below use File.mkdirs() method to create a collection of directories recursively. It will create a directory with all its necessary parent directories.
package org.kodejava.io;
import java.io.File;
public class CreateDirs {
public static void main(String[] args) {
// Define a deep directory structures and create all the
// directories at once.
String directories = "D:/kodejava/a/b/c/d/e/f/g/h/i";
File file = new File(directories);
// The mkdirs will create folder including any necessary
// but nonexistence parent directories. This method returns
// true if and only if the directory was created along with
// all necessary parent directories.
boolean created = file.mkdirs();
System.out.println("Directories created? " + created);
}
}
How can I change file attribute to writable?
Prior to Java 1.6 the java.io.File class doesn’t include a method to change a read only file attribute and make it writable. To do this on the old days we have to utilize or called operating system specific command. In Java 1.6 a new method named setWritable() was introduced to do exactly what the method name says.
package org.kodejava.io;
import java.io.File;
public class WritableExample {
public static void main(String[] args) throws Exception {
File file = new File("Writable.txt");
// Create a file only if it doesn't exist.
boolean created = file.createNewFile();
// Set file attribute to read only so that it cannot be written
boolean succeeded = file.setReadOnly();
// We are using the canWrite() method to check whether we can
// modified file content.
if (file.canWrite()) {
System.out.println("File is writable!");
} else {
System.out.println("File is in read only mode!");
}
// Now make our file writable
succeeded = file.setWritable(true);
// re-check the read-write status of file
if (file.canWrite()) {
System.out.println("File is writable!");
} else {
System.out.println("File is in read only mode!");
}
}
}
And here are the result of the code snippet above:
File is in read only mode!
File is writable!
How can I change file attribute to read only?
This code demonstrate how we can modify file attribute to be read only. File class has a setReadOnly() method to make file read only and a canWrite() method to know whether it is writable or not.
package org.kodejava.io;
import java.io.File;
public class FileReadOnlyExample {
public static void main(String[] args) throws Exception {
File file = new File("ReadOnly.txt");
// Create a file only if it doesn't exist.
boolean created = file.createNewFile();
// Set file attribute to read only so that it cannot be written
boolean succeeded = file.setReadOnly();
// We are using the canWrite() method to check whether we can
// modified file content.
if (file.canWrite()) {
System.out.println("File is writable!");
} else {
System.out.println("File is in read only mode!");
}
}
}
This code snippet print the following output:
File is in read only mode!
How do I determine if a pathname is a directory?
To determine if an abstract pathname is a directory we can use the File.isDirectory() method. Here is an example code.
package org.kodejava.io;
import java.io.File;
public class IsDirectoryExample {
public static void main(String[] args) {
// Creates a instance of File.
File file = new File("C:/Users/wsaryada");
// Check if the abstract pathname is a directory by calling
// isDirectory() method of the File class.
if (file.isDirectory()) {
System.out.println("This file is a directory.");
} else {
System.out.println("This is just an ordinary file.");
}
}
}
How do I rename a file or directory?
package org.kodejava.io;
import java.io.File;
import java.io.IOException;
public class FileRenameExample {
public static void main(String[] args) throws IOException {
// Creates a new file called OldHouses.csv
File oldFile = new File("OldHouses.csv");
boolean created = oldFile.createNewFile();
System.out.println("File created? " + created);
// Creates the target file.
File newFile = new File("NewHouses.csv");
// The renameTo() method renames file or directory to a
// new name by passing the new destination file.
boolean renamed = oldFile.renameTo(newFile);
System.out.println("File renamed? " + renamed);
}
}
How do I check if a file is hidden?
The File.isHidded() method checks to see if the file represented by aFile object is a hidden file. The definition of hidden is varied between operating systems. On UNIX based systems, a file is considered to be hidden when their name begins with a period character ('.'). On Windows operating system, a file is considered to be hidden if it has been marked hidden in the filesystem.
This File.isHidden() method returns true if and only if the file denoted by this File object is hidden according to the conventions of the underlying platform.
package org.kodejava.io;
import java.io.File;
import java.io.IOException;
public class FileHiddenExample {
public static void main(String[] args) throws IOException {
File file = new File("Hidden.txt");
file.createNewFile();
// We are using the isHidden() method to check whether a file
// is hidden.
if (file.isHidden()) {
System.out.println("File is hidden!");
} else {
System.out.println("File is not hidden!");
}
}
}
If you want to set the file attribute to hidden in Windows operating system you can see it in the following example: How do I set the value of file attributes?
