我有一个1000行的文本文件,格式如下:
19 x 75 Bullnose Architrave/Skirting £1.02
Run Code Online (Sandbox Code Playgroud)
我正在编写一个逐行读取文件的方法 - 这个工作正常.
然后,我想使用"£"作为分隔符拆分每个字符串,并按ArrayList<String>以下格式写出:
19 x 75 Bullnose Architrave/Skirting, Metre, 1.02
Run Code Online (Sandbox Code Playgroud)
这就是我接近它的方式(productList是ArrayList在try块之外声明/实例化):
try{
br = new BufferedReader(new FileReader(aFile));
String inputLine = br.readLine();
String delim = "£";
while (inputLine != null){
String[]halved = inputLine.split(delim, 2);
String lineOut = halved[0] + ", Metre, " + halved[1];//Array out of bounds
productList.add(lineOut);
inputLine = br.readLine();
}
}
Run Code Online (Sandbox Code Playgroud)
字符串没有分裂,我一直在得到ArrayIndexOutOfBoundsException.我对正则表达式不太熟悉.我也试过使用旧的StringTokenizer但得到相同的结果.
£作为一个delim 存在问题还是其他问题?我确实想知道这是否与第二个令牌没有被读作是有关系的String?
任何想法都会有所帮助.