错误的解决方法请参考:http://liangruijun.blog.51cto.com/3061169/673776


3.3.2 访问手机中的存储文件夹


3.3.3 读取assets中的文件

package com.example.sample3_5;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream; import android.os.Bundle;
import android.app.Activity;
import android.content.res.AssetManager;
import android.content.res.Resources;
//import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast; public class MyActivity extends Activity {
private Button but; //打开按钮
private EditText etContent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
but = (Button) findViewById(R.id.Button01);
but.setOnClickListener(new View.OnClickListener() { //private EditText etContent;
//private String contentResult; @Override
public void onClick(View v) {
etContent = (EditText) findViewById(R.id.EditText01);
loadFromAssert("AndroidSummary.txt");
}
});
}
/*
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}*/
//public String loadFromAssert(String fileName){
public void loadFromAssert(String fileName){
//String content = null; //结果字符串
//final String content = null; //结果字符串
try { //InputStream is = this.getResources().getAssets().open(fileName);
Resources resources = this.getResources();
AssetManager assets = resources.getAssets();
InputStream is = assets.open(fileName);
int ch = 0;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buff = null;
while((ch=is.read())!=-1){
baos.write(ch);
}
buff = baos.toByteArray();
baos.close();
is.close();
//content = new String(buff,"UTF-8");
final String content = new String(buff,"UTF-8"); //结果字符串
runOnUiThread(new Runnable(){ @Override
public void run() {
// TODO Auto-generated method stub
etContent.setText(content);
} });
} catch (IOException e) {
// TODO Auto-generated catch block
Toast.makeText(this, "对不起,没有找到指定文件!!!", Toast.LENGTH_SHORT).show();
e.printStackTrace(); } //return content; } }
package com.example.sample3_5;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream; import android.os.Bundle;
import android.app.Activity;
import android.content.res.AssetManager;
import android.content.res.Resources;
//import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast; public class MyActivity extends Activity {
private Button but; //打开按钮
@Override
protected 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) {
EditText etContent = (EditText) findViewById(R.id.EditText01);
String contentResult = loadFromAssert("AndroidSummary.txt");
etContent.setText(contentResult);
}
});
}
/*
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}*/
public String loadFromAssert(String fileName){
String content = null; //结果字符串
try { InputStream is = this.getResources().getAssets().open(fileName);
int ch = 0;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buff = null;
while((ch=is.read())!=-1){
baos.write(ch);
}
buff = baos.toByteArray();
baos.close();
is.close();
content = new String(buff,"UTF-8");
} catch (IOException e) {
// TODO Auto-generated catch block
Toast.makeText(this, "对不起,没有找到指定文件!!!", Toast.LENGTH_SHORT).show();
e.printStackTrace(); }
return content;
} }
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sample3_5"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" /> <application
android:allowBackup="true"
android:icon="@drawable/icon"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.sample3_5.MyActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application> </manifest>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<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_height="wrap_content"
android:layout_width="fill_parent">
<EditText
android:editable="false"
android:id="@+id/EditText01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
</EditText> <!-- 添加EditText -->
</ScrollView> <!-- 添加ScrollView --> </LinearLayout>

3.3 文件I/O的更多相关文章

  1. Mapreduce的文件和hbase共同输入

    Mapreduce的文件和hbase共同输入 package duogemap;   import java.io.IOException;   import org.apache.hadoop.co ...

  2. mapreduce多文件输出的两方法

    mapreduce多文件输出的两方法   package duogemap;   import java.io.IOException;   import org.apache.hadoop.conf ...

  3. 01.SQLServer性能优化之----强大的文件组----分盘存储

    汇总篇:http://www.cnblogs.com/dunitian/p/4822808.html#tsql 文章内容皆自己的理解,如有不足之处欢迎指正~谢谢 前天有学弟问逆天:“逆天,有没有一种方 ...

  4. SQL Server 大数据搬迁之文件组备份还原实战

    一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 解决方案(Solution) 搬迁步骤(Procedure) 搬迁脚本(SQL Codes) ...

  5. SQLSERVER将一个文件组的数据移动到另一个文件组

    SQLSERVER将一个文件组的数据移动到另一个文件组 有经验的大侠可以直接忽视这篇文章~ 这个问题有经验的人都知道怎麽做,因为我们公司的数据量不大没有这个需求,也不知道怎麽做实验 今天求助了QQ群里 ...

  6. SQL Server中的高可用性(2)----文件与文件组

        在谈到SQL Server的高可用性之前,我们首先要谈一谈单实例的高可用性.在单实例的高可用性中,不可忽略的就是文件和文件组的高可用性.SQL Server允许在某些文件损坏或离线的情况下,允 ...

  7. C# ini文件操作【源码下载】

    介绍C#如何对ini文件进行读写操作,C#可以通过调用[kernel32.dll]文件中的 WritePrivateProfileString()和GetPrivateProfileString()函 ...

  8. 【小程序分享篇 一 】开发了个JAVA小程序, 用于清除内存卡或者U盘里的垃圾文件非常有用

    有一种场景, 手机内存卡空间被用光了,但又不知道哪个文件占用了太大,一个个文件夹去找又太麻烦,所以我开发了个小程序把手机所有文件(包括路径下所有层次子文件夹下的文件)进行一个排序,这样你就可以找出哪个 ...

  9. 【原】Android热更新开源项目Tinker源码解析系列之二:资源文件热更新

    上一篇文章介绍了Dex文件的热更新流程,本文将会分析Tinker中对资源文件的热更新流程. 同Dex,资源文件的热更新同样包括三个部分:资源补丁生成,资源补丁合成及资源补丁加载. 本系列将从以下三个方 ...

  10. GreenDao 数据库:使用Raw文件夹下的数据库文件以及数据库升级

    一.使用Raw文件夹下的数据库文件 在使用GreenDao框架时,数据库和数据表都是根据生成的框架代码来自动创建的,从生成的DaoMaster中的OpenHelper类可以看出: public sta ...

随机推荐

  1. Lazarus Coolbar and AnchroDocking

    在lazarus1.6里加载了AnchroDocking后,Coolbar突然不见了,找了好久没找到,原来在这里! 在AnchroDocking中可能是为了界面的最大化,默认是开始Toolbar 而关 ...

  2. 【解题报告】 洛谷 P3492 [POI2009]TAB-Arrays

    [解题报告] 洛谷 P3492 [POI2009]TAB-Arrays 这题是我随机跳题的时候跳到的.写完这道题之后,顺便看了一下题解,发现只有一篇题解,所以就在这里顺便写一个解题报告了. 首先当然是 ...

  3. 如何在docker和宿主机之间复制文件

    如何在docker和宿主机之间复制文件   最近在用Docker布署hadoop,要将文件上传到HDFS首先文件得在Docker容器中吧,网上提供的方法差不多有三种 1.用-v挂载主机数据卷到容器内  ...

  4. jdk8--十大新特性

    https://www.cnblogs.com/dennyzhangdd/p/6722445.html 一.十大特性 1.Lambda表达式 2.Stream函数式操作流元素集合 3.接口新增:默认方 ...

  5. bupt summer training for 16 #1 ——简单题目

    D.What a Mess 给n个数,求其中能满足 a[i] % a[j] == 0 的数对之和 n = 1W,max_ai = 100W 不是很大,所以就直接筛就可以了 计算可得最高复杂度 < ...

  6. Java基础学习总结(42)——Log4j 2使用教程

    1. 去官方下载log4j 2,导入jar包,基本上你只需要导入下面两个jar包就可以了(xx是乱七八糟的版本号): log4j-core-xx.jar log4j-api-xx.jar 2. 导入到 ...

  7. [poj3735] Training little cats_矩乘快速幂

    Training little cats poj-3735 题目大意:给你n个数,k个操作,将所有操作重复m次. 注释:三种操作,将第i个盒子+1,交换两个盒子中的个数,将一个盒子清空.$1\le m ...

  8. P1265 公路修建 洛谷

    https://www.luogu.org/problem/show?pid=1265 题目描述 某国有n个城市,它们互相之间没有公路相通,因此交通十分不便.为解决这一“行路难”的问题,政府决定修建公 ...

  9. ZooKeeper动态增加Server(动态增加节点)的研究(待实践)

    说明:是动态增加Server,不是动态增加连接到ZK Server的Client. 场景如下(转自外文): 1.在t=t_1->[peer-1(Leader),peer-2],peer-1是主节 ...

  10. N天学习一个linux命令之ss

    用途 输出socket统计,无任何参数时默认显示的是已建立socket连接的列表 用法 ss [options] [ FILTER ] 常用选项 -h, --help 显示帮助信息 -V, --ver ...