[英]StringIndexOutOfBoundsException Error
我想解析一個數字文件。 我實現了解析,但是當我更改文件內容時,我得到一個 StringIndexOutOfBoundsException。 任何幫助將不勝感激。
我的代碼如下:
public void fileParse() throws IOException
{
File f = new File("180214.txt");
File f1 = new File("salelog.txt");
String s = null;
FileWriter fw = new FileWriter(f1);
PrintWriter pw = new PrintWriter(fw);
BufferedReader br = new BufferedReader(new FileReader(f));
while((s=br.readLine())!=null)
{
if(s.charAt(0) == '0')
{
pw.println("<receipt>");
pw.println(s);
continue;
}
if(s.charAt(0)=='8')
{
pw.println(s);
pw.println("</receipt>");
continue;
}
pw.println(s);
}
// br.close();
pw.close();
br1.close();
}
}
我的文件內容是這樣的:
001021402180000000000000000
4869359340051000000011001000000100000000000
58693593400510800000000008000 0
600000001100100
800300000001070002075700000001100000000008
001021402180000000000000000
4869697119156000000003001000000100000000000
58696971191560800000000002000 0
600000000300100
800500000001060002080000000000300000000002
001021402180000000000000000
4869069850477000000010001500000200000000000
58690698504770800000000015000 0
4000008691497000000012001500000100000000000
50000086914970800000000009000 0
4000004082260000000008001000000100000000000
50000040822600800000000006000 0
600000004000100
800500000001060003080100000004000000000030
001021402180000000000000000
4000008090548000000028501400000100000000000
50000080905480800000000021000 0
600000002850100
800500000001060004080900000002850000000021
001021402180000000000000000
4869088520157000000049501200000100000000000
58690885201571800000000076000 0
600000004950100
800500000001060005080900000004950000000076
001021402180000000000000000
4869749799821000000019501400000100000000000
58697497998210800000000014000 0
4869076767108000000049501500000100000000650
58690767671080800000000032000 0
986907676710801$0000000065000001
600000006250100
800500000001060006081200000006250000000046
001021402180000000000000000
4977214623561000000007503200000100000000000
59772146235610000000000000000 0
600000000750100
800500000001060007081300000000750000000000
這里唯一可以拋出IndexOutOfBoundsException
是
s.charAt(0)
代碼。
這意味着在第一個位置的這一行中,沒有任何 = 空行。
在循環開始時嘗試以下操作:
if(s == null || s.trim().length() == 0)
{
System.out.println("Empty line found.");
continue;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.