在android中读取文件
在以下代码中:在android中读取文件,android,file,android-file,Android,File,Android File,在以下代码中: fileInputStream = new FileInputStream(new File(pathToOurFile) ); bytesAvailable = fileInputStream.available(); bufferSize = Math.min(bytesAvailable, maxBufferSize); buffer = new byte[bufferSize]; bytesRead = fileInputStream.read(buffer, 0, bu
fileInputStream = new FileInputStream(new File(pathToOurFile) );
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
打印缓冲区是给一些随机值,而不是文件中的值buffersize
正在正确计算文件的大小。你能告诉我哪里出了问题吗?试试这个
private void ReadFile(AssetManager manager, String sourceFileName,
String destinationFileName) throws IOException {
// Read file from AccessManager
InputStream inputStream = manager.open(sourceFileName);
OutputStream outputStream = new FileOutputStream(destinationFileName);
Log.d("-->", "src: " + sourceFileName);
Log.d("-->", "Des: " + destinationFileName);
byte[] buffer = new byte[3072];
int length;
while ((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}
outputStream.flush();
outputStream.close();
inputStream.close();
outputStream = null;
inputStream = null;
}
可能是您读错了文件。请检查文件位置及其内容。请显示其余代码maxBufferSize,以及您实际如何打印缓冲区。其余代码很好,现在我尝试了另一种方法,它正在工作。