我尝试了很多阅读文件的方法,但我发现它们都不起作用。我没有使用默认库。我使用的是here
中的框架我试图读取它并将其逐行插入到String数组中。我尝试过的每个方法都需要一个Context,但由于这个方法没有像默认的android应用程序那样初始化,因此它没有使用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
。给我错误:
android.content.res.Resources$NotFoundException: Resource ID #0x0
完整的班级代码
package com.cciamlazy.pixunited;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.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();
}
}
答案 0 :(得分: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();
}