1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED)
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PackageManager.PERMISSION_GRANTED);
}
public void save(View v){
Boolean b= Environment.isExternalStorageRemovable();
File f = Environment.getExternalStorageDirectory();
// to this path add a new directory path
File dir = new File(f.getAbsolutePath() + "/Queneau");
// create this directory if not already created
dir.mkdir();
// create the file in which we will write the contents
File file = new File(dir, "sonnet1.txt");
String data = "Vive les vacances";
try {
OutputStreamWriter os = new OutputStreamWriter(getApplicationContext().openFileOutput("sonnet1.txt", Context.MODE_PRIVATE));
os.write(data);
os.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} |
Partager