編輯:關於Android編程
廢話不多說了,直接給大家貼代碼了。具體代碼如下所述:
<?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的資源文件,結果返回到InputStream
InputStream input=res.openRawResource(R.raw.friend);
//讀取資源文件內容
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文件不存在,需要你手動創建。
以上所述是小編給大家介紹的Andriod 資源文件之存取操作的相關知識,希望對大家以上幫助!
Android Design之Android4.0不一樣的裡程碑
想成為Android的傑出開發工程師,不懂得Android的設計規則怎麼可以,Android4.0問世後谷歌公司為Android程序員規范了一系列的設計原則,不要再盲目的
安卓Android-x86 4.4聯網設置教程 解決不能上網
Android-x86 4.4安裝完成後一般都是不能上網的,需要給網卡設置IP地址和DNS服務器方可聯網。Android-x86在本質上就是Linux系統,
Android多媒體-支持的多媒體格式
1.Network Protocols RTSP (RTP, SDP) HTTP progressive streaming HTTP live streaming dr
Android網絡框架Volley
Volley是Google I/O 2013推出的網絡通信庫,在volley推出之前我們一般會選擇比較成熟的第三方網絡通信庫,如:android-async-httpre