0

In my Android app I should store the data from user in simple text-file, that I created in the raw directory. After this, I'm trying to write file in APPEND MODE by using simple code from the Google's examples:

try 
{
    FileOutputStream fos = openFileOutput(FILE_NAME, Context.MODE_APPEND);
    fos.write((nameArticle+"|"+indexArticle).getBytes());
    fos.close();    
} 
catch (FileNotFoundException e) 
{               
    e.printStackTrace();
} 
catch (IOException e) 
{
    e.printStackTrace();
}

But nothing happens: no exceptions, but I can see nothing in my FILE_NAME, besides the single record, which was added by me.

What am I doing wrong ? Is it possible at common to write to file in emulator ?

yojimbo87
  • 65,684
  • 25
  • 123
  • 131
Eugene Shmorgun
  • 2,083
  • 12
  • 42
  • 67
  • I'm not sure what you mean? You mention you see the single report that was added by you. What else do you need? Does the file pre-exist and do you mean that your contents wipes out that pre-existing data? – Code Poet Jan 01 '12 at 08:03
  • Sorry (and for misspelling - new year's night), I had have to be more clear - I have the single record in my file raw\bookmarks.txt and I want to add the new line to it. Old record should be kept. – Eugene Shmorgun Jan 01 '12 at 08:11
  • resource files cant be editedat run time. – Padma Kumar Jan 01 '12 at 08:21
  • Does it meanm, that impossible to write the new line in file in txt file? Or I should create file in another place ? At asset ? – Eugene Shmorgun Jan 01 '12 at 08:30

1 Answers1

2

openFileOutput will only allow you to open a private file associated with this Context's application package for writing. I'm not sure where the file you're trying to write to is located. I mean full path. You can use the code below to write to a file located anywhere (as long as you have perms). The example is using the external storage, but you should be able to modify it to write anywhere:

public Uri writeToExternalStoragePublic() {
    final String        filename        = mToolbar.GetTitle() + ".html"; 
    final String        packageName     = this.getPackageName();
    final String        folderpath      = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/" + packageName + "/files/";
    File                folder          = new File(folderpath);
    File                file            = null;
    FileOutputStream    fOut            = null;

    try {
        try {
            if (folder != null) {
                boolean exists = folder.exists();
                if (!exists) 
                    folder.mkdirs();                    
                file = new File(folder.toString(), filename);
                if (file != null) {
                    fOut = new FileOutputStream(file, false);
                    if (fOut != null) {
                        fOut.write(mCurrentReportHtml.getBytes());
                    }
                }
            }
        } catch (IOException e) {
            Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
        }
        return Uri.fromFile(file);
    } finally {
        if (fOut != null) {
            try {
                fOut.flush();
                fOut.close();
            } catch (IOException e) {
                Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
            }
        }
    }
}

In the example you have given, try catching 'I0Exception`, I have a feeling you do not have permission where you are trying to write.

Have a Happy New Year.

Code Poet
  • 11,227
  • 19
  • 64
  • 97
  • Yeh, it works, thanks a lot! But really I don't understand between your code and mine, excepting the checking for existance folder and file...I should analyze,seems to be. – Eugene Shmorgun Jan 01 '12 at 08:55
  • Waht about to store data as internal storage ? My file has small size, so using the external storage is not need. To do it should I have to have in folderpath something like that raw\text.txt? – Eugene Shmorgun Jan 01 '12 at 09:08
  • AFAIK `openFileOutput` creates the files in internal storage, but I does not allow us to specify where the files are created. We can get the full path of the folder where files are created by using `getFilesDir` or the full path of the file using `getFileStreamPath`. I'm not sure why your code fragment may not work if you a folder in your file name like `raw\text.txt`. Try removing the folder component `raw` and see. BTW, the files, I think, are created in `data/data/com.yourpackage`. – Code Poet Jan 01 '12 at 10:49