android中的资源文件,在Android中访问资源文件

我的/ res / raw /文件夹(/res/raw/textfile.txt)中有一个资源文件,我正在尝试从我的android应用中读取该文件进行处理。

public static void main(String[] args) {

File file = new File("res/raw/textfile.txt");

FileInputStream fis = null;

BufferedInputStream bis = null;

DataInputStream dis = null;

try {

fis = new FileInputStream(file);

bis = new BufferedInputStream(fis);

dis = new DataInputStream(bis);

while (dis.available() != 0) {

// Do something with file

Log.d("GAME", dis.readLine());

}

fis.close();

bis.close();

dis.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

我尝试了不同的路径语法,但始终会遇到 java.io.FileNotFoundException

错误。如何访问/res/raw/textfile.txt进行处理?是 File file = new File(“ res / raw /

textfile.txt”); Android中的错误方法?

**_答案: _

// Call the LoadText method and pass it the resourceId

LoadText(R.raw.textfile);

public void LoadText(int resourceId) {

// The InputStream opens the resourceId and sends it to the buffer

InputStream is = this.getResources().openRawResource(resourceId);

BufferedReader br = new BufferedReader(new InputStreamReader(is));

String readLine = null;

try {

// While the BufferedReader readLine is not null

while ((readLine = br.readLine()) != null) {

Log.d("TEXT", readLine);

}

// Close the InputStream and BufferedReader

is.close();

br.close();

} catch (IOException e) {

e.printStackTrace();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值