来自 R.raw.file 的 Android FileReader

发布于 2024-11-29 23:35:32 字数 379 浏览 0 评论 0原文

真正的新手问题:

我有一个需要阅读的 .csv 文件。我把它放在raw文件夹里了。为了方便起见,我使用 http://opencsv.sourceforge.net/ 库来读取文件。该库提供了此方法来创建 CSVReader 对象:

CSVReader reader = new CSVReader(new FileReader("yourfile.csv"));

但我不知道如何将此构造函数指向我的文件,因为 Android 中的文件通常像 R.raw.file 一样引用,而不是文件的字符串地址。

任何帮助将不胜感激。

Really newbie question:

I have a .csv file that I need to read. I've put it in the raw folder. For convenience, Im' using the http://opencsv.sourceforge.net/ library for reading the file. The library provides this method for creating a CSVReader object:

CSVReader reader = new CSVReader(new FileReader("yourfile.csv"));

But I don0't get how to point this constructor to my file, since the file in Android is usually referenced like R.raw.file rather than a String address to the file.

Any help would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉风有信 2024-12-06 23:35:32

你想做这样的事情 -

public void readCSVFromRawResource(Context context)
{
    //this requires there to be a dictionary.csv file in the raw directory
    //in this case you can swap in whatever you want
    InputStream inputStream = getResources().openRawResource(R.raw.dictionary);
    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

    try
    {
       String word;//word
       int primaryKey = 0;//primary key
       Map dictionaryHash = new HashMap();

       while ((word = reader.readLine()) != null)
       {
           if(word.length() < 7)
           {
               dictionaryHash.put(primaryKey,word );
               primaryKey++;



               if(primaryKey % 1000 == 0)
                   Log.v("Percent load completed ", " " + primaryKey);
           }
       }

        //write the dictionary to a file
        File file = new File(DICTIONARY_FILE_NAME);
        BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(DICTIONARY_FILE_NAME));
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(dictionaryHash);
        oos.flush();
        oos.close();
                 Log.v("alldone","done");

   }
   catch (Exception ex) {
       // handle exception
       Log.v(ex.getMessage(), "message");
   }
   finally
    {
       try
       {
           inputStream.close();

       }
       catch (IOException e) {
           // handle exception
          Log.v(e.getMessage(), "message");
       }
   }
}

You want to do something like this -

public void readCSVFromRawResource(Context context)
{
    //this requires there to be a dictionary.csv file in the raw directory
    //in this case you can swap in whatever you want
    InputStream inputStream = getResources().openRawResource(R.raw.dictionary);
    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

    try
    {
       String word;//word
       int primaryKey = 0;//primary key
       Map dictionaryHash = new HashMap();

       while ((word = reader.readLine()) != null)
       {
           if(word.length() < 7)
           {
               dictionaryHash.put(primaryKey,word );
               primaryKey++;



               if(primaryKey % 1000 == 0)
                   Log.v("Percent load completed ", " " + primaryKey);
           }
       }

        //write the dictionary to a file
        File file = new File(DICTIONARY_FILE_NAME);
        BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(DICTIONARY_FILE_NAME));
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(dictionaryHash);
        oos.flush();
        oos.close();
                 Log.v("alldone","done");

   }
   catch (Exception ex) {
       // handle exception
       Log.v(ex.getMessage(), "message");
   }
   finally
    {
       try
       {
           inputStream.close();

       }
       catch (IOException e) {
           // handle exception
          Log.v(e.getMessage(), "message");
       }
   }
}
吻风 2024-12-06 23:35:32

您可以使用此解决方案从原始资源获取字符串:
Android 读取文本原始资源文件

,然后使用 StringReader 而不是 FileReader 与 CSVReader 构造函数。

You can use this solution to get a String from the raw resource:
Android read text raw resource file

then use StringReader instead of FileReader with the CSVReader constructor.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文