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 ...
随机推荐
- codeforces_724C_Ray Tracing
C. Ray Tracing time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- servlet学习总结(一)——HttpServletRequest(转载)
原文地址:http://www.cnblogs.com/xdp-gacl/p/3798347.html 一.HttpServletRequest介绍 HttpServletRequest对象代表客户端 ...
- copy the content of a file muliptle times and save as ordered files:
input: transient.case outputs: transient_1.case, transient_2.case,...transient_101.case ********** n ...
- BZOJ 1232 USACO 2008 Nov. 安慰奶牛Cheer
[题解] 对于每一条边,我们通过它需要花费的代价是边权的两倍加上这条边两个端点的点权. 我们把每条边的边权设为上述的值,然后跑一边最小生成树,再把答案加上最小的点权就好了. #include<c ...
- noip模拟赛 Chtholly Nota Seniorious
题目背景 大样例下发链接: https://pan.baidu.com/s/1nuVpRS1 密码: sfxg こんなにも.たくさんの幸せをあの人に分けてもらった だから.きっと 今の.私は 谁が何と ...
- 清北学堂模拟赛d3t5 c
分析:其实就是一道数学题.如果以左下角的点为原点建立平面直角坐标系,那么点(b,a)是最容易卡住棺材的.我们求出棺材左边到点(b,a)的距离最小值,只有w小于等于这个最小值才能被拉过去.那么先求出左面 ...
- Servlet中使用RequestDispatcher调派请求--forware
顺便演示了MVC的作法,以后hello.view可以移交到jsp中处理. 而MODEL和CONTROL,VIEW就实现了分享. HelloModel.java: package cc.openhome ...
- 小记——GTMD校园网
前言 学校一年前开通了校园网,然鹅信号未覆盖我们住的公寓,又多忍受了一年的小破宽带(10M带宽,100块300个小时) 上个星期,架设了一年的校园网终于通了,然后我们发现——校园网69元一个月,一个用 ...
- K - 贪心 基础
FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containi ...
- N天学习一个linux命令之ssh
用途 通过加密连接,远程登录主机和在远程主机执行命令,也可以用于转发x11和tcp,也可用于搭建VPN.第一次连接时,会弹出远程主机公钥指纹确认信息,通过这个方式防止中间人攻击. 用法 ssh [op ...