18 Jun

Read Write SD Card on Android

pertama yang harus dilakukan adalah membuka AndroidManifest.xml dan tambahkan Permission berikut:

android.permission.WRITE_EXTERNAL_STORAGE

Cara Menulis Data ke File SD Card

File file = new File("/sdcard/mysdfile.txt");
file.createNewFile();
FileOutputStream fos = 
		new FileOutputStream(file); 

OutputStreamWriter osw = 
		new OutputStreamWriter(fos);
osw.append(txtData.getText());
osw.close();
fos.close();

Cara Membaca Data dari File SD Card

File file = new File("/sdcard/mysdfile.txt");
FileInputStream fis = 
	new FileInputStream(file);
BufferedReader myReader = 
	new BufferedReader(new InputStreamReader(fis)); 
String aDataRow = ""; 
String aBuffer = ""; 
while ((aDataRow = myReader.readLine()) != null) {
	aBuffer += aDataRow + "\n"; 
} 
txtData.setText(aBuffer); 
myReader.close();

Leave a Reply

Your email address will not be published. Required fields are marked *