StringIndexOutOfBoundsException错误

时间:2014-04-16 13:52:06

标签: java file parsing file-io

我想解析一个数字文件。我实现了解析,但是当我更改文件内容时,我得到一个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 

1 个答案:

答案 0 :(得分:0)

这里唯一可以抛出IndexOutOfBoundsException的是

s.charAt(0)

这意味着在第一个位置的这一行中没有任何东西=空行。

在循环开始时尝试以下操作:

if(s == null || s.trim().length() == 0)
{
    System.out.println("Empty line found.");
    continue;
}