文本Java:读取txt文件并将其保存在字符串数组中,但不带反斜杠(空格)?
我用这段代码逐行读取一个txt文件
// Open the file that is the first command line parameter
FileInputStream fstream = new FileInputStream("/Users/dimitramicha/Desktop/SweetHome3D1.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
// Read File Line By Line
int i = 0;
while ((strLine = br.readLine()) != null) {
str[i] = strLine;
i++;
}
// Close the input stream
in.close();
我将它保存在一个数组中
之后,我想对保存在数组中的字符串做一个if语句。但是当我这样做的时候,它就不起作用了,因为(正如我所想)它也节省了空间(反斜杠)。如果没有数组中的数据,我该如何保存
# 1 楼答案
请查看
String
中的replace方法,并在将其放入数组之前在strLine
上调用它# 2 楼答案
我会:
如果发现其他不需要的字符,还可以进行更多替换
# 3 楼答案
您可以使用Scanner,默认情况下,它使用空白分隔令牌。看看this教程