How to Create and Delete Text Files in External Storage Programmatically in Android App Using Android Studio

Hello, How to Create and Delete Text Files in External Storage Programmatically in Android App Using Android Studio We are going to know the vibrancy. There are many interesting facts in this article. Let’s move on to the articles.

How to Create and Delete Text Files in External Storage Programmatically in Android App Using Android Studio

Create and Delete Text Files in External Storage Programmatically in Android App Using Android Studio

package com.programmerworld.createanddeletefile;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import java.io.File;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {
    private File filePath = new File(Environment.getExternalStorageDirectory() + "/ABC.pdf");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE
        , Manifest.permission.READ_EXTERNAL_STORAGE}, PackageManager.PERMISSION_GRANTED);

    }

    public void buttonCreateFile(View view){
        try {
            filePath.createNewFile();
            Toast.makeText(this, "File Created Successfully!", Toast.LENGTH_LONG).show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void buttonDeleteFile(View view){
        if (filePath.exists()){
            filePath.delete();
            Toast.makeText(this, "File Deleted Successfully!", Toast.LENGTH_LONG).show();
        }
    }
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.programmerworld.createanddeletefile">

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="132dp"
        android:layout_marginTop="96dp"
        android:onClick="buttonCreateFile"
        android:text="@string/create_file"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="135dp"
        android:layout_marginTop="67dp"
        android:onClick="buttonDeleteFile"
        android:text="@string/delete_file"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button" />
</androidx.constraintlayout.widget.ConstraintLayout>

Read Also: Build a Location Tracker Android App Using GPS and Google Maps and Geolocation API in Android Studio and Java

Final Words

We hope that you and your doubts have been resolved through the article How to Create and Delete Text Files in External Storage Programmatically in Android App Using Android Studio. And if you have any doubts let us know via the comment box. We cater to your doubts. And if you liked this article, we ask you to share it with your friends.

Hi, I'm Selva a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Deekshi. Here, I post about programming to help developers.

Share on:

Leave a Comment