> ## Documentation Index
> Fetch the complete documentation index at: https://starrycodes.mintlify.app/llms.txt
> Use this file to discover all available pages before exploring further.

# File - Operations

>  Powerful extensions for dart:io.File to simplify size formatting, path manipulation, and type checking for local files.

<Steps titleSize="h3">
  <Step title="Delete Operations">
    <ResponseField name="deleteIfExistsSafe()" type="Future<bool>">
      This method first checks for the file's existence using `exists()` before attempting to call `delete()`. It is designed to be a non-crashing wrapper for file cleanup.

      ```dart theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
      final file = File('path/to/temp.log');
      final success = await file.deleteIfExistsSafe();

      print('Deletion status: $success'); // true or false
      ```
    </ResponseField>
  </Step>

  <Step title="Read and Access">
    <ResponseField name="readAsJsonSafe()" type="Future<Map<String, dynamic>?>">
      A non-crashing wrapper for reading and parsing JSON files.

      It handles three common failure modes gracefully:

      1. File does not exist.
      2. File exists but cannot be read (e.g., permission error).
      3. File content is not valid JSON.

      ```dart lines wrap theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
      final configFile = File('config.json');
      final config = await configFile.readAsJsonSafe();

      if (config != null) {
          print('API Key: ${config['apiKey']}'); //  Output: API Key: jsonContent
      }
      ```
    </ResponseField>

    <ResponseField name="readAsBytesSafe()" type="Future<Uint8List?>">
      A safe wrapper around `readAsBytes()` that handles file existence and read errors gracefully. It is suitable for loading image or audio data.

      <Warning>
        Avoid calling this on the main thread for very large files.
      </Warning>

      ```dart lines wrap theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
      final imageBytes = await File('photo.jpg').readAsBytesSafe();

      if (imageBytes != null) {
          // Use the bytes to create an Image.memory widget
      }
      ```
    </ResponseField>
  </Step>

  <Step title="Getters">
    <ResponseField name="lengthSyncSafe" type="int">
      A robust wrapper around `lengthSync()`, preventing crashes if the file is deleted or becomes inaccessible between checks.

      ```dart theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
      // Safe to call inside a loop or initialization code
      final size = File('temp.log').lengthSyncSafe;

      if (size > 1000) {
          print('File is big.');
      }
      ```
    </ResponseField>

    <ResponseField name="lastModifiedSyncSafe" type="DateTime?">
      Safely gets the file's synchronous last modified timestamp without throwing an exception.

      ```dart theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
      final lastChange = File('cache.json').lastModifiedSyncSafe;

      if (lastChange.year == 1970) {
          print('Could not determine modification date.');
      }
      ```
    </ResponseField>
  </Step>

  <Step title="Comparators">
    <ResponseField name="isSameFile()" type="bool">
      <Expandable title="Parameters">
        <ParamField body="other" type="File" required>
          The other `File` object to compare against.
        </ParamField>
      </Expandable>

      Checks if two `File` objects point to the same file location on the filesystem.

      ```dart theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
      final fileA = File('data.txt');
      final fileB = File('./data.txt').absolute;

      print(fileA.isSameFile(fileB)); // Likely true
      ```
    </ResponseField>
  </Step>
</Steps>
