SD卡的数据读取与存储
程序员文章站
2022-07-15 10:02:46
...
1.添加对SD卡操作的权限
<!-- 读取SD的权限 -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<!-- 写入SD卡的权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
2.布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/edtext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20sp" />
<Button
android:id="@+id/write"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="write"
android:text="写入SD卡" />
<Button
android:id="@+id/read2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="read2"
android:text="读取SD卡的文件" />
<Button
android:id="@+id/read"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="read"
android:text="读取系统自带的文件" />
</LinearLayout>
3.Main_Activity.java主程序
package topteam.com.sdkarcunchu_demo;
import android.os.Environment;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
public class MainActivity extends AppCompatActivity {
EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar bar = getSupportActionBar();
if (bar != null) {
bar.hide();
}
editText = findViewById(R.id.edtext);
}
/**
* 往SD卡中写入数据
*
* @param view
*/
public void write(View view) {
//得到SD卡的状态
String state = Environment.getExternalStorageState();
//判断sd卡是否可用
if (state.equals(Environment.MEDIA_MOUNTED)) {
//得到路径
File dir = Environment.getExternalStorageDirectory();
//创建一个文件夹
File file = new File(dir, "text");
FileOutputStream fos = null;
BufferedWriter writer = null;
String data = editText.getText().toString();
try {
fos = new FileOutputStream(file);
writer = new BufferedWriter(new OutputStreamWriter(fos));
try {
writer.write(data);
writer.flush();
Toast.makeText(MainActivity.this, "写入完毕", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
if (writer != null) {
writer.close();
}
if (fos != null) {
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
} else {
Toast.makeText(MainActivity.this, "SD卡不可用", Toast.LENGTH_SHORT).show();
}
}
/**
* 读取程序自带的文件
*
* @param view
*/
public void read(View view) {
InputStream in = getResources().openRawResource(R.raw.text);
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(in, "UTF-8"));
StringBuilder sb = new StringBuilder();
String str = "";
try {
str = br.readLine();
while (str != null) {
sb.append(str);
str = br.readLine();
}
editText.setText(sb.toString());
} catch (IOException e) {
e.printStackTrace();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} finally {
try {
if (br != null) {
br.close();
}
if (in != null) {
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 从sd卡中去读数据
*
* @param view
*/
public void read2(View view) {
//得到SD卡的状态
String state = Environment.getExternalStorageState();
//判断是否可用
if (state.equals(Environment.MEDIA_MOUNTED)) {
File dir = Environment.getExternalStorageDirectory();
File file = new File(dir, "text");
FileInputStream is = null;
BufferedReader br = null;
StringBuilder sb = new StringBuilder();
String data = "";
try {
is = new FileInputStream(file);
br = new BufferedReader(new InputStreamReader(is));
try {
data = br.readLine();
while (data != null) {
sb.append(data);
data = br.readLine();
}
editText.setText(sb.toString());
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
if (br != null) {
br.close();
}
if(is!=null){
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
} else {
Toast.makeText(MainActivity.this, "SD卡不可用", Toast.LENGTH_SHORT).show();
}
}
}