欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Andriod 資源文件之存取操作

 更新時(shí)間:2016年03月06日 16:08:55   作者:gisoracle  
這篇文章主要介紹了Andriod 資源文件之存取操作的相關(guān)資料,需要的朋友可以參考下

廢話不多說了,直接給大家貼代碼了。具體代碼如下所述:

<?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)
{
//獲取資源對(duì)象
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文件不存在,需要你手動(dòng)創(chuàng)建。

以上所述是小編給大家介紹的Andriod 資源文件之存取操作的相關(guān)知識(shí),希望對(duì)大家以上幫助!

相關(guān)文章

最新評(píng)論