我是Java的新手,我正在尝试编写一个简单的Android应用程序.我的应用程序的assets文件夹中有一个大约3500行的大文本文件,我需要将其读入字符串.我找到了一个关于如何做到这一点的一个很好的例子但是我有一个关于为什么字节数组被初始化为1024的问题.我不想将它初始化为我的文本文件的长度吗?另外,我不想使用char,不是byte吗?这是代码:
private void populateArray(){
AssetManager assetManager = getAssets();
InputStream inputStream = null;
try {
inputStream = assetManager.open("3500LineTextFile.txt");
} catch (IOException e) {
Log.e("IOException populateArray", e.getMessage());
}
String s = readTextFile(inputStream);
// Add more code here to populate array from string
}
private String readTextFile(InputStream inputStream) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
inputStream.length
byte buf[] = new byte[1024];
int len;
try {
while ((len = inputStream.read(buf)) != -1) {
outputStream.write(buf, 0, len);
}
outputStream.close();
inputStream.close();
} catch …Run Code Online (Sandbox Code Playgroud)