# 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");
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://java-util.k43.ch/examples/file-tools.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
