无法以特定格式读取 java 中的文件
Trouble reading a file in java in a certain format
我正在从文件中读取文本,但在尝试将 List 1
和 List 2
读取为 2 个不同的 String
时遇到了问题。 *
指示第一个列表结束的位置。我试过使用数组,但数组只存储最后一个 * 符号。
List 1
Name: Greg
Hobby 1: Swimming
Hobby 2: Football
*
List 2
Name: Bob
Hobby 1: Skydiving
*
这是我到目前为止尝试过的:
String s = "";
try{
Scanner scanner = new Scanner(new File("file.txt"));
while(scanner.hasnextLine()){
s = scanner.nextLine();
}
}catch(Exception e){
e.printStackTrace}
String [] array = s.split("*");
String x = array[0];
String y = array[1];
你的代码有多个问题,比如@Henry 说你的字符串只包含文件的最后一行,而且你误解了 split()
because it takes a RegularExpression 作为参数。
我建议您使用以下示例,因为它有效并且比您的方法快得多。
启动示例:
// create a buffered reader that reads from the file
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("test.txt")));
// create a new array to save the lists
ArrayList<String> lists = new ArrayList<>();
String list = ""; // initialize new empty list
String line; // initialize line variable
// read all lines until one becomes null (the end of the file)
while ((line = reader.readLine()) != null) {
// checks if the line only contains one *
if (line.matches("\s*\*\s*")) {
// add the list to the array of lists
lists.add(list);
} else {
// add the current line to the list
list += line + "\r\n"; // add the line to the list plus a new line
}
}
说明
难懂的特殊行我再解释一下
查看第一行:
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("test.txt")));
这一行创建的 BufferedReader
与 Scanner
几乎相同,但速度更快,并且没有 Scanner
那样多的方法。对于这种用法,BufferedReader
就足够了。
然后它在构造函数中接受一个 InputStreamReader
作为参数。这只是将下面的FileInputStream
转换成一个Reader
.
为什么要那样做?因为InputStream
≠Reader
。 InputStream
returns 原始值和 Reader
将其转换为人类可读的字符。参见 the difference between InputStream
and Reader
。
看下一行:
ArrayList<String> lists = new ArrayList<>();
创建一个变量数组,其中包含 add()
和 get(index)
等方法。参见 the difference of arrays and lists。
最后一个:
list += line + "\r\n";
此行将 line
添加到当前列表并向其添加新行。
"\r\n"
是特殊字符。 \r
结束当前行,\n
新建一行。
你也可以只使用\n
,但是在它前面加\r
会更好,因为它支持更多的Os,比如Linux可能会有问题当 \r
未命中时。
相关
Using BufferedReader to read Text File
我正在从文件中读取文本,但在尝试将 List 1
和 List 2
读取为 2 个不同的 String
时遇到了问题。 *
指示第一个列表结束的位置。我试过使用数组,但数组只存储最后一个 * 符号。
List 1
Name: Greg
Hobby 1: Swimming
Hobby 2: Football
*
List 2
Name: Bob
Hobby 1: Skydiving
*
这是我到目前为止尝试过的:
String s = "";
try{
Scanner scanner = new Scanner(new File("file.txt"));
while(scanner.hasnextLine()){
s = scanner.nextLine();
}
}catch(Exception e){
e.printStackTrace}
String [] array = s.split("*");
String x = array[0];
String y = array[1];
你的代码有多个问题,比如@Henry 说你的字符串只包含文件的最后一行,而且你误解了 split()
because it takes a RegularExpression 作为参数。
我建议您使用以下示例,因为它有效并且比您的方法快得多。
启动示例:
// create a buffered reader that reads from the file
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("test.txt")));
// create a new array to save the lists
ArrayList<String> lists = new ArrayList<>();
String list = ""; // initialize new empty list
String line; // initialize line variable
// read all lines until one becomes null (the end of the file)
while ((line = reader.readLine()) != null) {
// checks if the line only contains one *
if (line.matches("\s*\*\s*")) {
// add the list to the array of lists
lists.add(list);
} else {
// add the current line to the list
list += line + "\r\n"; // add the line to the list plus a new line
}
}
说明
难懂的特殊行我再解释一下
查看第一行:
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("test.txt")));
这一行创建的 BufferedReader
与 Scanner
几乎相同,但速度更快,并且没有 Scanner
那样多的方法。对于这种用法,BufferedReader
就足够了。
然后它在构造函数中接受一个 InputStreamReader
作为参数。这只是将下面的FileInputStream
转换成一个Reader
.
为什么要那样做?因为InputStream
≠Reader
。 InputStream
returns 原始值和 Reader
将其转换为人类可读的字符。参见 the difference between InputStream
and Reader
。
看下一行:
ArrayList<String> lists = new ArrayList<>();
创建一个变量数组,其中包含 add()
和 get(index)
等方法。参见 the difference of arrays and lists。
最后一个:
list += line + "\r\n";
此行将 line
添加到当前列表并向其添加新行。
"\r\n"
是特殊字符。 \r
结束当前行,\n
新建一行。
你也可以只使用\n
,但是在它前面加\r
会更好,因为它支持更多的Os,比如Linux可能会有问题当 \r
未命中时。
相关
Using BufferedReader to read Text File