3.3 文件I/O


错误的解决方法请参考: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的更多相关文章
- Mapreduce的文件和hbase共同输入
Mapreduce的文件和hbase共同输入 package duogemap; import java.io.IOException; import org.apache.hadoop.co ...
- mapreduce多文件输出的两方法
mapreduce多文件输出的两方法 package duogemap; import java.io.IOException; import org.apache.hadoop.conf ...
- 01.SQLServer性能优化之----强大的文件组----分盘存储
汇总篇:http://www.cnblogs.com/dunitian/p/4822808.html#tsql 文章内容皆自己的理解,如有不足之处欢迎指正~谢谢 前天有学弟问逆天:“逆天,有没有一种方 ...
- SQL Server 大数据搬迁之文件组备份还原实战
一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 解决方案(Solution) 搬迁步骤(Procedure) 搬迁脚本(SQL Codes) ...
- SQLSERVER将一个文件组的数据移动到另一个文件组
SQLSERVER将一个文件组的数据移动到另一个文件组 有经验的大侠可以直接忽视这篇文章~ 这个问题有经验的人都知道怎麽做,因为我们公司的数据量不大没有这个需求,也不知道怎麽做实验 今天求助了QQ群里 ...
- SQL Server中的高可用性(2)----文件与文件组
在谈到SQL Server的高可用性之前,我们首先要谈一谈单实例的高可用性.在单实例的高可用性中,不可忽略的就是文件和文件组的高可用性.SQL Server允许在某些文件损坏或离线的情况下,允 ...
- C# ini文件操作【源码下载】
介绍C#如何对ini文件进行读写操作,C#可以通过调用[kernel32.dll]文件中的 WritePrivateProfileString()和GetPrivateProfileString()函 ...
- 【小程序分享篇 一 】开发了个JAVA小程序, 用于清除内存卡或者U盘里的垃圾文件非常有用
有一种场景, 手机内存卡空间被用光了,但又不知道哪个文件占用了太大,一个个文件夹去找又太麻烦,所以我开发了个小程序把手机所有文件(包括路径下所有层次子文件夹下的文件)进行一个排序,这样你就可以找出哪个 ...
- 【原】Android热更新开源项目Tinker源码解析系列之二:资源文件热更新
上一篇文章介绍了Dex文件的热更新流程,本文将会分析Tinker中对资源文件的热更新流程. 同Dex,资源文件的热更新同样包括三个部分:资源补丁生成,资源补丁合成及资源补丁加载. 本系列将从以下三个方 ...
- GreenDao 数据库:使用Raw文件夹下的数据库文件以及数据库升级
一.使用Raw文件夹下的数据库文件 在使用GreenDao框架时,数据库和数据表都是根据生成的框架代码来自动创建的,从生成的DaoMaster中的OpenHelper类可以看出: public sta ...
随机推荐
- [Windows Server 2012] Filezilla安全加固方法
★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com ★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频. ★ 本节我们将带领大家:FileZ ...
- JAVA中EXTENDS 与 IMPLEMENT 区别
简单说: 1.extends是继承父类,只要那个类不是声明为final或者那个类定义为abstract的就能继承,2.JAVA中不支持多重继承,但是可以用接口来实现,这样就要用到implements, ...
- __declspec(dllexport)
__declspec(dllexport) (2010-06-17 10:04:28) 转载▼ 标签: __declspec dllexport 导出 it 分类: C 先看代码:以下是在dev-c+ ...
- CentOS下使用yum安装配置和使用svn
安装说明 系统环境:CentOS-6.3安装方式:yum install (源码安装容易产生版本兼容的问题)安装软件:系统自动下载SVN软件 检查已安装版本 ? 1 2 3 4 5 6 7 8 9 1 ...
- Spring 注解注入的几种方式(转)
平常的java开发中,程序员在某个类中需要依赖其它类的方法,则通常是new一个依赖类再调用类实例的方法,这种开发存在的问题是new的类实例不好统一管理,spring提出了依赖注入的思想,即依赖类不由程 ...
- 手机访问pc版网站自动跳转为手机版页面
1.PC版首页</head>标签前加上以下脚本 <script src="/tools/browser_redirect.ashx"></script ...
- echarts demo
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- PKI 的组成
PKI(Public Key Infrastructure)公钥基础设施是提供公钥加密和数字签名服务的系统或平台,目的是为了管理密钥和证书.一个机构通过采用PKI 框架管理密钥和证书可以建立一个安全的 ...
- 【Codeforces 349B】Color the Fence
[链接] 我是链接,点我呀:) [题意] 让你组成一个只由1~9组成的数字 每个数字需要的paint数字给定. 让你组成一个最大的数字,且所有数字的paint的总和不超过v. [题解] 先求出a中的最 ...
- Python之路【第一篇】:Python基础
本节内容 Python介绍 发展史 Python 2 or 3? 安装 Hello World程序 变量 用户输入 模块初识 .pyc是个什么鬼? 数据类型初识 数据运算 表达式if ...else语 ...