<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="base.readassetsfile.MainActivity">
<Button
android:textSize="20sp"
android:text="@string/aasets_txt"
android:id="@+id/readFile"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:textSize="20sp"
android:text="@string/raw"
android:id="@+id/readRawFile"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
package base.readassetsfile;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.readFile).setOnClickListener(this);
findViewById(R.id.readRawFile).setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.readFile:
readAsset();
break;
case R.id.readRawFile:
readRaw();
break;
}
}
public void readAsset(){
try {
//获取文件中的字节
InputStream inputStream=getResources().getAssets().open("Test.txt");
//将字节转换为字符
InputStreamReader isReader=new InputStreamReader(inputStream,"UTF-8");
//使用bufferReader去读取内容
BufferedReader reader=new BufferedReader(isReader);
String out="";
while((out=reader.readLine())!=null){
Log.d("读取到的文件信息:",out);
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void readRaw(){
try {
//获取文件中的内容
InputStream inputStream=getResources().openRawResource(R.raw.test);
//将文件中的字节转换为字符
InputStreamReader isReader=new InputStreamReader(inputStream,"UTF-8");
//使用bufferReader去读取字符
BufferedReader reader=new BufferedReader(isReader);
String out="";
try {
while((out=reader.readLine())!=null){
Log.d("从raw文件夹中读取到的数据:",out);
}
} catch (IOException e) {
e.printStackTrace();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有