New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@capacitor/filesystem

Package Overview
Dependencies
Maintainers
8
Versions
732
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@capacitor/filesystem

The Filesystem API provides a NodeJS-like API for working with files on the device.

  • 7.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
108K
decreased by-13.18%
Maintainers
8
Weekly downloads
 
Created

What is @capacitor/filesystem?

@capacitor/filesystem is a Capacitor plugin that provides a set of APIs for reading, writing, and managing files on the device's filesystem. It allows developers to interact with the filesystem in a way that is consistent across different platforms, such as iOS, Android, and the web.

What are @capacitor/filesystem's main functionalities?

Read a file

This feature allows you to read the contents of a file from the device's filesystem. The code sample demonstrates how to read a file named 'text.txt' from the 'Documents' directory and log its contents to the console.

const { Filesystem, Directory } = require('@capacitor/filesystem');

async function readFile() {
  try {
    const contents = await Filesystem.readFile({
      path: 'secrets/text.txt',
      directory: Directory.Documents,
      encoding: 'utf8'
    });
    console.log('File contents:', contents.data);
  } catch (e) {
    console.error('Unable to read file', e);
  }
}

readFile();

Write a file

This feature allows you to write data to a file on the device's filesystem. The code sample demonstrates how to write the string 'This is a test' to a file named 'text.txt' in the 'Documents' directory.

const { Filesystem, Directory } = require('@capacitor/filesystem');

async function writeFile() {
  try {
    await Filesystem.writeFile({
      path: 'secrets/text.txt',
      data: 'This is a test',
      directory: Directory.Documents,
      encoding: 'utf8'
    });
    console.log('File written successfully');
  } catch (e) {
    console.error('Unable to write file', e);
  }
}

writeFile();

Delete a file

This feature allows you to delete a file from the device's filesystem. The code sample demonstrates how to delete a file named 'text.txt' from the 'Documents' directory.

const { Filesystem, Directory } = require('@capacitor/filesystem');

async function deleteFile() {
  try {
    await Filesystem.deleteFile({
      path: 'secrets/text.txt',
      directory: Directory.Documents
    });
    console.log('File deleted successfully');
  } catch (e) {
    console.error('Unable to delete file', e);
  }
}

deleteFile();

Create a directory

This feature allows you to create a new directory on the device's filesystem. The code sample demonstrates how to create a directory named 'secrets' in the 'Documents' directory.

const { Filesystem, Directory } = require('@capacitor/filesystem');

async function createDirectory() {
  try {
    await Filesystem.mkdir({
      path: 'secrets',
      directory: Directory.Documents,
      recursive: false
    });
    console.log('Directory created successfully');
  } catch (e) {
    console.error('Unable to create directory', e);
  }
}

createDirectory();

List files in a directory

This feature allows you to list all files in a specified directory on the device's filesystem. The code sample demonstrates how to list all files in the 'secrets' directory within the 'Documents' directory.

const { Filesystem, Directory } = require('@capacitor/filesystem');

async function listFiles() {
  try {
    const result = await Filesystem.readdir({
      path: 'secrets',
      directory: Directory.Documents
    });
    console.log('Files in directory:', result.files);
  } catch (e) {
    console.error('Unable to list files', e);
  }
}

listFiles();

Other packages similar to @capacitor/filesystem

Keywords

FAQs

Package last updated on 20 Jan 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc