Andriod 資源文件之存取操作
廢話不多說了,直接給大家貼代碼了。具體代碼如下所述:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/read"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="讀取資源文件(Raw)" />
<TextView
android:id="@+id/cont"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
package com.example.yanlei.wifi;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;
public class MainActivity extends AppCompatActivity {
private Button btnRead=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnRead=(Button)super.findViewById(R.id.read);
//讀取資源文件
btnRead.setOnClickListener(new OnClickListener(){
public void onClick(View v)
{
//獲取資源對象
Resources res=MainActivity.this.getResources();
//通過openRawResource()讀取資源為R.raw.friend的資源文件,結(jié)果返回到InputStream
InputStream input=res.openRawResource(R.raw.friend);
//讀取資源文件內(nèi)容
Scanner scan=new Scanner(input);
StringBuffer info=new StringBuffer();
while(scan.hasNext())
info.append(scan.next()).append("\n");
scan.close();
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
Toast.makeText(getApplicationContext(), info.toString(),Toast.LENGTH_LONG).show();
}
});
}
}
我們把文件friend.txt保存到res/raw文件夾中。
注意:raw文件不存在,需要你手動創(chuàng)建。
以上所述是小編給大家介紹的Andriod 資源文件之存取操作的相關(guān)知識,希望對大家以上幫助!
相關(guān)文章
ListView的Adapter使用 之 初學(xué)ArrayAdapter String
ListView是Android中經(jīng)常會使用的東西,綁定數(shù)據(jù)對于初學(xué)者來說,尤其是剛接觸編程的人來說,往往會覺得很難理解,我上大二的時候?qū)W的java,但是基本上相當(dāng)于沒有學(xué),什么都沒寫過,真正接觸編程就是開始上手學(xué)android,把這些記錄下來,自己可以回頭看下,也可以讓新手更好的理解2013-06-06
Android studio gradle環(huán)境變量配置教程
這篇文章主要為大家詳細(xì)介紹了Android studio gradle環(huán)境變量配置教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-05-05
Android判斷服務(wù)是否運(yùn)行及定位問題實例分析
這篇文章主要介紹了Android判斷服務(wù)是否運(yùn)行及定位問題,以實例形式較為詳細(xì)的分析了Android判斷服務(wù)運(yùn)行狀態(tài)及獲取經(jīng)緯度的相關(guān)實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-09-09
Android自定義下拉刷新控件RefreshableView
這篇文章主要介紹了Android自定義下拉刷新控件RefreshableView,支持所有控件的下拉刷新,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-11-11
Android學(xué)習(xí)筆記-保存數(shù)據(jù)到SQL數(shù)據(jù)庫中(Saving Data in SQL Databases)
這篇文章主要介紹了Android學(xué)習(xí)筆記-保存數(shù)據(jù)到SQL數(shù)據(jù)庫中的(Saving Data in SQL Databases)2014-10-10
Android Retrofit2數(shù)據(jù)解析代碼解析
這篇文章主要介紹了Android Retrofit2數(shù)據(jù)解析代碼解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-12-12

