# File Tools

### 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

* [org.json.JSONObject](https://mvnrepository.com/artifact/org.json/json)

### Example Delete File

```java
KFile.delete("temp.txt);
```

### Example Read JSON File

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

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

### Example Create JSON File

```java
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

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

if (!K.isEmpty(fileBuffer) {
   KFile.writeBytes(fileBuffer, "outputfile");
}
```
