这篇文章主要介绍了Andriod 资源文件之存取操作的相关资料,需要的朋友可以参考下
这篇文章主要介绍了Andriod 资源文件之存取操作的相关资料,需要的朋友可以参考下
这篇文章主要介绍了Andriod 资源文件之存取操作的相关资料,需要的朋友可以参考下
废话不多说了,直接给大家贴代码了。具体代码如下所述:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><Buttonandroid:id="@+id/read"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="读取资源文件(Raw)" /><TextViewandroid: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;@Overrideprotected 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的资源文件,结果返回到InputStreamInputStream 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 资源文件之存取操作的相关知识,希望对大家以上帮助!
发表评论