【问题标题】:Loading text file gives an error with path加载文本文件时出现路径错误
【发布时间】:2016-12-12 09:30:32
【问题描述】:

我想阅读一个文本文件。为此,我给出了文件的路径,但它没有被读取。

出现错误,例如:ClassLoader 引用了未知路径:/data/app/com.kiranaapp-1/lib/arm

我已将文本文件保存在应用程序的帮助文件夹中。

public void ReadFile() {

    try {
        BufferedReader in = new BufferedReader(new FileReader("E:/siddhiwork/KiranaCustomerApp/app/src/main/java/com/kiranacustomerapp/helper/itemNames.txt"));
        String str;

        List<String> list = new ArrayList<String>();
        while ((str = in.readLine()) != null) {
            list.add(str);
        }

        String[] stringArr = list.toArray(new String[0]);
    }
    catch (FileNotFoundException e)
    {
        System.out.print(e);
    }
    catch (IOException e)
    {
        System.out.print(e);
    }
}

当我调试以查看文件是否正在被读取并且字符串是否存储在数组中时, 但什么也没发生。

请帮忙,谢谢..

编辑:

我尝试在列表中获取字符串,但在 itemList 中没有得到任何值

public class StartupActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);



    List<String> itemList = new ArrayList<>();

    itemList = readRawTextFile(StartupActivity.this);

    }

    public static List<String> readRawTextFile(Context context) {
        String sText = null;
        List<String> stringList;
     try{

        InputStream is = context.getResources().openRawResource(R.raw.item_names);
        //Use one of the above as per your file existing folder

        int size = is.available();
        byte[] buffer = new byte[size];
        is.read(buffer);
        is.close();
        sText = new String(buffer, "UTF-8");

        stringList = new ArrayList<String>(Arrays.asList(sText.split(" ")));
         System.out.print(stringList);
    } catch (IOException ex) {
        ex.printStackTrace();
        return null;
    }
    return stringList;

    }
}

【问题讨论】:

  • 用双反斜杠替换每个斜杠可能会有所帮助
  • 也试过了。不工作。 @XtremeBaumer
  • 我认为,该文件应该放在原始资源文件夹中。
  • 我们有布局和drawabels的文件夹? @艾哈迈德

标签: java android file path


【解决方案1】:

您不应将文件路径指定为计算机路径。将文件存储在 assets 文件夹或 raw 文件夹中,然后在 android 中从那里获取。

public String loadTextFromFile() {
        String sText = null;
        try {
            //If your file is in assets folder
            InputStream is = getAssets().open("file_name.txt");
            //If your file is in raw folder
            InputStream is = getResources().openRawResource(R.raw.file_name);
            //Use one of the above as per your file existing folder

            int size = is.available();
            byte[] buffer = new byte[size];
            is.read(buffer);
            is.close();
            sText = new String(buffer, "UTF-8");
        } catch (IOException ex) {
            ex.printStackTrace();
            return null;
        }
        return sText;
    }

用“,”格式分割文本:

String[] sTextArray = sText.replace("\"", "").split(",");
List<String> stringList = new ArrayList<String>(Arrays.asList(sTextArray));

【讨论】:

  • 我的文件包含这样的数据:“Potato”、“rice”、“Eggs”、“Maggi”、“Dryfruits”、“Maza”、“cold drink”、“sauce”、“catchup” ”、“椰子油”@Ready Android
  • 检查字符串数组列表的最后一行答案
  • 这可行,但我正在尝试将此列表添加到活动中的另一个列表中。为什么它不起作用。请检查已编辑的问题。 @Ready Android
  • 显示代码你是如何将这个 stringList 添加到你活动中的另一个数组列表中的?我认为您没有完全按照我在回答中发布的内容进行操作。再次检查我的答案。
【解决方案2】:

首先,将文件放在res目录下的raw目录下。 现在试试下面的代码来读取文件,

public static String readRawTextFile(Context ctx, int resId) {
    InputStream inputStream = ctx.getResources().openRawResource(resId);

    InputStreamReader inputreader = new InputStreamReader(inputStream);
    BufferedReader buffreader = new BufferedReader(inputreader);
    String line;
    StringBuilder text = new StringBuilder();

    ArrayList<String> lineList = new ArrayList<>();
    try {
        while (( line = buffreader.readLine()) != null) {
            text.append(line);
            lineList.add(line);
            text.append('\n');
        }
    } catch (IOException e) {
        return null;
    }

    // Use your arraylist here, since its filled up.
    return text.toString();
}

【讨论】:

  • 提供什么作为int resId? @向导
  • 文件的资源 ID - R.raw.file_name
  • 文件正在被读取,但我想将每个字符串存储在一个数组列表中。 @向导
  • 你的意思是每一行??
  • 您想将该字符串的每个单词保存在字符串 ArrayList 中,然后检查 readyandroid 答案。
【解决方案3】:

如果文件是在缓存中动态生成的,可以

File file = getCacheDir() + "FOLDER_PATH_WITH_FILENAME";

否则,将文件保存在主目录内的 assets 文件夹中。

main
-----> java
-----> res
-----> assets
-----> AndroidManifest.xml

然后,使用以下方法获取文件:

InputStream inputStream = getAssets().open("FILE_NAME");

【讨论】:

  • 不会修复它,因为/data/app/com.kiranaapp-1/lib/arm 确实 not 出现在 E:/siddhiwork/KiranaCustomerApp/app/src/main/java/com/kiranacustomerapp/helper/itemNames.txt
  • E://是windows目录地址。他应该使用 assets 或 raw。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-17
  • 2020-06-21
  • 2013-08-17
  • 2018-11-15
  • 1970-01-01
  • 2021-10-03
相关资源
最近更新 更多