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)文章
Android創(chuàng)建簡單發(fā)送和接收短信應(yīng)用
收發(fā)短信應(yīng)該是每個手機最基本的功能之一了,即使是許多年前的老手機也都會具備這項功能,而Android 作為出色的智能手機操作系統(tǒng),自然也少不了在這方面的支持。今天我們開始自己創(chuàng)建一個簡單的發(fā)送和接收短信的應(yīng)用,需要的朋友可以參考下2016-04-04
Android中導(dǎo)航組件Navigation的實現(xiàn)原理
大家好,本篇文章主要講的是Android中導(dǎo)航組件Navigation的實現(xiàn)原理,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下2022-02-02
Android ListView position詳解及實例代碼
這篇文章主要介紹了Android ListView position的相關(guān)資料,在開發(fā)Android 應(yīng)用的時候你真的用對了嗎?這里給大家徹底解釋下,需要的朋友可以參考下2016-10-10
Android編程使用自定義shape實現(xiàn)shadow陰影效果的方法
這篇文章主要介紹了Android編程使用自定義shape實現(xiàn)shadow陰影效果的方法,涉及Android中xml文件布局的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-11-11
Android中查看USB連接的外接設(shè)備信息的代碼實例
這篇文章主要介紹了Android中查看USB連接的外接設(shè)備信息的代碼實例,需要的朋友可以參考下2014-04-04
Android UI設(shè)計系列之自定義ViewGroup打造通用的關(guān)閉鍵盤小控件ImeObserverLayout(9)
這篇文章主要介紹了Android UI設(shè)計系列之自定義ViewGroup打造通用的關(guān)閉鍵盤小控件ImeObserverLayout,具有一定的實用性和參考價值,感興趣的小伙伴們可以參考一下2016-06-06
Android設(shè)備與外接U盤實現(xiàn)數(shù)據(jù)讀取操作的示例
本篇文章主要介紹了Android設(shè)備與外接U盤實現(xiàn)數(shù)據(jù)讀取操作的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11

