简体   繁体   中英

Can't Get File to Write on Android Using FileWriter

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();
        }
    }
}

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM