0
0

More than 3 years have passed since last update.

51歳からのプログラミング ファイルに書き読み android 備忘

Posted at
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をつかったっけ。

R020125-1.JPG

R020125-2.JPG

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0