我想在读取我正在使用的txt文件时将每行分割成两个单独的字符串,并稍后将它们存储在HashMap中。但现在我似乎无法正确读取文件。这是我的文件的一小部分看起来像:使用BufferedReader将行分割为两个字符串
....
CPI Clock Per Instruction
CPI Common Programming Interface [IBM]
.CPI Code Page Information (file name extension) [MS-DOS]
CPI-C Common Programming Interface for Communications [IBM]
CPIO Copy In and Out [Unix]
....
这是我的代码是什么样子:
try {
BufferedReader br = new BufferedReader(new FileReader("akronymer.txt"));
String line;
String akronym;
String betydning;
while((line = br.readLine()) != null) {
String[] linje = line.split("\\s+");
akronym = linje[0];
betydning = linje[1];
System.out.println(akronym + " || " + betydning);
}
} catch(Exception e) {
System.out.println("Feilen som ble fanget opp: " + e);
}
我要的是存储在一个字符串的缩写和定义另一个字符串
那么,什么阻止了你? –
@PeterLawrey我认为,OP的方式是将'whitespaces'上的字符串'分开'。 – Smit
@Smit对我来说看起来很好。 –