gpt4 book ai didi

java - android从文件中读取

转载 作者:太空宇宙 更新时间:2023-11-03 13:30:21 27 4
gpt4 key购买 nike

我是 android 的新手,在看书时无法理解循环中新分配的原因。在循环之前做一次还不够吗?

        FileInputStream fIn =
openFileInput("textfile.txt");
InputStreamReader isr = new
InputStreamReader(fIn);
char[] inputBuffer = new char[READ_BLOCK_SIZE];
String s = "";
int charRead;
while ((charRead = isr.read(inputBuffer))>0)
{
//---convert the chars to a String---
String readString =
String.copyValueOf(inputBuffer, 0,
charRead);
s += readString;
inputBuffer = new char[READ_BLOCK_SIZE];
}

最佳答案

来自 String.copyValueOf javadoc :

  /**
* Creates a new string containing the specified characters in the character
* array. Modifying the character array after creating the string has no
* effect on the string.
*
* @param start
* the starting offset in the character array.
* @param length
* the number of characters to use.

因此没有理由在循环中创建一个新的 char[]。

关于java - android从文件中读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14219175/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com