0

Hey guys I am really struggling with a problem when trying to read a text file in android java.

public String readFromFile(String filename)
{
    String content = "";
    try
    {
        File dir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS) + "/MyApplication");
        File file = new File(dir, filename);

        Scanner scanner = new Scanner(file);
        String line = "";

        while(scanner.hasNextLine())
        {
            line = scanner.nextLine();
            content += line + "\n";
        }
        scanner.close();

    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    return content;
}

I tried extracting the content by using a scanner as well as a buffered reader

public List<String> readFromFile(String filename)
{
    List<String> lines = new ArrayList<String>();
    FileReader fileReader;
    BufferedReader bufferedReader;

    try
    {
        File dir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS) + "MyApplication");
        File file = new File(dir, filename);

        fileReader = new FileReader(file);
        bufferedReader = new BufferedReader(fileReader);

        String line = bufferedReader.readLine();
        while(line != null)
        {
            lines.add(line);
            bufferedReader.readLine();
        }
        bufferedReader.close();
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    return lines;
}

But none of them seems to work. The functions return just zero, as if the file was empty. Also I get no error messages and I double checked the path. Usually those functions work perfectly fine for me, but I couldn't resolve this one.

I appreciate any help :)

EDIT: If found the problem. Everything is fine with the permissions or the location of the file, the problem was simply that I put

FileOutputStream fileOutputStream = new FileOutputStream(file)

in the code before I called the readFromFile() method. I think because of that the readFromFile() method returned that the file was empty. Thank you for your answers!

2 Answers2

1

The second method doesn't work because in your while loop you are not assigning the result of readLine method to line variable. Change this

bufferedReader.readLine();

to this

line = bufferedReader.readLine();

Also, you are missing a / in a file path before MyApplication string.

I tried the first method and it read the file successfully. Check if the file exists, and check it's content. Also, check if your app has permission before reading by calling

if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
            != PackageManager.PERMISSION_GRANTED) {
    Log.e(TAG, "READ_EXTERNAL_STORAGE permission not granted!");
}
Vladimír Bielený
  • 2,795
  • 2
  • 11
  • 16
  • As you said I checked for the files existance and its content. The file appears to be empty when checking for its length although it contains text. Also, I checked for the permission and it is granted. So I don't understand why it shows the file is empty. – Albert Klug Jun 09 '18 at 21:34
  • @AlbertKlug Have you tried it on a different device if it behaves the same? – Vladimír Bielený Jun 09 '18 at 21:54
0

did you give your app read file permission?

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