【发布时间】:2015-01-31 14:03:51
【问题描述】:
我正在尝试读取文件并逐行拆分字符串行。这是文件中的示例字符串
Decorative Platters--------Home & Kitchen->Home & Décor->Home Décor Accents
Hookah--------Watches & Jewellery->Fashion Jewellery->Bangles
hookah--------
在这种情况下,第三行在点之后没有任何内容。
private static void getCategoriesFromFileAndMAtch() {
try {
BufferedReader br=new BufferedReader(new FileReader("mapping_log"));
String eachLine;
while((eachLine = br.readLine()) != null)
{
String input1, input2, tempString;
input1=eachLine.split("--------")[0];
tempString=eachLine.split("--------")[1];
if(!(eachLine.split("--------")[1].isEmpty()))
{
tempString=eachLine.split("--------")[1].split("::=>")[0];
System.out.println(input1+" "+tempString);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
因为 [1] 的值是空的,我得到了异常并且程序停止了。我怎样才能避免这种情况?我在 if 循环中检查它是否为空。这还不够吗?
【问题讨论】:
标签: java string split substring