java读取文件时,空格或换行是各个字符串的分割符,怎么才能将字符串一一读出?

2025-01-07 01:40:41
推荐回答(1个)
回答1:

1、按行读,每个换行符是一行
2、读出行后按空格拆分字符串下面提供一个工具方法:
public static String[] split(String str, char separatorChar) {
if (str == null) {
return null;
}

int length = str.length();

if (length == 0) {
return ArrayUtil.EMPTY_STRING_ARRAY;
}

List list = new ArrayList();
int i = 0;
int start = 0;
boolean match = false;

while (i < length) {
if (str.charAt(i) == separatorChar) {
if (match) {
list.add(str.substring(start, i));
match = false;
}

start = ++i;
continue;
}

match = true;
i++;
}

if (match) {
list.add(str.substring(start, i));
}

return (String[]) list.toArray(new String[list.size()]);
}