Android force在连续5次从内部存储器读取数据时关闭
在我的android应用程序中,每次加载新游戏时,我都会从内部存储器读取一个文件。 我这样做的前4次,它工作正常,但在第五次它强制关闭。 这是我的密码Android force在连续5次从内部存储器读取数据时关闭,android,file-io,Android,File Io,在我的android应用程序中,每次加载新游戏时,我都会从内部存储器读取一个文件。 我这样做的前4次,它工作正常,但在第五次它强制关闭。 这是我的密码 private String readFromInternalStorage(String filename) { FileInputStream fis=null; byte[] bytes = new byte[1000000]; try { fis=startGame.openFileInput(fi
private String readFromInternalStorage(String filename) {
FileInputStream fis=null;
byte[] bytes = new byte[1000000];
try {
fis=startGame.openFileInput(filename);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
fis.read(bytes);
} catch (IOException e) {
e.printStackTrace();
}
return new String(bytes);
}
在处理代码时,我注意到,如果我更改字节数组的长度,它会更改在不强制关闭的情况下读取文件的次数。如果我将长度更改为2000000,它将在第二次之后关闭,如果我将其更改为100000,它将在第八次之后关闭。我不知道为什么会发生这种情况,因为每次调用该方法时,我都会创建一个新的字节数组,所以我认为大小不会改变任何东西
更新:
在回去做了更多的测试之后,似乎文件输入与我的应用程序为何强制关闭无关。当这段代码被注释掉时,应用程序将连续加载五个级别而不强制关闭,所以我认为这是问题所在,但在八次尝试后,它仍然强制关闭。很明显,还有其他一些东西不起作用。无论如何感谢您的帮助。我在您的代码中没有看到“close()”:
- 您不应该硬编码数组大小。此外,您应该使用finally,以确保
FileInputStream
关闭,即使失败
下面是一个代码示例,演示了如何执行此操作:
FileInputStream fis;
String info = "";
try {
fis = mContext.openFileInput(this.fileName);
byte[] dataArray = new byte[fis.available()];
if (dataArray.length > 0) {
while (fis.read(dataArray) != -1) {
info = new String(dataArray);
}
Log.i("File Reading" , "Success!");
isOk = true;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
} finally {
fis.close();
}
您不应该硬编码数组大小。此外,您应该使用finally,以确保
FileInputStream
关闭,即使失败
下面是一个代码示例,演示了如何执行此操作:
FileInputStream fis;
String info = "";
try {
fis = mContext.openFileInput(this.fileName);
byte[] dataArray = new byte[fis.available()];
if (dataArray.length > 0) {
while (fis.read(dataArray) != -1) {
info = new String(dataArray);
}
Log.i("File Reading" , "Success!");
isOk = true;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
} finally {
fis.close();
}
您所做工作的安全版本是,例如:
private String readFromInternalStorage(String filename) {
FileInputStream fis = null;
File file = new File(startGame.getFilesDir(), filename);
long size = file.length();
// impossible to have more than that (= 2GB)
if (size > Integer.MAX_VALUE) {
Log.d("XXX", "File too big");
return null;
}
int iSize = (int) size;
try {
fis = new FileInputStream(file);
// part of Android since API level 1 - buffer can scale
ByteArrayBuffer bb = new ByteArrayBuffer(iSize);
// some rather small fixed buffer for actual reading
byte[] buffer = new byte[1024];
int read;
while ((read = fis.read(buffer)) != -1) {
// just append data as long as we can read more
bb.append(buffer, 0, read);
}
// return a new string based on the large buffer
return new String(bb.buffer(), 0, bb.length());
} catch (FileNotFoundException e) {
Log.w("XXX", e);
} catch (IOException e) {
Log.w("XXX", e);
} catch (OutOfMemoryError e) {
// this could be left out. Keep if you read several MB large files.
Log.w("XXX", e);
} finally {
// finally is executed even if you return in above code
// fis will be null if new FileInputStream(file) throws
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
// ignored, nothing can be done if closing fails
}
}
}
return null;
}
您所做工作的安全版本是,例如:
private String readFromInternalStorage(String filename) {
FileInputStream fis = null;
File file = new File(startGame.getFilesDir(), filename);
long size = file.length();
// impossible to have more than that (= 2GB)
if (size > Integer.MAX_VALUE) {
Log.d("XXX", "File too big");
return null;
}
int iSize = (int) size;
try {
fis = new FileInputStream(file);
// part of Android since API level 1 - buffer can scale
ByteArrayBuffer bb = new ByteArrayBuffer(iSize);
// some rather small fixed buffer for actual reading
byte[] buffer = new byte[1024];
int read;
while ((read = fis.read(buffer)) != -1) {
// just append data as long as we can read more
bb.append(buffer, 0, read);
}
// return a new string based on the large buffer
return new String(bb.buffer(), 0, bb.length());
} catch (FileNotFoundException e) {
Log.w("XXX", e);
} catch (IOException e) {
Log.w("XXX", e);
} catch (OutOfMemoryError e) {
// this could be left out. Keep if you read several MB large files.
Log.w("XXX", e);
} finally {
// finally is executed even if you return in above code
// fis will be null if new FileInputStream(file) throws
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
// ignored, nothing can be done if closing fails
}
}
}
return null;
}
强制关闭后,您在Logcat中读取了什么?强制关闭后,您在Logcat中读取了什么?您的代码有一些缺陷:a)如果
fis,您会覆盖信息。读取不会一次读取所有内容b)InputStream.available()
不可靠c)您必须检查fis!=null
在您的最后
中,因为如果mContext.openFileInput(this.fileName),它将是null
代码>引发异常(而不是将某些内容分配给fis
)您的代码有一些缺陷:a)如果fis.read
不能一次读取所有内容,则覆盖信息b)InputStream.available()
不可靠c)您必须检查fis!=null
在您的最后
中,因为如果mContext.openFileInput(this.fileName),它将是null
代码>引发异常(而不是将某些内容分配给fis
)