gpt4 book ai didi

java - 如何读取带有空格的文本文件 android java

转载 作者:行者123 更新时间:2023-12-01 18:23:27 32 4
gpt4 key购买 nike

当我知道我的文件包含时,如何将带有空格的文本文件读取到 int 数组中

"22 23 29 1 "

诸如此类

private String readFromFile() {

String ret = "";

try {
InputStream inputStream = openFileInput("mywords.txt");

if ( inputStream != null ) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String receiveString = "";
StringBuilder stringBuilder = new StringBuilder();

while ( (receiveString = bufferedReader.readLine()) != null ) {
stringBuilder.append(receiveString);
}

inputStream.close();
ret = stringBuilder.toString();
}
}
catch (FileNotFoundException e) {
Log.e("login activity", "File not found: " + e.toString());
} catch (IOException e) {
Log.e("login activity", "Can not read file: " + e.toString());
}

return ret;
}

到目前为止我一直用它来阅读

最佳答案

对于 "22 23 29 1 " 即带空格的整数,请尝试以下代码:

String ret="22 23 29 1 ";
ret=ret.trim();
String tok[]=ret.split(" ");
for(String s:tok) {
System.out.println(s);
}

输出:

22
23
29
1

如果您需要,可以使用 Integer.parseInt()string 解析为 integer

关于java - 如何读取带有空格的文本文件 android java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26989943/

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