Write String From A File in Android - Stack Overflow
Write String From A File in Android - Stack Overflow
1 of 5
http://stackoverflow.com/questions/14376807/how-to-read-write-string-...
sign up
log in
tour
help
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no
registration required.
careers 2.0
I searched and went through most of the questions here, none of which seemed to solve my situation.
I want to save a file to the internal storage by getting the text inputted from EditText. Then I want the same
file to return the inputted text in String form and save it to another String which is to be used later.
Here's the code:
package com.omm.easybalancerecharge;
import
import
import
import
import
import
import
import
import
import
import
import
android.app.Activity;
android.content.Context;
android.content.Intent;
android.net.Uri;
android.os.Bundle;
android.telephony.TelephonyManager;
android.view.Menu;
android.view.View;
android.view.View.OnClickListener;
android.widget.Button;
android.widget.EditText;
android.widget.TextView;
android
string
file-io
the question is "how to read/write to/from file?" Good.Dima Jan 17 '13 at 10:22
Did you consider using the app's preferences to store your strings ? fiddler Jan 17 '13 at 10:23
9/19/2014 6:57 AM
2 of 5
http://stackoverflow.com/questions/14376807/how-to-read-write-string-...
2 BTW, be sure you put permission to the mainfest file, to operate with storage... Good.Dima Jan 17 '13 at
10:24
This is my half complete app with many changes to implement. My idea is that the user enter the ID only once
at the first run of the app. Then the app will reference that stored ID as many times as the user runs the app.
Permissions are all added to the manifest. Major Aly Jan 17 '13 at 10:28
4 Answers
Hope this might be useful to you.
private void writeToFile(String data) {
try {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(openFileOutput("config.txt"
outputStreamWriter.write(data);
outputStreamWriter.close();
}
catch (IOException e) {
Log.e("Exception", "File write failed: " + e.toString());
}
}
9 If the class is not extended from Activity, usage of the "openFileInput()" method should be like this:
context.openFileInput() Behzad Oct 21 '13 at 10:17
9/19/2014 6:57 AM
3 of 5
http://stackoverflow.com/questions/14376807/how-to-read-write-string-...
For those looking for a general strategy for reading and writing a string to file:
First, get a file object
You'll need the storage path. For the internal storage, use:
String path = context.getFilesDir().getAbsolutePath();
For the external storage (SD card), use:
String path = context.getExternalFilesDir(null).getAbsolutePath();
Then create your file object:
File file = new File(path + "/my-file-name.txt");
Write a string to the file
FileOutputStream stream = new FileOutputStream(file);
try {
stream.write("text-to-write".getBytes());
} finally {
stream.close();
}
Or with Google Guava
Files.write("text-to-write", file, "UTF-8");
Read the file to a string
int length = (int) file.length();
byte[] bytes = new byte[length];
9/19/2014 6:57 AM
4 of 5
http://stackoverflow.com/questions/14376807/how-to-read-write-string-...
Ok for example I want a user to see all his posts, and when he goes to another screen and comes back, do I
need to draw it again or because it is cached it just pulls it out from cache and just shows it, if it just does pull it
out, how do I add an if conditional to say not to query my servers Lion789 Mar 8 at 5:55
9/19/2014 6:57 AM
5 of 5
http://stackoverflow.com/questions/14376807/how-to-read-write-string-...
Not the answer you're looking for? Browse other questions tagged java
android
9/19/2014 6:57 AM