2

I am having this exception when trying to read from the file

java.io.FileNotFoundException: /data/data/.../files

I used this method because it can handle Unicode text while reading from the file

public void save(String string )
{

String filename = "main";


FileOutputStream outputStream;

try {
  outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
  outputStream.write(string.getBytes());
  outputStream.close();
} catch (Exception e) {
  e.printStackTrace();
}
}
public String read()
{
    try
    {

        Reader readerUnicode =
                new InputStreamReader(new FileInputStream(getFilesDir()), Charset.forName("UTF-16"));
                int e = 0;
                String f="";
                while ((e = readerUnicode.read()) != -1) {
                // cast to char. The casting removes the left most bit.
                 f = f+Character.toString((char) e);
                System.out.print(f);
                }

                return f;
    }
    catch(Exception e)
    {

        return e+"";
    }

}

how can I retrieve the internal save path

thanks

  • can you illustrate more plz – user3027345 Oct 21 '14 at 14:49
  • I have, see my answer below :) – IAmGroot Oct 21 '14 at 14:54
  • @Doomsknight - no, this is not a duplicate. And the target of your link is a horribly confused question anyway, where the poster says "internal" but actually means "external". – Chris Stratton Oct 21 '14 at 15:08
  • @ChrisStratton I noticed the variation, it was linked from this one http://stackoverflow.com/questions/20280250/how-to-get-file-path-of-file-from-internal-storage-in-android?lq=1 Which does seem similar. They all have good ideas of what might be the issue. I am unsure of if the `MODE_PRIVATE` is an issue. Im happy to see the solution from anyone, although im sure he was missing the file name at the least. – IAmGroot Oct 21 '14 at 15:13
  • @Doomsknight - one does not propose a duplicate when something merely has "good ideas of what *might* be the issue". Duplicates are for when the problem (or at least the solution) is **known for a fact** to be the same. – Chris Stratton Oct 21 '14 at 15:15
  • @ChrisStratton Ive removed it, but its still a duplicate of http://stackoverflow.com/questions/20280250/how-to-get-file-path-of-file-from-internal-storage-in-android?lq=1 . Either way, thats why it requires more than just one vote to close. Incase people get it wrong. – IAmGroot Oct 21 '14 at 15:20

1 Answers1

1

You are using getFilesDir() But not setting the actual file name. Just the directory path.

Try adding the file name in. Plus, you should probably add an extension like .txt to both the save and load path.

 new InputStreamReader(new FileInputStream(getFilesDir() + "/" + filename ), Charset.forName("UTF-16"));

and change filename to something more sensible.

String filename = "main.txt";

You could/should also check the file exists before accessing it. (Although you do try catch anyway)

File file = new File(getFilesDir() + "/" + filename);
if(!file.exists()) 
   return ""; 
IAmGroot
  • 13,760
  • 18
  • 84
  • 154
  • same error java.io.FileNotFoundException: /data/data/asia.tv/files/main: open failed: ENOENT (No such file or directory) where asia.tv is the name of my main package but I am calling it from different package named news – user3027345 Oct 21 '14 at 14:55
  • @user3027345 Try the variation that sets the extension. Are you sure the file actually exists?... Have you called `save()` at any point before loading – IAmGroot Oct 21 '14 at 14:56
  • I did the save as shown in my main code above but i used this code: try{ FileInputStream fin = openFileInput("main"); int c; String temp=""; while( (c = fin.read()) != -1){ temp = temp + Character.toString((char)c); } return temp; }catch(Exception e){ return "no"; } and it worked but my main problem was with unicode reading .... but anyway i didn't know where does it save the data – user3027345 Oct 21 '14 at 15:00
  • @user3027345 Yes, `openFileInput` prepends the folder location for you. You need to use `getFilesDir` when using `FileInputStream`. – IAmGroot Oct 21 '14 at 15:03