public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv = findViewById(R.id.tv);
String text = "";
try {
FileOutputStream output = openFileOutput("userInfo.txt", Context.MODE_PRIVATE);
output.write("hello".getBytes());
output.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
String file = "userInfo.txt";
FileInputStream fileInputStream = openFileInput(file);
BufferedReader reader = new BufferedReader(new InputStreamReader(fileInputStream,StandardCharsets.UTF_8));
StringBuffer buffer = new StringBuffer();
String lineBuffer;
while( (lineBuffer = reader.readLine()) != null ) {
text = lineBuffer;;
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
tv.setText(text);
}
}
Appの内部ストレージに書き込みするとき、ファイルがなければ、ファイルを作成して書き込む。
通常は内部ストレージのファイルは見えないけれど、Android Studio のデバイス・ファイル・エクスプローラー(DFE)で中身を覗けるんだった。sqliteを使うときもDFEをつかったっけ。