Java Can';t读取文本文件并可以';我找不到解决办法
我尝试过许多读取文件的方法,但我发现它们都不起作用。我没有使用默认库。我使用的框架是从 我试图读取它并将其逐行插入到字符串数组中。我尝试过的每个方法都需要一个上下文,但由于它的初始化方式与默认android应用程序不同,因此它没有使用getAssets的上下文。还有其他读取文本文件的方法吗 我试着使用这组代码,但它不起作用,因为它需要资产Java Can';t读取文本文件并可以';我找不到解决办法,java,android,Java,Android,我尝试过许多读取文件的方法,但我发现它们都不起作用。我没有使用默认库。我使用的框架是从 我试图读取它并将其逐行插入到字符串数组中。我尝试过的每个方法都需要一个上下文,但由于它的初始化方式与默认android应用程序不同,因此它没有使用getAssets的上下文。还有其他读取文本文件的方法吗 我试着使用这组代码,但它不起作用,因为它需要资产 InputStream iS; resources = AndroidGame.resources; if (loadFromRaw
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();
此代码为我提供错误和资源!=空
。给我一个错误:
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();
}
}
希望这有助于
将这两行添加到您的活动中
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();
}
请发布一些代码“资源”在我的情况下不是一个变量。请参阅我编辑的答案。我不能使用getResources,因为我没有使用android应用程序的正常框架。我不使用res,甚至当我导入它时,我知道我必须创建一个名为GetResources的方法。你说的普通框架是什么意思?我真的不明白。我想在编写代码之前,请阅读android开发者网站。你可以在这里完整复制和粘贴代码。这对任何人都有帮助。我更新了我的代码,现在仍然可以给了我错误