手機外部文件存儲(SD卡存儲)

 

 

 

 

package com.atguigu.l04_datastorage;java

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;android

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;app

/**
* 測試手機外部文件存儲
*
* @author 侯志強
*
*/
public class OFActivity extends Activity {ide

private EditText et_of_name;
private EditText et_of_content;

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

et_of_name = (EditText) findViewById(R.id.et_of_name);
et_of_content = (EditText) findViewById(R.id.et_of_content);
}測試

public void save(View v) throws IOException {
//1. 判斷sd卡狀態, 若是是掛載的狀態才繼續, 不然提示
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
//2. 讀取輸入的文件名/內容
String fileName = et_of_name.getText().toString();
String content = et_of_content.getText().toString();
//3. 獲得指定文件的OutputStream
//1).獲得sd卡下的files路徑
String filesPath = getExternalFilesDir(null).getAbsolutePath();
//2).組成完整路徑
String filePath = filesPath+"/"+fileName;
//3). 建立FileOutputStream
FileOutputStream fos = new FileOutputStream(filePath);
//4. 寫數據
fos.write(content.getBytes("utf-8"));
fos.close();
//5. 提示
Toast.makeText(this, "保存完成", 0).show();
} else {
Toast.makeText(this, "sd卡沒有掛載", 0).show();
}

}ui

public void read(View v) throws Exception {

// 1. 判斷sd卡狀態, 若是是掛載的狀態才繼續, 不然提示
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
// 2. 讀取輸入的文件名
String fileName = et_of_name.getText().toString();
// 3. 獲得指定文件的InputStream
// 1).獲得sd卡下的files路徑
String filesPath = getExternalFilesDir(null).getAbsolutePath();
// 2).組成完整路徑
String filePath = filesPath + "/" + fileName;
// 3). 建立FileInputStream
FileInputStream fis = new FileInputStream(filePath);
// 4. 讀取數據, 成String
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = -1;
while((len=fis.read(buffer))!=-1) {
baos.write(buffer, 0, len);
}
String content = baos.toString();

// 5. 顯示
et_of_content.setText(content);
} else {
Toast.makeText(this, "sd卡沒有掛載", 0).show();
}
}this

// /storage/sdcard/atguigu/xxx.txt
public void save2(View v) throws IOException {
//1. 判斷sd卡狀態, 若是是掛載的狀態才繼續, 不然提示
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
//2. 讀取輸入的文件名/內容
String fileName = et_of_name.getText().toString();
String content = et_of_content.getText().toString();
//3. 獲得指定文件的OutputStream
//1). /storage/sdcard/
String sdPath = Environment.getExternalStorageDirectory().getAbsolutePath();
//2). /storage/sdcard/atguigu/(建立文件夾)
File file = new File(sdPath+"/atguigu");
if(!file.exists()) {
file.mkdirs();//建立文件夾
}
//3). /storage/sdcard/atguigu/xxx.txt
String filePath = sdPath+"/atguigu/"+fileName;
//4). 建立輸出流
FileOutputStream fos = new FileOutputStream(filePath);
//4. 寫數據
fos.write(content.getBytes("utf-8"));
fos.close();
//5. 提示
Toast.makeText(this, "保存完成", 0).show();
} else {
Toast.makeText(this, "sd卡沒有掛載", 0).show();
}
}blog

public void read2(View v) throws Exception {
// 1. 判斷sd卡狀態, 若是是掛載的狀態才繼續, 不然提示
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
// 2. 讀取輸入的文件名
String fileName = et_of_name.getText().toString();
// 3. 獲得指定文件的InputStream
String sdPath = Environment.getExternalStorageDirectory().getAbsolutePath();
String filePath = sdPath+"/atguigu/"+fileName;
FileInputStream fis = new FileInputStream(filePath);
// 4. 讀取數據, 成String
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = -1;
while((len=fis.read(buffer))!=-1) {
baos.write(buffer, 0, len);
}
String content = baos.toString();
fis.close();
// 5. 顯示
et_of_content.setText(content);
} else {
Toast.makeText(this, "sd卡沒有掛載", 0).show();
}
}
}utf-8

相關文章
相關標籤/搜索