首页 > 分享 > Android 存储学习之在外部存储中读写文件

Android 存储学习之在外部存储中读写文件

上节学习了如何在手机内部存储中读写文件,本节学习如何在手机的外部存储中读写文件。那就是如何在Sdcard中读写文件。

那我们还是用以前登录界面的例子举例说明,(登录界面请看上节Android 存储学习之在内部存储中读写文件)

先我们显示写的代码:

当点击确定并且自动登录的钩是选中的,则就会在sdcard文件夹写创建一个info.txt文件

public void login(View v)

{

String name = ed_nam.getText().toString();

String passwd = ed_passwd.getText().toString();

if((name.equals("")) || (passwd.equals("")))

{

Toast.makeText(this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();

}

else

{

if(cb.isChecked())

{

File file = new File("sdcard/info.txt");

try {

FileOutputStream fos = new FileOutputStream(file);

fos.write((name + "##" + passwd).getBytes());

fos.close();

} catch (Exception e) {

e.printStackTrace();

}

}

Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();

}

}

}

执行后,显示效果如下:


同理读也就是将上节读取的路径改为sdcard的路径即可:

public void readInfo()

{

File file = new File("sdcard/info.txt");

if(file.exists())

{

try {

FileInputStream fin = new FileInputStream(file);

BufferedReader buffer = new BufferedReader(new InputStreamReader(fin));

String text = buffer.readLine();

String s[] = text.split("##");

ed_nam.setText(s[0]);

ed_passwd.setText(s[1]);

} catch (Exception e) {

e.printStackTrace();

}

}

}

当然了,同理可知到。我们上面那些写Sdcard的路径是不对的,程序的健壮性不是很高,那就的用Google提供的访问sdcard的API:getExternalStorageDirectory

File file = new File(Environment.getExternalStorageDirectory(), "info.txt");

设想一种情况,当我们往sdcard中写一个文件,但是由于sdcard容量有限,不足与放下此文件,所以在用户打算放入一个文件到sdcard中时,需要先判断存储空间是不满足,不满足请用户给予提醒即可。

那如何获得sdcard的当前可用容量:

public Boolean sdcardAvailable(int size)

{

File file = Environment.getExternalStorageDirectory();

StatFs stat = new StatFs(file.getPath());

long blockSize = stat.getBlockSize();

long totalBlocks = stat.getBlockCount();

long availableBlocks = stat.getAvailableBlocks();

if(availableBlocks > size)

{

return true;

}

return false;

}

当写入一个文件时,先和sdcard的可用空间比较即可:

try {

FileOutputStream fos = new FileOutputStream(file);

int len = (name + "##" + passwd).getBytes().length/8;

if(sdcardAvailable(len))

{

fos.write((name + "##" + passwd).getBytes());

}

else {

Toast.makeText(this, "sdcard存储空间不足", Toast.LENGTH_SHORT).show();

}

fos.close();

} catch (Exception e) {

e.printStackTrace();

}

这样就能避免文件过大,写到一半出现容量不足的情况。

再设想一种情况,当我们突然往sdcard中写入内容时,如果sdcard出现某些问题,没有挂载,那也是不能写入东西的。所以,读写之前需要判断sdcard是否正常运行

if(cb.isChecked())

{

if(Environment.getExternalStorageDirectory().equals(Environment.MEDIA_MOUNTED))

{

File file = new File(Environment.getExternalStorageDirectory(), "info2.txt");

try {

FileOutputStream fos = new FileOutputStream(file);

int len = (name + "##" + passwd).getBytes().length/8;

if(sdcardAvailable(len))

{

fos.write((name + "##" + passwd).getBytes());

}

else {

Toast.makeText(this, "sdcard存储空间不足", Toast.LENGTH_SHORT).show();

}

fos.close();

} catch (Exception e) {

e.printStackTrace();

}

}

}

Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();

}

也就是在读写时,先判断sdcard是否已经正在运行,如果sdcard的状态是ok的。则执行接下来的操作即可

相关知识

【移动应用开发技术】Android中怎么保存数据.docx
随意花
花佳鲜花
花伍供应商平台
Kafka数据安全性、运行原理、存储
花涧悦
花草植物助手
花礼网鲜花
计算机病毒除通过读写或复制移动存储器上带病毒的文件传染外,另一条主要的传染途径是A.
Python 文件操作中的读写模式:open(path, ‘

网址: Android 存储学习之在外部存储中读写文件 https://m.huajiangbk.com/newsview1095105.html

所属分类:花卉
上一篇: DB2,创建存储过程报错,SQL
下一篇: 假设以数组Q[m]存放循环队列中