从原始资源文本文件读取 [英] Reading from a raw resource text file

查看:56
本文介绍了从原始资源文本文件读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Android Studio的res \ raw文件中保留一个.txt文件,并读取/解析该文件.我在"res"目录(未创建)中创建的名为"raw"的文件夹中有一个文件"my_file.txt".

I'm trying to keep a .txt file in my res\raw file in Android Studio and read/parse the file. I have a file "my_file.txt" in a folder called "raw" that I created in the "res" directory (that I didn't create).

这就是我的主要问题:创建File对象(与Scanner对象一起使用)时,我应该为文本文件传递什么路径?

Here's what I think my main issue is: When creating the File object (to use with the Scanner object), what path should I pass in for my text file?

这是我的代码:

private void readFile(){
    Scanner scanner = null;
    try {
        scanner = new Scanner(new File("\\res\\raw\\my_file.txt"));  //I've also tried without the .txt extension
        Log.v("readFile->try","Trying to read file.");
    }catch(Exception e)
    {
        //Send error message.
    }
    if(scanner != null) {
        while (scanner.hasNext()) {
            String[] line = scanner.nextLine().split("\t");
            //Process string here
        }
    }
}

推荐答案

认为您正在寻找一些类似的东西

Think you are looking for something along the lines of

InputStream is = ctx.getResources().openRawResource(res_id);

ctx是上下文的实例

Where ctx is a instance of Context

这篇关于从原始资源文本文件读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆