【Android开发学习06】Android中的文件I/O操作
16lz
2021-01-23
本节分两部分:
1.访问SD卡.
2.访问手机中的存储文件夹.
3.读取assets中的文件.
一.访问SD卡:
1.界面编辑(res\layout\main.xml):
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/Button01" android:layout_width="128dp" android:layout_height="wrap_content" android:text="打开" > </Button><!-- 添加一个Button --> <Button android:id="@+id/button1" android:layout_width="125dp" android:layout_height="wrap_content" android:text="测试按钮" /> <ScrollView android:id="@+id/ScrollView01" android:layout_width="fill_parent" android:layout_height="wrap_content"> <EditText android:editable="false" android:id="@+id/EditText01" android:layout_width="fill_parent" android:layout_height="wrap_content"> </EditText><!-- 添加一个EditText --> </ScrollView> <!-- 添加一个ScrollView --></LinearLayout>
2. 代码编辑( \src\wyf\zcl\MyActivity.java):
package wyf.zcl;import java.io.File;//引入相关包import java.io.FileInputStream;//引入相关包import android.app.Activity;//引入相关包import android.os.Bundle;//引入相关包import android.view.View;//引入相关包import android.widget.Button;//引入相关包import android.widget.EditText;//引入相关包import android.widget.Toast;//引入相关包public class MyActivity extends Activity { /** Called when the activity is first created. */Button but;//打开按钮引用 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); but=(Button)findViewById(R.id.Button01); //打开按钮初始化 but.setOnClickListener(new View.OnClickListener() { //为打开按钮添加监听器@Overridepublic void onClick(View v) {String contentResult=loadContentFromSDCard("歌词.txt");//调用读取文件方法,获得文件内容EditText etContent=(EditText)findViewById(R.id.EditText01);//实例化EditTextetContent.setText(contentResult);//设置EditText的内容}}); } public String loadContentFromSDCard(String fileName){ //从SD卡读取内容 String content=null;//sd卡 的内容字符串 try{ File f=new File("/sdcard/ebook/"+fileName);//待读取的文件 int length=(int)f.length(); byte[] buff=new byte[length]; FileInputStream fis=new FileInputStream(f); fis.read(buff);// 从此输入流中将 byte.length 个字节的数据读入一个 byte 数组中 fis.close();//关闭此输入流并释放与此流关联的所有系统资源 content=new String(buff,"UTF-8"); }catch(Exception e){ Toast.makeText(this, "对不起,没有找到文件", Toast.LENGTH_SHORT).show(); }return content; }}
运行效果如下:
二.访问手机中的存储文件夹:
访问手机中的文件夹和访问SD卡一样,只需要指明具体位置即可,只是权限这一块需要提升。
三.读取assets中的文件:
1.在项目工程的"assets"目录下,新建一个UTF8编码的文本文件"test.txt"作为测试的对象。
2.界面编辑(res\layout\main.xml):
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:text="打开" android:id="@+id/Button01" android:layout_width="fill_parent" android:layout_height="wrap_content"> </Button><!-- 添加Button按钮 --> <ScrollView android:id="@+id/ScrollView01" android:layout_width="fill_parent" android:layout_height="wrap_content"> <EditText android:editable="false" android:id="@+id/EditText01" android:layout_width="fill_parent" android:layout_height="wrap_content"> </EditText><!-- 添加EditText --> </ScrollView> <!-- 添加ScrollView --></LinearLayout>
3. 代码编辑(\src\wyf\zcl\MyActivity.java):
package wyf.zcl;import java.io.ByteArrayOutputStream;//引入相关包import java.io.InputStream;//引入相关包import android.app.Activity;//引入相关包import android.os.Bundle;//引入相关包import android.view.View;//引入相关包import android.widget.Button;//引入相关包import android.widget.EditText;//引入相关包import android.widget.Toast;//引入相关包public class MyActivity extends Activity { /** Called when the activity is first created. */private Button but;//打开按钮 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); but=(Button)findViewById(R.id.Button01);//打开按钮实例化 but.setOnClickListener(new View.OnClickListener() {//打开按钮添加监听器@Overridepublic void onClick(View v) {String contentResult=loadFromAssert("test.txt");EditText etContent=(EditText)findViewById(R.id.EditText01);etContent.setText(contentResult);}}); } public String loadFromAssert(String fileName){ String content=null;//结果字符串 try{ InputStream is=this.getResources().getAssets().open(fileName);//打开文件 int ch=0; ByteArrayOutputStream baos = new ByteArrayOutputStream();//实现了一个输出流 while((ch=is.read())!=-1){ baos.write(ch);// 将指定的字节写入此 byte 数组输出流。 } byte[] buff=baos.toByteArray();//以 byte 数组的形式返回此输出流的当前内容 baos.close();//关闭流 is.close();//关闭流 content=new String(buff,"UTF-8"); //设置字符串编码 }catch(Exception e){ Toast.makeText(this, "对不起,没有找到指定文件!", Toast.LENGTH_SHORT).show(); } return content; }}
4.运行效果:
更多相关文章
- 关于xml文件中的一些常用标签的意义
- Android中资源文件夹res/raw和assets的使用
- [Android Studio / NDK] 如何使用javah生成.h文件
- Android 打开本地文件兼容7.0
- Android媒体应用(六)--响应媒体按钮
- [Google Android] 理解NDK(1)-- 编译Android .so文件
- Android开发8——利用pull解析器读写XML文件
- Android 获取未读短信同时,一并获取该短信内容的方法
- Android编程之文件操作