Android SDcard 文件读写,RandomAccessFile操作
程序员文章站
2022-05-16 21:00:53
...
SD Card 相关的权限:
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="shortcut.song.com.myapplication.SDCardTestActivity">
<Button
android:id="@+id/btn_read_sdcard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="read"/>
<Button
android:id="@+id/btn_write_sdcard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="write"/>
<EditText
android:id="@+id/edit_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="read"/>
<EditText
android:id="@+id/edit_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="write"/>
</LinearLayout>
package shortcut.song.com.myapplication;
import android.os.Environment;
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.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
public class SDCardTestActivity extends AppCompatActivity {
private String FILE_NAME = "/sdandroid.bin";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sdcard_test);
final Button read = (Button)findViewById(R.id.btn_read_sdcard);
final Button write = (Button)findViewById(R.id.btn_write_sdcard);
final EditText edit1 = (EditText)findViewById(R.id.edit_1);
final EditText edit2 = (EditText)findViewById(R.id.edit_2);
read.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//将edit1输入的内容写到文件中
edit2.setText(read());
}
});
write.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
write(edit1.getText().toString());
edit1.setText("");
}
});
}
private String read() {
try {
//如果设置插入了SD卡,并且应用程序具有访问SD卡的权限
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
//获取SD卡对应的存储目录
File sdCardDir = Environment.getExternalStorageDirectory();
//获取指定文件对应的输入流
FileInputStream fis = new FileInputStream(sdCardDir.getCanonicalPath() + FILE_NAME);
//将指定的输入流包装成BufferedReader
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
StringBuilder sb = new StringBuilder("");
String line = null;
//循环读取文件内容
while ( (line = br.readLine()) != null)
{
sb.append(line);
}
//关闭资源
br.close();
return sb.toString();
}else{
Toast.makeText(SDCardTestActivity.this, "Not Find SD card.", Toast.LENGTH_LONG).show();
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private void write(String content) {
try {
//如果设置插入了SD卡,并且应用程序具有访问SD卡的权限
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
//获取SD卡对应的存储目录
File sdCardDir = Environment.getExternalStorageDirectory();
File targetFile = new File(sdCardDir.getCanonicalPath() + FILE_NAME);
//以指定文件创建RandomAccessFile 对象
RandomAccessFile raf = new RandomAccessFile(targetFile, "rw");
//将文件记录移动到文件最后
raf.seek(targetFile.length());
//输出文件内容
raf.write(content.getBytes());
//关闭RandomAccessFile
raf.close();
}else{
Toast.makeText(SDCardTestActivity.this, "Not Find SD card.", Toast.LENGTH_LONG).show();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
运行效果:
上一篇: 六、把文件存放在SDCard