【问题标题】:FileInputStream for a raw file原始文件的 FileInputStream
【发布时间】:2016-01-18 04:31:40
【问题描述】:

我在res/raw 中有一个名为“pack.dat”的原始文件。

我可以使用以下代码打开InputStream

    InputStream fis = null;
    try {
        fis = context.getResources().openRawResource(R.raw.pack);
        BufferedReader br = new BufferedReader(new InputStreamReader(fis));
        String nextLine;
        int i = 0, j = 0;
        while ((nextLine = br.readLine()) != null) {
            if (j == 5) {
                j = 0;
                i++;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

这是有效的,我可以从那个文件中读取。

但不幸的是,我需要FileInputStream。当我这样做时:

    FileInputStream fs = null;
    Uri url = Uri.parse("android.resource://" + 
                     context.getPackageName() + "/" + R.raw.pack);
    File file = new File(url.toString());
    try {
        fs = new FileInputStream(file);
        fs.getChannel().position(0);
        fs.read(bDatensatz, 0, indexlaenge);
    } catch (Exception e) {
        e.printStackTrace();
    }

我在

收到“file not found
fs = new FileInputStream(file);

context.getResources().openRawResource(R.raw.pack) 在第一个示例中返回一个InputStream

我可以用什么来获得FileInputStream

【问题讨论】:

    标签: java android android-studio inputstream fileinputstream


    【解决方案1】:

    从另一个线程复制!可能这对你有帮助

    FileInputStream fis;
    fis = openFileInput("test.txt");
    StringBuffer fileContent = new StringBuffer("");
    
    byte[] buffer = new byte[1024];
    int n  = 0;
    
    while ((n = fis.read(buffer)) != -1) 
    { 
      fileContent.append(new String(buffer, 0, n)); 
    } 
    

    【讨论】:

    • 这只有在我有一个带有文件名的字符串时才有效。但我的文件在 res/raw 中。使用 url.toString 我得到类似“android.resource://com.foo.myprogram/2131099648”的东西。我该如何处理这个文件?
    • 我认为 url.toString() 方法存在问题。您正在尝试将 R.raw.pack(资源 ID)解析为字符串,这不是您要查找的内容!
    【解决方案2】:

    但不幸的是我需要一个 FileInputStream

    为什么?

    我在 fs = new FileInputStream(file); 处得到一个“找不到文件”;

    那是因为您试图打开的不是文件。

    我可以用什么来获取 FileInputStream?

    您需要将资源复制到本地文件(例如,使用 openFileOutput() 和 Java I/O)。然后,您可以打开本地文件(例如,使用openFileInput())并获得FileInputStream

    或者,只需使用InputStream,修复您正在使用的任何期望FileInputStream 的代码。

    【讨论】:

    • 1.我需要设置随机读取的文件位置。 2. 文件“res/raw/pack.dat”可用。在我的第二个示例中,您可以看到我是如何准备文件的。 3. 你是说不可能用 FileInputStream 读取原始文件吗?该文件大小为 15 MB,我想避免将其复制到本地文件,尤其是不要复制到 sdcard。
    • @thpitsch: "文件 "res/raw/pack.dat" 可用" -- 在你的开发机器上。它不是 Android 上的文件。 “你想说用 FileInputStream 读取原始文件是不可能的吗?” -- 正确。
    • 数据在资源中,并与apk一起发送到设备。如果不是文件,如何复制到 getMainActivity().getFilesDir()?
    • @thpitsch: "如果不是文件,如何复制到 getMainActivity().getFilesDir()?" -- 使用您的第一个代码 sn-p 在原始资源上获取 InputStream。然后,在您想要的文件上获取FileOutputStream(例如,new FileOutputStream(new File(getFilesDir(), "thing.txt")))。然后,使用 Java I/O 将数据从InputStream 复制到FileOutputStream,完成后刷新并关闭FileOutputStream。有很多代码 sn-ps 展示了如何在 Java 中将数据从一个流复制到另一个流。
    • 谢谢!经过几天尝试在原始资源上构建 FileInputStream,现在我在第一次启动时将该文件复制到应用程序的 FilesDir,然后可以根据需要使用 FileInputStream。
    猜你喜欢
    • 2012-02-18
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 2011-02-19
    • 1970-01-01
    • 1970-01-01
    • 2014-06-06
    相关资源
    最近更新 更多