File Utils

@Alex Ljunggren Alex Ljunggren authored on 23 May 2025
src Removed unused code 1 year ago
.gitignore Initial commit 4 years ago
Jenkinsfile New file 2 years ago
README.md Added write to file for inputstreams 1 year ago
pom.xml Added build, distributionManagement, scm 2 years ago
README.md

File Utils

A collection of useful file utilities.

Get File

File file = FileUtils.get(path);
File file = FileUtils.get(pathString);

Check if File exists

boolean exists = FileUtils.exists(file);
boolean exists = FileUtils.exists(path);
boolean exists = FileUtils.exists(pathString);

Create Directories

Path path = FileUtils.createDirectories(path);
Path path = FileUtils.createDirectories(pathString);

Read File to string

String content = FileUtils.readFile(file);
String content = FileUtils.readFile(path);
String content = FileUtils.readFile(pathString);

Parse File to List

List<String> lines = FileUtils.parseFile(file);
List<String> lines = FileUtils.parseFile(path);
List<String> lines = FileUtils.parseFile(pathString);

Create File

FileUtils.createFile(path);
FileUtils.createFile(pathString);

Create File with content

FileUtils.createFile(path, content);
FileUtils.createFile(pathString, content);

Write to File

FileUtils.writeToFile(path, content);
FileUtils.writeToFile(pathString, content);

Write to File list of contents

List<String> contents = ...
FileUtils.writeToFile(path, contents);
FileUtils.writeToFile(pathString, contents);

Write to File input steam

InputStream inputStream = ...
FileUtils.writeToFile(path, inputStream);
FileUtils.writeToFile(pathString, inputStream);

Append to File

FileUtils.writeToFile(path, content);
FileUtils.writeToFile(pathString, content);

Append to File list of contents

List<String> contents = ...
FileUtils.appendToFile(path, contents);
FileUtils.appendToFile(pathString, contents);

Append To File with new line

FileUtils.writeToFile(path, content);
FileUtils.writeToFile(pathString, content, true);

Append to File list of contents with new line

List<String> contents = ...
FileUtils.appendToFile(path, contents);
FileUtils.appendToFile(pathString, contents, true);

Rename File

FileUtils.renameFile(path, filename);
FileUtils.renameFile(pathString, filename);

Move File

FileUtils.moveFile(sourcePath, targetPath);
FileUtils.moveFile(sourcePathString, targetPathString);

Copy File

FileUtils.copyFile(sourcePath, targetPath);
FileUtils.copyFile(sourcePathString, targetPathString);

Truncate File

FileUtils.truncateFile(path);
FileUtils.truncateFile(pathString);

Delete File

FileUtils.deleteFile(path);
FileUtils.deleteFile(pathString);
FileUtils.deleteFile(file);

Delete Directory

FileUtils.deleteDirectory(path);
FileUtils.deleteDirectory(pathString);
FileUtils.deleteDirectory(file);

List Files (not including directories)

List<File> files = FileUtils.listFiles(path);
List<File> files = FileUtils.listFiles(pathString);

List Directories

List<File> directories = FileUtils.listDirectories(path);
List<File> directories = FileUtils.listDirectories(pathString);

Filter Files by prefix

List<File> filteredFiles = FileUtils.filterByPrefix(files, "prop-");

Note: This will return files that start with "prop-"

Filter Files by suffix

List<File> filteredFiles = FileUtils.filterBySuffix(files, ".txt");

Note: This will return files that end with ".txt"

Order Files by last modified (touch) date

List<File> orderedFiles = FileUtils.orderByLastModifiedDate(files);

Order Files by last modified (touch) date descending

List<File> orderedFiles = FileUtils.orderByLastModifiedDate(files, true);

Read resource File

String content = FileUtils.readResourceFile(this.getClass(), pathString);

Parse resource File

String lines = FileUtils.parseResourceFile(this.getClass(), pathString);

File Extension

String extension = FileUtils.getFileExtension(file);

File Size

long size = FileUtils.getSize(file);

Human Readable Size

String humanReadableSize = FileUtils.getHumanReadableSize(file);