Android的工作室从阅读原始资源文本文件 [英] Android Studio Reading from Raw Resource Text File

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

问题描述

我想保持我的资源.txt文件\\原始文件在Android中Studio和读/解析文件。我有一个名为原始的文件夹,我在资源目录下创建一个文件my_file.txt适用(即我没有创建)。

这里就是我想我的主要问题是:什么时候创建File对象,我应该通过什么路径在我的文本文件(与扫描对象使用)

下面是我的code:

 私人无效ReadFile的(){
    扫描仪扫描仪= NULL;
    尝试{
        扫描仪=新的扫描仪(新File(\\\\ \\\\资源\\\\原料my_file.txt适用)); //我也试着不带扩展名为.txt
        Log.v(readFile->尝试,试图读取文件。);
    }赶上(例外五)
    {
        //发送错误信息。
    }
    如果(扫描仪!= NULL){
        而(scanner.hasNext()){
            的String []行= scanner.nextLine()分裂(\\ t的)。
            //处理字符串这里
        }
    }
}


解决方案

想想你正在寻找沿

东西线

  InputStream为= ctx.getResources()openRawResource(RES_ID)。

在哪里CTX是上下文的实例

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).

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?

Here's my code:

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);

Where ctx is a instance of Context

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

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