20170203

package com.example.wata.myapplication;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// ファイルを保存
FileOutputStream fileOutputstream = null;
try {
fileOutputstream = openFileOutput("sample.txt", Context.MODE_PRIVATE);
fileOutputstream.write("123456789".getBytes());
} catch (IOException e) {
e.printStackTrace();
}

// ファイルを読み出し
FileInputStream fileInputStream;
String text = null;
try {
fileInputStream = openFileInput("sample.txt");
String lineBuffer = null;
BufferedReader reader= new BufferedReader(new InputStreamReader(fileInputStream,"UTF-8"));
while( (lineBuffer = reader.readLine()) != null ) {
text = lineBuffer ;
}
} catch (IOException e) {
e.printStackTrace();
}
Toast.makeText(getApplicationContext(),text,Toast.LENGTH_LONG).show();
}

}