filewriter - How to write my file to the Android directory

This is just an early stage test of creating and writing a file to the project directory. I thought this was going to go really quick, but either I'm doing something wrong or perhaps I'm not updating something properly?

All I want to do is to be able to dynamically create and write a file to my raw folder.

I use a couple of methods for other things such as getting my ArrayList of objects, using a for loop to get them individually, and then using "write" to write to the file.

Here is my code: The file I want to create and write to is called a txt file called output.

I really appreciate anyone looking at this for me, thank you very much!

//for writing file...
  public static void writeGrids(List<Grid> grids)
  {
      for(int i = 0; i < grids.size(); i++)
      {

          try {
            write(grids.get(i));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
      }
  }

 static void write(Grid grid) throws IOException
  {

     try
     {
     FileWriter fstream = new FileWriter("/My Project/res/raw/output.txt");
     BufferedWriter out = new BufferedWriter(fstream);
     //FILE DIR!!!!!!

          out.write(grid.gridNickName + ";" + grid.gridName + ";" +
                  grid.firstName + ";" + grid.lastName + ";" +
                  grid.password + ";" + grid.loginURI + ";");

     out.close();
     }
     catch (Exception e)
     {
         System.err.println("Error: " + e.getMessage());
     }

  }

This is an updated version of trying to save something to the internal storage. Would this be an ideal solution?

UPDATED CODE:

//for writing file...
public void writeGrids(List<Grid> grids)
{
      for(int i = 0; i < grids.size(); i++)
      {

          try {
            write(grids.get(i));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
      }
}

void write(Grid grid) throws IOException
{    

     try
     {
      FileOutputStream fos;
     String FILENAME = "InternalString";

     fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
     String data = grid.getGridNickName() + ";" + grid.getGridName() + ";" +
              grid.getFirstName() + ";" + grid.getLastName() + ";" +
              grid.getPassword() + ";" + grid.getLoginURI() + ";";
     fos.write(data.getBytes());
     fos.close();

     }
     catch (Exception e)
     {
         System.err.println("Error: " + e.getMessage());
     }

}

The user would call the write method to get things started like so.. writeGrids(myNewArrayList);

2 Answers

  1. Dennis- Reply

    2019-11-16

    The "My project" folder only exists on your development machine, so cannot be written by the device. While the contents of the res/raw subfolder do end up on the device, they end up there in a read-only (as far as your app's userid is concerned) form.

    You will instead need to store your runtime-changeable data either in the applications internal storage directory - look up getFileDir() and related methods

    or on the External Storage (ie, sdcard) if present. If and only if you are going to write to the sdcard would you need the sdcard permission in the application manifest.

    If you are putting your data in a subdirectory of either of those locations, you will have to make sure that all directories comprising the file location actually exist, or otherwise create them.

    If you want to package data which you can also change, you will have to first copy the packaged data to one of these two locations where you can have changeable data.

Leave a Reply

Your email address will not be published. Required fields are marked *

You can use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>