为什么初始化这个字节数组1024 [英] why initialize this byte array to 1024
问题描述
我是比较新的Java和我试图写一个简单的Android应用程序。我有一个在我的应用程序的文件夹资产约3500行的大的文本文件,我需要读入一个字符串。我发现如何做到这一点的好例子,但我有一个关于为什么字节数组初始化为1024。难道我想将它初始化到我的文本文件的长度问题?此外,也不会我想用字符
,不是字节
?这里是code:
私人无效populateArray(){
AssetManager assetManager = getAssets();
的InputStream的InputStream = NULL;
尝试 {
的InputStream = assetManager.open(3500LineTextFile.txt);
}赶上(IOException异常E){
Log.e(IOException异常populateArray,e.getMessage());
}
字符串s = readTextFile(InputStream的);
//添加更多code这里来填充从字符串数组
}
私人字符串readTextFile(InputStream中的InputStream){
ByteArrayOutputStream的OutputStream =新ByteArrayOutputStream();
inputStream.length
中byte buf [] =新的字节[1024];
INT LEN;
尝试 {
而((LEN = inputStream.read(BUF))!= - 1){
outputStream.write(BUF,0,的len);
}
outputStream.close();
inputStream.close();
}赶上(IOException异常E){
Log.e(IOException异常readTextFile,e.getMessage());
}
返回outputStream.toString();
}
编辑:根据你的建议,我尝试这种方法。是不是更好吗?谢谢你。
私人无效populateArray(){
AssetManager assetManager = getAssets();
的InputStream的InputStream = NULL;
读者iStreamReader = NULL;
尝试 {
的InputStream = assetManager.open(LIST.TXT);
iStreamReader =新的InputStreamReader(InputStream中,UTF-8);
}赶上(IOException异常E){
Log.e(IOException异常populateArray,e.getMessage());
}
字符串字符串= readTextFile(iStreamReader);
//更多code在这里
}
私人字符串readTextFile(InputStreamReader的InputStreamReader的){
StringBuilder的SB =新的StringBuilder();
烧焦BUF [] =新的char [2048];
INT读取;
尝试 {
做 {
读= inputStreamReader.read(BUF,0,buf.length);
如果(读> 0){
sb.append(BUF,0,读);
}
}而(读> = 0);
}赶上(IOException异常E){
Log.e(IOException异常readTextFile,e.getMessage());
}
返回sb.toString();
}
这个例子是不好的。这是十足的不良做法(隐藏例外,不关闭流在finally块,不指定显式编码等)。它采用的是1024字节长缓冲器,因为它没有任何办法知道输入流的长度的
阅读的Java IO教程了解如何从阅读的文本文件。
I'm relatively new to Java and I'm attempting to write a simple android app. I have a large text file with about 3500 lines in the assets folder of my applications and I need to read it into a string. I found a good example about how to do this but I have a question about why the byte array is initialized to 1024. Wouldn't I want to initialize it to the length of my text file? Also, wouldn't I want to use char
, not byte
? Here is the code:
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 (IOException e) {
Log.e("IOException readTextFile", e.getMessage());
}
return outputStream.toString();
}
EDIT: Based on your suggestions, I tried this approach. Is it any better? Thanks.
private void populateArray(){
AssetManager assetManager = getAssets();
InputStream inputStream = null;
Reader iStreamReader = null;
try {
inputStream = assetManager.open("List.txt");
iStreamReader = new InputStreamReader(inputStream, "UTF-8");
} catch (IOException e) {
Log.e("IOException populateArray", e.getMessage());
}
String String = readTextFile(iStreamReader);
// more code here
}
private String readTextFile(InputStreamReader inputStreamReader) {
StringBuilder sb = new StringBuilder();
char buf[] = new char[2048];
int read;
try {
do {
read = inputStreamReader.read(buf, 0, buf.length);
if (read>0) {
sb.append(buf, 0, read);
}
} while (read>=0);
} catch (IOException e) {
Log.e("IOException readTextFile", e.getMessage());
}
return sb.toString();
}
This example is not good at all. It's full of bad practices (hiding exceptions, not closing streams in finally blocks, not specify an explicit encoding, etc.). It uses a 1024 bytes long buffer because it doesn't have any way of knowing the length of the input stream.
Read the Java IO tutorial to learn how to read text from a file.
这篇关于为什么初始化这个字节数组1024的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!