从文件中读取多行到单个字符串
Read multiple lines from file to a single string
我有一个文件,其中的行有特定的前缀。在某些情况下,某些类型的数据是多行的,例如在这个文件示例中:
Num: 10101
Name: File_8
Description: qwertz qwertz
qwertz qwertz ztrewq
Quantity: 2
未定义属性(编号、名称、描述、数量)的顺序。我使用以下代码从文件中读取数据并存储到数组中。
BufferedReader abc = new BufferedReader(new FileReader(file));
while ((strLine = abc.readLine()) != null) {
if(strLine.startsWith("Name:")){
data[0] = strLine.substring(strLine.indexOf(" ")+1);
data[0].trim();
}
}
前缀之间的字符串应该存储在一个字符串中。
Read multiple lines from file to a single string
如果将内容读入数组,可以使用join:
String.join(delimiter, elements);
例如。带分隔符 ,
和一个数组:
String str = String.join(",", new String[]{"1st line", "2nd line", "3rd line"});
产生输出:
1st line,2nd line,3rd line
或者直接读取到一个字符串:
// assume we have a function
byte[] encoded = Files.readAllBytes(Paths.get(path));
return new String(encoded, encoding);
要捕获映射:
String line, key = null, value = null;
while(scanner.hasNextLine()) {
line = scanner.nextLine();
if (line.contains(":")) {
if (key != null) {
values.put(key, value.trim());
}
int indexOfColon = line.indexOf(":");
key = line.substring(0, indexOfColon);
value = line.substring(indexOfColon + 1);
} else {
value += " " + line;
}
}
values.put(key, value.trim());
for (Map.Entry<String, String> mapEntry: values.entrySet()) {
System.out.println(mapEntry.getKey() + " -> '" + mapEntry.getValue() + "'");
}
打印:
Description -> 'qwertz qwertz qwertz qwertz ztrewq'
Num -> '10101'
Quantity -> '2'
Name -> 'File_8'
好的所以传递给 data[0] 的所有内容都应该连接成一个字符串?为什么不这样使用 StringBuilder class?
StringBuilder stringBuilder = new StringBuilder();
BufferedReader abc = new BufferedReader(new FileReader(file));
while ((strLine = abc.readLine()) != null) {
if(strLine.startsWith("Name:")){
data[0] = strLine.substring(strLine.indexOf(" ")+1);
data[0].trim();
stringBuilder.append(data[0]);
}
}
我有一个文件,其中的行有特定的前缀。在某些情况下,某些类型的数据是多行的,例如在这个文件示例中:
Num: 10101
Name: File_8
Description: qwertz qwertz
qwertz qwertz ztrewq
Quantity: 2
未定义属性(编号、名称、描述、数量)的顺序。我使用以下代码从文件中读取数据并存储到数组中。
BufferedReader abc = new BufferedReader(new FileReader(file));
while ((strLine = abc.readLine()) != null) {
if(strLine.startsWith("Name:")){
data[0] = strLine.substring(strLine.indexOf(" ")+1);
data[0].trim();
}
}
前缀之间的字符串应该存储在一个字符串中。
Read multiple lines from file to a single string
如果将内容读入数组,可以使用join:
String.join(delimiter, elements);
例如。带分隔符 ,
和一个数组:
String str = String.join(",", new String[]{"1st line", "2nd line", "3rd line"});
产生输出:
1st line,2nd line,3rd line
或者直接读取到一个字符串:
// assume we have a function
byte[] encoded = Files.readAllBytes(Paths.get(path));
return new String(encoded, encoding);
要捕获映射:
String line, key = null, value = null;
while(scanner.hasNextLine()) {
line = scanner.nextLine();
if (line.contains(":")) {
if (key != null) {
values.put(key, value.trim());
}
int indexOfColon = line.indexOf(":");
key = line.substring(0, indexOfColon);
value = line.substring(indexOfColon + 1);
} else {
value += " " + line;
}
}
values.put(key, value.trim());
for (Map.Entry<String, String> mapEntry: values.entrySet()) {
System.out.println(mapEntry.getKey() + " -> '" + mapEntry.getValue() + "'");
}
打印:
Description -> 'qwertz qwertz qwertz qwertz ztrewq'
Num -> '10101'
Quantity -> '2'
Name -> 'File_8'
好的所以传递给 data[0] 的所有内容都应该连接成一个字符串?为什么不这样使用 StringBuilder class?
StringBuilder stringBuilder = new StringBuilder();
BufferedReader abc = new BufferedReader(new FileReader(file));
while ((strLine = abc.readLine()) != null) {
if(strLine.startsWith("Name:")){
data[0] = strLine.substring(strLine.indexOf(" ")+1);
data[0].trim();
stringBuilder.append(data[0]);
}
}