Write to internal storage - File is empty
I'm trying to write text files to internal storage in many ways. I don't know why the result text files are empty.
package com.testandroid;
public class TestAndroidActivity extends Activity {
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
writeToInternalStorage(this);
}
private void writeToInternalStorage(final Context context) {
try开发者_StackOverflow社区 {
// Way1
final PrintWriter writer = new PrintWriter(new File("/data/data/com.testandroid/test.txt"));
writer.println("Something");
// Way2
final PrintWriter writer2 = new PrintWriter(new FileOutputStream("/data/data/com.testandroid/test2.txt"));
writer2.write("Something more");
// Way3
final FileOutputStream fos = openFileOutput("test3.txt", Context.MODE_PRIVATE);
final PrintWriter writer3 = new PrintWriter(fos);
writer3.write("Something of something");
try {
fos.close();
} catch (final IOException e) {
e.printStackTrace();
}
// Way4
try {
final BufferedWriter writer4 = new BufferedWriter(new FileWriter(new File("/data/data/com.testandroid/test4.txt")));
writer4.write("Something please");
} catch (final IOException e) {
e.printStackTrace();
}
} catch (final FileNotFoundException e1) {
e1.printStackTrace();
}
}
}
try this out
FileWriter outFile = new FileWriter("/data/data/com.testandroid/test.txt");
PrintWriter out = new PrintWriter(outFile);
// Printwriter out = new PrintWriter(new FileWriter("/data/data/com.testandroid/test.txt"));
out.println("This is line 1");
out.close();
精彩评论