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 ...
随机推荐
- 28 I/O限制的异步操作
28.2 C#的异步函数 private static async Task<string> IssueClientRequestAsync(string serverName, stri ...
- How To:防火墙规则去重
主要命令 iptables-save| awk ' !x[$0]++ | iptables-restore 演示: [root@testname ~]# iptables -vL Chain INPU ...
- 洛谷——P1196 [NOI2002]银河英雄传说
P1196 [NOI2002]银河英雄传说 题目大意: 给你一个序列,支持两种操作: 合并指令为$M_{i,j}$j,含义为第i号战舰所在的整个战舰队列,作为一个整体(头在前尾在后)接至第j号战舰所 ...
- Python time & random模块
time模块 三种时间表示 在Python中,通常有这几种方式来表示时间: 时间戳(timestamp) : 通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的 ...
- 【Codeforces 118B】Caesar's Legions
[链接] 我是链接,点我呀:) [题意] 序列中不能连续出现k1个以上的1以及不能连续出现k2个以上的2,然后一共有n1个1以及n2和2,要求这n1+n2个数字都出现. 问序列有多少种可能. [题解] ...
- 【[Offer收割]编程练习赛12 B】一面砖墙
[题目链接]:http://hihocoder.com/problemset/problem/1494 [题意] [题解] 显然只要记住每一行的各个砖头的间隔处的坐标有多少个就好了: ->也就对 ...
- Android欢迎页面以及引导页面
开发环境:Windows 10 x64,Android Studio 3.0 很多APP都会在启动主界面(MainActivity)之前显示一个短暂的欢迎页面,设置微博,知乎,百度之类APP还是在欢迎 ...
- html实现 省——市——区三级联动
html实现 省——市——区三级联动 html中实现三级联动是一个不错的demo,博主在这里跟大家分享一下实现的过程,以及自己在过程中出现的一些问题,仅供参考. 首先我们将全国的省市区数据导入进来, ...
- 阿伦 凯 Alan Kay 面向对象编程思想创始人
The best way to predict the future is to invent it. 预测未来最好的办法就是创造它.
- COCOS2D 学习笔记
cc.Node.scheduleUpdate:function () * schedules the "update" method. ...