-1

I'm new to both Java and Android Studio, but I've been teaching myself recently. Currently, I'm trying to figure out how to write and read simple text documents to the Android device.

I've seen a few different ways of doing this online but pretty much all of them have to do with writing and reading bytes. I don't really understand all of that at this stage and I just want to write/read text documents. I could just copy and paste the stuff I've seen online and see if that works, but I'd prefer to have at least some understanding of what the code is doing. I've tried to read the online documentation for a lot of the stuff I've seen other people use to achieve this goal and I find it hard to understand.

The solution I found and worked fine running Java from the command line on my computer isn't working when I put it in my MainActivity.java, and run the emulator. I'm not sure if its a file path issue or a permissions issue, but I'm hoping someone here can tell me why this code won't write text documents to the Android internal storage.

package com.example.test;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Scanner;

public class MainActivity extends AppCompatActivity {
    static File testFile = new File("test_file.txt");
    static TextView textDisplay;

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

        textDisplay = findViewById(R.id.textDisplay);
        saveData();
    }

    public static void saveData() {
        String text = String.format("%s,%s,%s,%s,%s", 7, 3, 4, 9, 2);
        try {
            FileWriter textWriter = new FileWriter(testFile);
            textWriter.write(text);
            textWriter.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void loadData(View view) {
        ArrayList<Integer> intList = new ArrayList<>();
        try {
            Scanner readFile = new Scanner(testFile).useDelimiter(",");
            while (readFile.hasNext()) {
                Integer data = Integer.parseInt(readFile.next());
                intList.add(data);
            }
            readFile.close();
            textDisplay.setText(intList.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Niroshan Ratnayake
  • 3,433
  • 3
  • 20
  • 18
  • You wrote in your question: _isn't working when I put it in my MainActivity.java_ Can you expand? Are you getting an error? Is the file not being created? Is it created but empty? – Abra Apr 03 '20 at 04:41

2 Answers2

1

I think you have both problems here:

to write and read the text file you will need something like is explained here:how to read and write

but you also need to require the permisions with a code like this in your activity:

static void requestPermissoes(Activity act) throws Exception {
        try {
            ActivityCompat.requestPermissions(act,
                    new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,
                            Manifest.permission.WRITE_EXTERNAL_STORAGE,
                            Manifest.permission.CAMERA,
                    }, 0x3);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

and add a line in your manifest with the permisions:

 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

note: android has a different way to refer to external storage (it is not equals to SD Card external in android is like external to your app files but not in SD Card) try take a look to this description

thiago
  • 103
  • 5
  • Cool thank you very much! It didn't turn out to be a permissions issue, I'm only trying to write to the internal storage right now, which I read requires no special permission. My issue lied with the getFilesDir method. I kept trying to use it but couldn't get it to work. I didn't understand context properly, but I was able to make a few changes and it all works now!! Thank you for your time and help!! :) – ItWasntZak Apr 03 '20 at 13:39
0

To Write and read a file first we need manifest permission and we have to take it runtime.After that we need file path to save the file and read the file.solve these issue you will definitely success.

shakac
  • 379
  • 4
  • 15
  • It didn't turn out to be an issue of permission, I was trying to write to the internal storage of the device so no permissions needed. I did have to learn how context worked though so I could use getFilesDir properly. I got it working now, thank you so much!! – ItWasntZak Apr 03 '20 at 13:41