-2

Im trying to write to a file in my android app.

I dont think its creating the file though.

  1. is the code to correct?

  2. if it is, how do I locate the file on my phone? Is it hidden?

The code resides in an Activity fragment

        String FILENAME = "alarmStatus";
        FileOutputStream fos;
        try {
            fos = getActivity().openFileOutput(FILENAME, MODE_PRIVATE);
            fos.write("Hello".getBytes());
            fos.close();


        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
Squonk
  • 48,735
  • 19
  • 103
  • 135
mogoli
  • 2,153
  • 6
  • 26
  • 41
  • Are you getting an error? Have you stepped through the code? What does the documentation say? Asking a question here should be your fifth or sixth step, not your second (after writing and executing the code.) – Dave Swersky Aug 13 '14 at 22:52
  • Hi Dave thanks for your reply. At first I was getting "Read only File System" errors when I tried to introduce a directory to write the file to. Ive read thedeveloper docs and this I tried to follow their example. http://developer.android.com/guide/topics/data/data-storage.html#filesInternal – mogoli Aug 13 '14 at 22:59
  • 08-14 00:01:20.847: W/System.err(21797): java.io.IOException: open failed: EROFS (Read-only file system) – mogoli Aug 13 '14 at 23:02
  • Trouble is I dont know if the file where the file is, I use a file manager app to try and find it but I cant see it. I also dont know how to change the file permissions upon creation. – mogoli Aug 13 '14 at 23:03
  • You won't be able to find the file using a file manager app as `openFileOutput(...)` creates a file in internal storage which is restricted to your app. I can't think of a valid reason why you're getting an error indicating the file system is read-only though. That method call doesn't require any special permissions. – Squonk Aug 13 '14 at 23:50
  • Did you try just openFileOutput() instead of getActivity().openFileOutput()? – Mobile Developer Aug 14 '14 at 00:02
  • 1
    @MobileDeveloper : The `Fragment` class doesn't have an `openFileOutput` method. That method is only available to classes which extend `ContextWrapper` or `Context`. – Squonk Aug 14 '14 at 00:06
  • Your app needs file writing permissions to write a file. http://stackoverflow.com/questions/2121833/permission-to-write-to-the-sd-card – Dave Swersky Aug 14 '14 at 13:48
  • I have already added to the manifest, doesnt work Im afraid – mogoli Aug 14 '14 at 22:52
  • @Squonk, thanks for your comment, do you know the write way to write and read to a file in a fragment please? – mogoli Aug 14 '14 at 22:53

1 Answers1

0

I think Ive found a solution, abit long winded but it does work, please poke at it :)

      File file = new File(getActivity().getFilesDir() +"alarmStatus.txt");
        OutputStream out = null;

        try {
            out = new BufferedOutputStream(new FileOutputStream(file));
            out.write("Hello".getBytes());
           out.close();

        }catch(Exception e){

        }

        BufferedReader in =null;

        try{
            FileInputStream fis = new FileInputStream(file);
            in = new BufferedReader(new InputStreamReader(fis));
            System.out.println("File Output is "+in.readLine());


        }catch(Exception e){

        }
mogoli
  • 2,153
  • 6
  • 26
  • 41