🔨
Java Utility Package
🔨
Java Utility Package
  • Java Utility Package (Open Source)
  • Examples
    • 📖Logging
    • 🔧Utility Class
    • 🔑Password Vault
    • 🖥️HTTP / Socket Server
    • ⚒️HTTP Client
    • 🖥️Socket Client
    • 🗃️JDBC Database
    • ✉️SMTP Mailer
    • ⚔️Java Thread
    • 🗄️File Tools
    • ⌚Timer
    • 💡Tips / FAQ
  • Downloads
    • ⬇️Package ch.k43.util
    • ⬇️Sample Code
    • 📖JavaDoc API
    • 👨‍💻GitHub
Powered by GitBook
  1. Examples

File Tools

Simple file manipulation

Last updated 4 months ago

KFile Class Overview

The KFile class provides a versatile set of methods for managing file operations, supporting a wide range of use cases including reading, writing, renaming, and deleting files. Its straightforward API ensures efficient handling of various file formats and operations.

Key Features

  • Read and Write File Content - Binary Files: Supports reading and writing entire binary files for seamless handling of non-text data. - Character Files: Enables reading and writing text-based files with full encoding support. - Java Properties: Simplifies interaction with .properties files, allowing easy serialization and deserialization of key-value pairs. - JSON Objects: Facilitates reading and writing JSON data to and from files using JSONObject.

  • File Renaming and Deletion - Rename Files: Provides a method to rename files, ensuring atomic operations where supported by the underlying filesystem. - Delete Files: Allows safe and efficient file deletion with error handling for cases like missing or locked files.

  • Additional File Operations Includes various helper methods for other file-related tasks.

Use Cases

  • Data Serialization Manage binary, textual, and structured data formats like JSON or properties files.

  • File Maintenance Perform common file management tasks such as renaming and deletion.

  • Utility for Applications Ideal for applications requiring flexible and reliable file handling capabilities.

  • Read/Write Read and write entire entire file content (binary, character, Java Properties or JSONObject data)

Prerequisites

Example Delete File

KFile.delete("temp.txt);

Example Read JSON File

JSONObject jsonData = KFile.readJSONFile("text.json");

if (jsonData == null) {
   KLog.abort("Unable to read JSON file");
}

Example Create JSON File

JSONObject jsonObject = new JSONObject();
jsonObject.put("lastName", "Smith");
jsonObject.put("firstName", "John");
jsonObject.put("gender", "male");
KFile.writeFile(jsonObject, "test.json");

Example Copy Binary File

byte[] fileBuffer = KFile.readByteFile("inputFile");

if (!K.isEmpty(fileBuffer) {
   KFile.writeBytes(fileBuffer, "outputfile");
}
org.json.JSONObject
🗄️
Page cover image