服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Andriod 资源文件之存取操作

Andriod 资源文件之存取操作

2021-06-23 15:45gisoracle Android

这篇文章主要介绍了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"?>
<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 资源文件之存取操作的相关知识,希望对大家以上帮助!

延伸 · 阅读

精彩推荐