Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Andriod 资源文件之存取操作大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

废话不多说了,直接给大家贴代码了。具体代码如下所述:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://scheR_389_11845@as.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 资源文件之存取操作的相关知识,希望对大家以上帮助!

大佬总结

以上是大佬教程为你收集整理的Andriod 资源文件之存取操作全部内容,希望文章能够帮你解决Andriod 资源文件之存取操作所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。