# FileUtil

`FileUtil` class provides utility methods for working with files in your application. It includes methods for reading, creating, and managing files.

### Methods

#### `readFile(filePath: String): File`

Reads the content of a file located at the specified `filePath` and returns a `File` object representing the file.

* Parameter&#x73;**:**
  * `filePath` (String): The path to the file to be read.
* Return&#x73;**:**
  * `File`: A `File` object representing the read file.

```typescript
// Read a file
var filePath = "path/to/your/file.txt";
var file = FileUtil.readFile(filePath);
```

***

#### `readInputStream(inputStream: InputStream, relativeIterationPath: String): File`

Reads the content of an input stream and creates a `File` object. The `relativeIterationPath` is used to determine the file path for the created `File` object.

* Parameter&#x73;**:**
  * `inputStream` (InputStream): The input stream to read.
  * `relativeIterationPath` (String): The relative path for the created `File` object.
* Return&#x73;**:**
  * `File`: A `File` object representing the read content.

```typescript
var inputStream = ...; // Your input stream source

// Relative path for the created File object
var relativeIterationPath = "output/sample.txt";

// Call the method to read the input stream and create a File object
var file = FileUtil.readInputStream(inputStream, relativeIterationPath);

// Now, you can work with the created File object
// For example, you can check the file's absolute path
var absolutePath = file.getAbsolutePath();
return "Absolute Path: " + absolutePath;
```

***

#### `createFile(filePath: String, content: String): File`

Creates a new file at the specified `filePath` with the provided `content`.

* Parameter&#x73;**:**
  * `filePath` (String): The path for the new file.
  * `content` (String): The content to be written to the file.
* Return&#x73;**:**
  * `File`: A `File` object representing the newly created file.

```typescript
// Create a file
var newFilePath = "path/to/newfile.txt";
var fileContent = "This is the content of the new file.";
var newFile = FileUtil.createFile(newFilePath, fileContent);
```

***

#### `createTempFile(fileName: String, content: String): File`

Creates a temporary file with the specified `fileName` and writes the provided `content` to it.

* Parameter&#x73;**:**
  * `fileName` (String): The name for the temporary file.
  * `content` (String): The content to be written to the file.
* Return&#x73;**:**
  * `File`: A `File` object representing the created temporary file.

```typescript
// Create a temp file
var newFilePath = "path/to/newfile.txt";
var fileContent = "This is the content of the new file.";
var newFile = FileUtil.createTempFile(newFilePath, fileContent);
```

***

#### `readFileContent(fileName: String): String`

Reads the content of a file with the specified `fileName` and returns it as a string.

* Parameter&#x73;**:**
  * `fileName` (String): The name of the file to read.
* Return&#x73;**:**
  * `String`: The content of the file as a string.

```typescript
// Read file content
var fileToRead = "path/to/your/anotherfile.txt";
var fileContent = FileUtil.readFileContent(fileToRead);
return "File Content: " + fileContent;
```

***

### Examples

**Logging Data to a File:**

```java
// Logging business operation
var logFilePath = "logs/business_log.txt";
var logMessage = "User John Doe performed a critical operation.";

// Create or append to the log file
var logFile = FileUtil.createFile(logFilePath, logMessage);

// Log the operation details for auditing purposes
return "Operation logged: " + logFile.getAbsolutePath();
```

**Generating Reports:**

```java
// Generating a business report
var reportName = "Q3_Sales_Report.pdf";
var reportData = "Quarterly sales report data...";

// Create a temporary report file
var tempReport = FileUtil.createTempFile(reportName, reportData);

// In a real scenario, you might generate a PDF report and save it temporarily
// Provide a download link to the user for the report
return "Report generated: " + tempReport.getAbsolutePath();
```

**Reading Customer Data:**

```java
// Reading customer data from a file
var customerFile = "customer_profiles/customer123.txt";

// Read the customer data
var customerData = FileUtil.readFileContent(customerFile);

// Process and display customer information in your application
return "Customer Data:\n" + customerData;
```

**Data Backup and Recovery:**

```java
// Creating a backup of important data
var dataFilePath = "data/important_data.csv";
var dataToBackup = FileUtil.readFileContent(dataFilePath);

// Create a backup file with a timestamp
var backupFileName = "data_backup_" + System.currentTimeMillis() + ".csv";
var backupFile = FileUtil.createFile("backups/" + backupFileName, dataToBackup);

// Create a backup of important data for disaster recovery
return "Data backup created: " + backupFile.getAbsolutePath();
```


---

# 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://docs.b2winsuite.com/5.2/scripting/static-classes/fileutil.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.
