简体   繁体   中英

File IO Exceptions in Java

I am reading input from a tab delimited file in a java class. The file opens properly and the information from the file seems to be read in correctly as well. Every line in the file winds up printing to the screen as expected but then at the end of file it seems like it tries to print one more line and I get an ArrayIndexOutOfBoundsException: 1.

It is worth noting that if I uncomment the line where I output the value of sCurrentline and comment out the output of the split array I do not get the error.

Code:

BufferedReader br = null;

try {
        String sCurrentLine;

        br = new BufferedReader(new FileReader(fname));

        while ((sCurrentLine = br.readLine()) != null){

            String[] values = sCurrentLine.split("\\t", -1); // don't truncate empty fields

            System.out.println("Col1: " + values[0] + " Col2: " + values[1] + " Col3: " 
            + values[2] + " Col4: " + values[3] + " Col5: " + values[4] );

            //System.out.println(sCurrentLine);

        }
} catch (IOException e) {
    System.out.println("IOException");
    e.printStackTrace();
} finally {
    try {
        if(br != null){
            br.close();
        }
    } catch (IOException ex) {
        System.out.println("ErrorClosingFile");
        ex.printStackTrace();
    }
}

The last line has not the same amount of elements as the other lines. After splitting the last line, you try to access fields of the array, that do not exist. That is indicated by the exception http://docs.oracle.com/javase/7/docs/api/java/lang/ArrayIndexOutOfBoundsException.html . Before you access the fields of the array, you have to check if there is the expected amount of items in it. Like this:

BufferedReader br = null;

try {
    String sCurrentLine;
    br = new BufferedReader(new FileReader(fname));

    while ((sCurrentLine = br.readLine()) != null){
        String[] values = sCurrentLine.split("\\t", -1); // don't truncate empty fields

        if (5 == values.length) {
            System.out.println("Col1: " + values[0] + " Col2: " + values[1] + " Col3: " 
            + values[2] + " Col4: " + values[3] + " Col5: " + values[4] );
        }

        // System.out.println(sCurrentLine);
    }
} catch (IOException e) {
    System.out.println("IOException");
    e.printStackTrace();
} finally {
    try {
        if(br != null){
            br.close();
        }
    } catch (IOException ex) {
        System.out.println("ErrorClosingFile");
        ex.printStackTrace();
    }
}

the code seems ok... do you have an empty newline at the end?

while ((sCurrentLine = br.readLine()) != null){
    if (sCurrentLine.isEmpty() || sCurrentLine.startsWith(";")) // skip empty and comment lines
        continue;

    String[] values = sCurrentLine.split("\\t"); // are you sure the -1 is required?
...
}

Try this

    String[] values = "".split("\\t", -1); // don't truncate empty fields
    int index=1;
    StringBuffer sb = new StringBuffer();
    for (String value : values) {
         sb.append("Col"+index+":").append(value).append(" ");
        index++;
    }
    System.out.println(sb.toString());

Is obvious that you are reading an array position that does not exist

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM