【Android开发学习06】Android中的文件I/O操作

本节分两部分:

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() {
        								//为打开按钮添加监听器
			@Override
			public void onClick(View v) {
				String contentResult=loadContentFromSDCard("歌词.txt");
										//调用读取文件方法,获得文件内容
				EditText etContent=(EditText)findViewById(R.id.EditText01);	
										//实例化EditText
				etContent.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() {//打开按钮添加监听器
			@Override
			public 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.运行效果:






  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值