日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

3.3 文件I/O

發布時間:2025/3/15 编程问答 12 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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;@Overrideprotected 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; @Overridepublic void onClick(View v) {etContent = (EditText) findViewById(R.id.EditText01);loadFromAssert("AndroidSummary.txt");}});} /*@Overridepublic 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(){@Overridepublic void run() {// TODO Auto-generated method stub etContent.setText(content);}});} catch (IOException e) {// TODO Auto-generated catch blockToast.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; //打開按鈕 @Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);but = (Button) findViewById(R.id.Button01);but.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {EditText etContent = (EditText) findViewById(R.id.EditText01);String contentResult = loadFromAssert("AndroidSummary.txt");etContent.setText(contentResult);}});} /*@Overridepublic 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 blockToast.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-sdkandroid:minSdkVersion="8"android:targetSdkVersion="17" /><applicationandroid:allowBackup="true"android:icon="@drawable/icon"android:label="@string/app_name"android:theme="@style/AppTheme" ><activityandroid: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" ><Buttonandroid:text="打開"android:id="@+id/Button01"android:layout_width="fill_parent"android:layout_height="wrap_content"></Button> <!-- 添加Button按鈕 --><ScrollViewandroid:id="@+id/ScrollView01"android:layout_height="wrap_content"android:layout_width="fill_parent"><EditTextandroid:editable="false"android:id="@+id/EditText01"android:layout_width="fill_parent"android:layout_height="wrap_content"> </EditText> <!-- 添加EditText --></ScrollView> <!-- 添加ScrollView --></LinearLayout>

?

轉載于:https://www.cnblogs.com/ZHONGZHENHUA/p/7458664.html

總結

以上是生活随笔為你收集整理的3.3 文件I/O的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。