Java don't see the file

34 views Asked by At

`MainActivity:

` package com.example.game;

import android.view.Window;
import android.view.View;
import android.widget.ImageView;
import android.widget.Button;
import android.widget.TextView;
import android.app.Activity;
import android.os.Bundle;
import java.util.HashMap;



public class MainActivity extends Activity {

    public Database db = new Database();

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main_activity);


        // inizializating varibales
        db.main();
    }
}`

Database:

`package com.example.game;

import java.lang.Exception;
import java.io.*;
import java.nio.file.Path;

public class Database {

    public static void main(){
        try {

            FileReader fr = new FileReader("user.txt");
            fr.close();
        } catch (Exception e){
            e.printStackTrace();
        }
    }
}``

I'm tried to add READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE to android manifest

Then I'm tried to check files in my dir by MainActivity program and it shows nothing.

2

There are 2 answers

4
Aimar On

I think you have not defined the path

try {

        FileReader fr = new FileReader("user.txt");
        fr.close();
    } catch (Exception e){
        e.printStackTrace();
    }

user.txt

instead you should use like

FileReader fr = new FileReader("/sdcard/Downloads/user.txt");

or the main path where user.txt exists

Hope it Helps!

1
Deadli_wolf On

I found the problem, you might enter the path like /data/data/(package name)/files/your_file.txt or enter other path to app storage.