我正在使用 BufferedReader 逐行读取文本文件。然后我使用一种方法来规范化每一行文本。但是我的规范化方法有问题,在调用它之后,BufferedReader 对象停止读取文件。有人可以帮我弄这个吗。
这是我的代码:
public static void main(String[] args) {
String string = "";
try (BufferedReader br = new BufferedReader(new FileReader("file.txt"))) {
String line;
while ((line = br.readLine()) != null) {
string += normalize(line);
}
} catch (Exception e) {
}
System.out.println(string);
}
public static String normalize(String string) {
StringBuilder text = new StringBuilder(string.trim());
for(int i = 0; i < text.length(); i++) {
if(text.charAt(i) == ' ') {
removeWhiteSpaces(i + 1, text);
}
}
if(text.charAt(text.length() - 1) != '.') {
text.append('.');
}
text.append("\n");
return text.toString();
}
public static void removeWhiteSpaces(int index, StringBuilder text) {
int j = index;
while(text.charAt(j) == ' ') {
text.deleteCharAt(j);
}
}
这是我使用的文本文件:
abc .
asd.
dasd.
哔哔one
人到中年有点甜
慕虎7371278
慕码人8056858
尚方宝剑之说
幕布斯7119047
随时随地看视频慕课网APP
相关分类