# Access Local Files

We can easily use the native Node.js API to implement the functionality of accessing local files. This makes it easier for us to perform such tasks in the plugin system.

***

## Using the `fs` module to access local files <a href="#iamlo" id="iamlo"></a>

Using a series of methods from Node.js's `fs` module, we can perform operations on the local file system to access local files. For example, you can use the `fs.readFile()` method to read the contents of a file and the `fs.writeFile()` method to write to a file. Here is an example:

```javascript
const fs = require('fs');

// Read the file
fs.readFile('/path/to/file', (err, data) => {
  if (err) throw err;

  console.log(data);
});

// Write to the file
fs.writeFile('/path/to/file', 'hello world', (err) => {
  if (err) throw err;

  console.log('done');
});
```

These methods are asynchronous, so they won't block the UI, ensuring high performance for the application. Additionally, the `fs` module provides other useful methods, such as using `fs.stat()` to obtain file size, creation time, and other information, or using `fs.rename()` to rename a file. By using the `fs` module, we can conveniently access local files.

{% hint style="success" %}
**🦄 Best practice:** Avoid using synchronous methods in Node.js as much as possible, as these methods can cause UI thread blocking, leading to a user interface lag and poor user experience. Moreover, using asynchronous methods improves program execution efficiency because it does not block program execution and can handle multiple tasks simultaneously.
{% endhint %}

***

## Use native dialogs to get file paths <a href="#nyq1o" id="nyq1o"></a>

Eagle Plugin API provides a `dialog` module that can be used to create native system dialogs for file saving and selection. Here are some examples:

**Example 1: Pop-up file selection dialog**

```javascript
let result = await eagle.dialog.showOpenDialog({
    properties: ['openFile', 'openDirectory']
});
```

**Example 2: Pop-up save dialog**

```javascript
let result = await eagle.dialog.showSaveDialog({
    properties: ['openDirectory']
});
```

If you need a more detailed introduction, you can refer to the [dialog module](https://developer.eagle.cool/plugin-api/api/dialog).
