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>