有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java无法读取文本文件,也找不到解决方案

我尝试过许多读取文件的方法,但我发现它们都不起作用。我没有使用默认库。我正在使用一个从here中找到的框架

我试图读取它并将其逐行插入到字符串数组中。我尝试过的每个方法都需要一个上下文,但由于它的初始化方式与默认安卓应用程序不同,因此它没有使用getAssets的上下文。还有其他读取文本文件的方法吗

我试着使用这组代码,但它不起作用,因为它需要资产

    InputStream iS;
    resources = AndroidGame.resources;

    if (loadFromRawFolder) {
        int rID = resources.getIdentifier("raw/store_app", "raw", "com.cciamlazy.pixunited");
        iS = resources.openRawResource(rID);
    } else {
        iS = resources.getAssets().open(inFile);
    }
    byte[] buffer = new byte[iS.available()];
    iS.read(buffer);
    ByteArrayOutputStream oS = new ByteArrayOutputStream();
    oS.write(buffer);
    oS.close();
    iS.close();

这段代码给了我错误和资源!= null。给我一个错误:

安卓.content.res.Resources$NotFoundException: Resource ID #0x0

全类代码

package com.cciamlazy.pixunited;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import 安卓.app.Activity;
import 安卓.content.res.Resources;


public class MapLoader extends Activity {

    private static Resources resources;

    public MapLoader() {
        resources = getResources();
    }

    public static String LoadFile(String fileName, boolean loadFromRawFolder) throws IOException {

        // Create a InputStream to read the file into
        InputStream iS;

        if (loadFromRawFolder) {
            // get the resource id from the file name
            // int rID = resources.getIdentifier(fileName, null, null);
            int rID = resources.getIdentifier("raw/store_app", "raw",
                    "com.cciamlazy.activity");
            // get the file as a stream
            iS = resources.openRawResource(rID);
        } else {
            // get the file as a stream
            iS = resources.getAssets().open(fileName);
        }

        // create a buffer that has the same size as the InputStream
        byte[] buffer = new byte[iS.available()];
        // read the text file as a stream, into the buffer
        iS.read(buffer);
        // create a output stream to write the buffer into
        ByteArrayOutputStream oS = new ByteArrayOutputStream();
        // write this buffer to the output stream
        oS.write(buffer);
        // Close the Input and Output streams
        oS.close();
        iS.close();

        // return the output stream as a String
        return oS.toString();
    }

}

共 (1) 个答案

  1. # 1 楼答案

    希望这有助于

    将这两行添加到您的活动中

    private static Resources resources;
    
    resources = getResources();
    

    然后添加此方法

     public static String LoadFile(String fileName, boolean loadFromRawFolder)
    
    
                throws IOException {
    
            // Create a InputStream to read the file into
            InputStream iS;
    
            if (loadFromRawFolder) {
                // get the resource id from the file name
                // int rID = resources.getIdentifier(fileName, null, null);
                int rID = resources.getIdentifier("raw/store_app", "raw",
                        "com.example.activity");
                // get the file as a stream
                iS = resources.openRawResource(rID);
            } else {
                // get the file as a stream
                iS = resources.getAssets().open(fileName);
            }
    
            // create a buffer that has the same size as the InputStream
            byte[] buffer = new byte[iS.available()];
            // read the text file as a stream, into the buffer
            iS.read(buffer);
            // create a output stream to write the buffer into
            ByteArrayOutputStream oS = new ByteArrayOutputStream();
            // write this buffer to the output stream
            oS.write(buffer);
            // Close the Input and Output streams
            oS.close();
            iS.close();
    
            // return the output stream as a String
            return oS.toString();
        }