---
title: "File I/O"
description: "Learn more about the Sentry file I/O integration for the Android SDK."
url: https://docs.sentry.io/platforms/android/integrations/file-io/
---

# File I/O | Sentry for Android

Supported in Sentry's Android SDK version `5.5.0` and above.

Supported in Sentry Android Gradle Plugin version `3.0.0` and above.

The [Sentry Android Gradle Plugin](https://docs.sentry.io/platforms/android/configuration/gradle.md) provides file I/O support through bytecode manipulation. The source can be found [on GitHub](https://github.com/getsentry/sentry-android-gradle-plugin/tree/main/plugin-build/src/main/kotlin/io/sentry/android/gradle/instrumentation).

On this page, we get you up and running with Sentry's file I/O integration, so that it will automatically start a span from an active transaction that's bound to the scope of each file input/output stream operation.

## [Install](https://docs.sentry.io/platforms/android/integrations/file-io.md#install)

To use the file I/O integration, add the Sentry Android Gradle plugin in `build.gradle`:

```groovy
plugins {
  id "io.sentry.android.gradle" version "6.4.0"
}
```

Make sure, that [tracing](https://docs.sentry.io/platforms/android/tracing.md#configure-the-sample-rate) is enabled.

## [Configure](https://docs.sentry.io/platforms/android/integrations/file-io.md#configure)

In general, no further configuration is required as the auto-instrumentation is enabled by default. If you would like to disable the file I/O instrumentation feature, we expose a configuration option for that:

```groovy
import io.sentry.android.gradle.extensions.InstrumentationFeature

sentry {
  tracingInstrumentation {
    enabled = true
    features = EnumSet.allOf(InstrumentationFeature) - InstrumentationFeature.FILE_IO
  }
}
```

## [Verify](https://docs.sentry.io/platforms/android/integrations/file-io.md#verify)

Assuming you have the following (reduced) code snippet performing a file I/O operation:

```kotlin
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.activity.ComponentActivity
import io.sentry.Sentry
import io.sentry.SpanStatus
import java.io.File

class LyricsActivity : ComponentActivity() {

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    findViewById<Button>(R.id.load_lyrics).setOnClickListener {
      val transaction = Sentry.startTransaction(
        name = "Track Interaction",
        operation = "ui.action.lyrics",
        bindToScope = true
      )

      val file = File(context.filesDir, "lyrics.txt")

      val lyricsTextView = findViewById<TextView>(R.id.lyrics)
      lyricsTextView.setText(file.readText())

      transaction.finish(SpanStatus.OK)
    }
  }
}
```

To view the recorded transaction, log into [sentry.io](https://sentry.io) and open your project. Clicking **Performance** will open a page with transactions, where you can select the just recorded transaction with the name `Track Interaction`. The event will look similar to this:

At the moment, we only support `java.io.FileInputStream`, `java.io.FileOutputStream`, `java.io.FileReader` and `java.io.FileWriter` classes for auto-instrumentation.

## [(Optional) Custom Instrumentation](https://docs.sentry.io/platforms/android/integrations/file-io.md#optional-custom-instrumentation)

Alternatively, if you are not using our Gradle Plugin, the Android SDK provides the capability to manually instrument file I/O operations through the custom Sentry-specific implementations.

To instrument `FileInputStream`/`FileOutputStream` the SDK provides drop-in replacements such as `SentryFileInputStream`/`SentryFileOutputStream`.

*Old*:

```java
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

File file1 = new File("file1.txt");
File file2 = new File("file2.txt");
try (FileInputStream fis = new FileInputStream(file1)) {
  byte[] buffer = new byte[1024];
  try (FileOutputStream fos = new FileOutputStream(file2)) {
    int read;
    while (true) {
      read = fis.read(buffer);
      if (read == -1) {
        break;
      }
      fos.write(buffer, 0, read);
    }
  }
}
```

*New*:

```java
import io.sentry.instrumentation.file.SentryFileInputStream;
import io.sentry.instrumentation.file.SentryFileOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

File file1 = new File("file1.txt");
File file2 = new File("file2.txt");
try (FileInputStream fis = new SentryFileInputStream(file1)) {
  byte[] buffer = new byte[1024];
  try (FileOutputStream fos = new SentryFileOutputStream(file2)) {
    int read;
    while (true) {
      read = fis.read(buffer);
      if (read == -1) {
        break;
      }
      fos.write(buffer, 0, read);
    }
  }
}
```

Similarly, `FileReader`/`FileWriter` can be instrumented through `SentryFileReader`/`SentryFileWriter`:

*Old*:

```java
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

File file1 = new File("file1.txt");
File file2 = new File("file2.txt");
try (FileReader reader = new FileReader(file1)) {
  char[] buffer = new char[1024];
  try (FileWriter writer = new FileWriter(file2, true)) {
    int read;
    while (true) {
      read = reader.read(buffer, 0, buffer.length);
      if (read == -1) {
        break;
      }
      writer.write(buffer, 0, buffer.length);
    }
    writer.flush();
  }
}
```

*New*:

```java
import io.sentry.instrumentation.file.SentryFileReader;
import io.sentry.instrumentation.file.SentryFileWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

File file1 = new File("file1.txt");
File file2 = new File("file2.txt");
try (FileReader reader = new SentryFileReader(file1)) {
  char[] buffer = new char[1024];
  try (FileWriter writer = new SentryFileWriter(file2, true)) {
    int read;
    while (true) {
      read = reader.read(buffer, 0, buffer.length);
      if (read == -1) {
        break;
      }
      writer.write(buffer, 0, buffer.length);
    }
    writer.flush();
  }
}
```
