私は Android プログラミングに慣れていないので、この質問がばかげているように聞こえる場合は、申し訳ありません。Android プログラムでファイルを読み書きしたい。私はJavaでそれを行う方法を知っています。
ドキュメントを読んで、次のコードを書きました。ただし、このファイルの場所を特定できません。ドキュメントによると、フォルダー data/data/package_name にあるはずです。
私はSamsung Galaxy S2を持っています。アプリケーションを起動してから閉じました。ファイルを探しに行ったところ、見つかりませんでした。マイファイル/データを調べました。ただし、このデータ フォルダー内にはデータ フォルダーはありません。私は何を間違っていますか?
お手伝いありがとう。
ソース: 1. http://developer.android.com/training/basics/data-storage/files.html 2. http://www.vogella.com/articles/AndroidFileBasedPersistence/article.html#overview
public class MainActivity extends Activity {
private void writeFileToInternalStorage() {
String eol = System.getProperty("line.separator");
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new OutputStreamWriter(openFileOutput("myfile.txt", MODE_WORLD_WRITEABLE)));
writer.write("This is a test1." + eol);
writer.write("This is a test2." + eol);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
writeFileToInternalStorage();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}