SD卡读写之FileNotFoundException: /storage/emulated/0object.txt: open failed: ENOENT (No such file or dir
读写sd卡中的文件依照例如以下步骤:1调用Environment的getExternalStorageState()方法推断手机上是否插入了sd卡。而且应用程序具有读写SD卡的能力
//假设手机已经插入了SD卡,且具有读写sd卡的能力。以下的语句将会返回true
Environment.getExternalStorageState().equals(Envronment.MEDIA_MOUNTED)
2)调用environment的getExternalStorageDIrectory()方法获取外部存储器,也就是SD卡的文件夹
3)使用FileInputStream、FileOUtputStream FileReader或者FileWriter来读写sd卡中的文件
注冊权限
<--!在SD卡中创建与删除文件权限-->
<--!向SD卡写入数据的权限--->
<uses-permission android:name="androd.permission.WRITE_EXTERNAL_STORAGE"/>
<--!读取数据的权限--->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
实例代码:
有两个EditText 和两个Button组件,text_Write用于写入数据,Button(write)组件用于读取text_Write中的数据并写入/storage/emulated/0/object.txt 文件里。
Button(read)组件用于从SD卡的/storage/emulated/0/object.txt中读取数据,并显示在text_Read组件中。
<span style="font-size:18px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.sdcard03.MainActivity$PlaceholderFragment" > <EditText android:id="@+id/text_Write"
android:layout_width="match_parent"
android:layout_height="45dp"
/>
<EditText
android:id="@+id/text_Read"
android:layout_width="match_parent"
android:layout_height="45dp"
/>
<Button
android:id="@+id/read"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="read"/>
<Button
android:id="@+id/write"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="write"/> </LinearLayout>
</span>
主程序:
<span style="font-size:18px;">package com.example.sdcard03; import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader; import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast; public class MainActivity extends Activity {
private EditText read_Text;
private EditText write_Text;
private Button read;
private Button write;
private String fileName = <span style="color:#FF6666;">"/object.txt";</span> @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
read = (Button) findViewById(R.id.read);
write = (Button) findViewById(R.id.write);
read_Text = (EditText) findViewById(R.id.text_Read);
write_Text = (EditText) findViewById(R.id.text_Write);
read.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
String s = readFromSdcard();
read_Text.setText(s); }
});
// ------------------------
write.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
String s2 = write_Text.getText().toString();
writeToSdcard(s2); } }); } public String readFromSdcard() { if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) { try {
File sdPath = Environment<span style="color:#FF0000;">.getExternalStorageDirectory()</span>;
<span style="color:#6633FF;">System.out.println(sdPath.toString());</span> FileInputStream fis = new FileInputStream(sdPath
.<span style="color:#FF0000;">getCanonicalFile()</span>.toString() + fileName);
<span style="color:#3366FF;">System.out.println(sdPath.getCanonicalFile().toString());</span>
BufferedReader br = new BufferedReader(new InputStreamReader(
fis));
StringBuilder sb = new StringBuilder("");
String line = null;
while ((line = br.readLine()) != null) {
sb.append(line); }
fis.close();
br.close();
return sb.toString();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} return null; } // -------------------------------------------------------
public void writeToSdcard(String s) {
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
File sdCardDir = Environment.getExternalStorageDirectory();
try {
File file = new File(sdCardDir.getCanonicalPath() + fileName);
FileOutputStream fos = new FileOutputStream(file); fos.write(s.getBytes());
fos.close(); } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } else {
Toast.makeText(MainActivity.this, "sd卡异常", Toast.LENGTH_LONG)
.show();
} }
}
</span>
几点注意:1 不要忘记注冊权限
2,
<span style="font-size:18px;">File sdPath = Environment<span style="color:#FF0000;">.getExternalStorageDirectory()</span>;中getExternalStorageDirectory()方法返回的是什么?
</span><pre name="code" class="java"><span style="font-size:18px;">sdPath.<span style="color:#FF0000;">getCanonicalFile()</span>.toString()方法返回的是什么?
通过上面两行蓝色输出语句能够在logcat中看到返回的都是/storage/emulated/0 注意在0后面没有“/”反斜杠,所以我们在最初声明字符串常量filName时</span><pre name="code" class="java"><span style="font-size:18px;">private String fileName = <span style="color:#FF6666;">"/object.txt";object前有反斜杠,也就是代表了路径</span></span><pre name="code" class="java"><pre name="code" class="java">/storage/emulated/0<span style="color:#FF6666;">/object.txt</span> 。假设我们最初在声明fileName时不加反斜杠就变成了/storage/emulated/0<span style="color:#FF6666;">object.txt</span>。这个路径是不合法的,不存在程序会报错 03-15 15:07:39.440: W/System.err(26730): java.io.FileNotFoundException: /storage/emulated/0object.txt: open failed: ENOENT (No such file or directory) <pre name="code" class="java"><pre name="code" class="java"><pre name="code" class="java">
SD卡读写之FileNotFoundException: /storage/emulated/0object.txt: open failed: ENOENT (No such file or dir的更多相关文章
- 【STM32】使用SDIO进行SD卡读写,包含文件管理FatFs(六)-FatFs使用的思路介绍
[STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(一)-初步认识SD卡 [STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(二)-了解SD总线,命令的相关介绍 [STM3 ...
- 第36章 SDIO—SD卡读写测试
第36章 SDIO—SD卡读写测试 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/f ...
- 第36章 SDIO—SD卡读写测试—零死角玩转STM32-F429系列
第36章 SDIO—SD卡读写测试 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/f ...
- 【STM32】使用SDIO进行SD卡读写,包含文件管理FatFs(终)-配合内存管理来遍历SD卡
[STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(一)-初步认识SD卡 [STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(二)-了解SD总线,命令的相关介绍 [STM3 ...
- 【STM32】使用SDIO进行SD卡读写,包含文件管理FatFs(八)-认识内存管理
[STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(一)-初步认识SD卡 [STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(二)-了解SD总线,命令的相关介绍 [STM3 ...
- 【STM32】使用SDIO进行SD卡读写,包含文件管理FatFs(七)-准备移植FatFs
[STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(一)-初步认识SD卡 [STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(二)-了解SD总线,命令的相关介绍 [STM3 ...
- 【STM32】使用SDIO进行SD卡读写,包含文件管理FatFs(五)-文件管理初步介绍
其他链接 [STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(一)-初步认识SD卡 [STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(二)-了解SD总线,命令的相关介绍 ...
- 【STM32】使用SDIO进行SD卡读写,包含文件管理FatFs(一)-初步认识SD卡
由于一张SD卡要能读写,涉及到的技术有些多,我打算分以下几篇博客 [STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(一)-初步认识SD卡 [STM32]使用SDIO进行SD卡读写,包含 ...
- 【STM32】使用SDIO进行SD卡读写,包含文件管理FatFs(二)-了解SD总线,命令的相关介绍
其他链接 [STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(一)-初步认识SD卡 [STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(二)-了解SD总线,命令的相关介绍 ...
随机推荐
- mcu读写调式
拿仿真SPIS为例: 对于其他外设(UART.SPIM.I2S.I2C...)都是一个道理. 当MCU写时:主要对一个寄存器进行写,此寄存器是外设的入口(基本都会做并转串逻辑). spis_tx_da ...
- Appium+python自动化-环境搭建
一.前言 本文是在windows10 X64系统下搭建appium,流程如下: 1.安装jdk1.8+python3.6 (64位) 2.安装node.js 3.安装Appium-desktop 4 ...
- hdu2024
这题目感觉不是很严谨,如果是关键字的话也是不能作为合法标识符的,但是这个不用检测,就算要检测也会很费劲,还得用字符串匹配,而且还得知道一共都有哪些关键字,太麻烦了,所以出题人原意就是检查大小写字母数字 ...
- BASH重定向问题
APUE 3.5关于重定向有个容易迷惑人的问题: ./a.out > outfile 2>&1 ./a.out 2>&1 > outfile 问两者区别? in ...
- mac安装R语言启动后-Warning messages
During startup - Warning messages: : Setting LC_CTYPE failed, using "C" : Setting LC_COLLA ...
- uva 11916 解模方程a^x=b (mod n)
Emoogle Grid You have to color an M x N ( 1M, N108) two dimensional grid. You will be provided K ...
- ElasticSearch集群状态查看命令大全
Elasticsearch中信息很多,同时ES也有很多信息查看命令,可以帮助开发者快速查询Elasticsearch的相关信息. _cat $ curl localhost:9200/_cat =^. ...
- HDU5056 BoringCount--线性扫一遍
11754936 2014-09-29 10:08:45 Accepted 5056 31MS 392K 1257 B G++ czy 好简单的思路,怎么就没想到呢..... Boring count ...
- Ansible进阶
YAML YAML简介 YAML是一个可读性高,并用来表达资料序列的格式.YAML参考了其它多种语言,包括:XML.C语言.Python.Perl以及电子邮件格式RFC2822等 它是一种直观的能够被 ...
- ci框架——数据库(增删改查)
1:配置数据库(application/config/database.php)修改 $db['default'] = array( 'dsn' => '', 'hostname' => ...