Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing 'FileWriter' to Write to a Text File on an SD Card in Android
    text
    copied!<p>I've researched this problem for a while now and I've only found really complicated answers so I'm very confused. Keep in mind that I'm not an expert programmer so don't expect me to know a ton about this!</p> <p>All I want to do is print a new line of characters to a text file located in the downloads folder of an SD card in Android. I set up my emulator to have an SD card and placed the text file in the downloads folder. This piece of code is for a database class that will access a text file in an SD card to read the data. I know that the class works outside of Android so assume that all of the methods are working as they should to read the data!</p> <p>I get an IOException when I run this method in another class:</p> <pre><code>public void addRecordToDataBase(ChildRecord c) throws IOException { FileWriter outFile = new FileWriter("/mnt/sdcard/download/database.txt"); PrintWriter out = new PrintWriter(outFile); out.println(c.printToDataBase()); out.close(); } </code></pre> <p>The weird thing is I can read from the database just fine in other methods using that same path; no problems there. I just can't write to it. I've read somewhere that you can use "regular Java methods" to write to an SD card in Android without those crazy "OutputStream" things all over the place. Is this true? I debugged this thing and found out that the line of code that is throwing the exception is right here:</p> <pre><code>FileWriter outFile = new FileWriter("/mnt/sdcard/download/database.txt"); </code></pre> <p>If anyone has any idea why I'm geting this IOException, I would be really grateful! I did try all the crazy methods that Android wants to use but I think I got lost in it so I just reverted back to what I knew how to do.</p> <p>Thank you so much!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload