java出现StringIndexOutOfBoundsException错误

查看:146
本文介绍了java出现StringIndexOutOfBoundsException错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

1.使用poi以及dom4j读取一个有4000多条xml格式的excel如下:

需要取出具体几个节点里面的值然后输出到一个txt中,代码运行出现StringIndexOutOfBoundsException错误。写出的txt中有几百条正确数据,代码如下:

2.代码

public class Main {

    public static void main(String[] args) throws ParserConfigurationException, SAXException, DocumentException, IOException {

        String sr[]={"<ns2:uniqueKey>","<ns3:tmId>","<ns3:status>","<ns3:volume>","<ns3:weight>","<ns3:orderId>"};
        String sr2[]={"</ns2:uniqueKey>","</ns3:tmId>","</ns3:status>","</ns3:volume>","</ns3:weight>","</ns3:orderId>"};
        String filePath = "C:\\Users\\sun.song\\Desktop\\test.xlsx";
        DaiPoi.loadScoreInfo(filePath,sr,sr2);

    }
}

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.dom4j.*;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import java.io.*;
import java.util.ArrayList;
import java.util.Formatter;
import java.util.Iterator;
import java.util.List;

/**
 * Created by sun.song on 2016/12/7.
 */
public class DaiPoi {
    public static void loadScoreInfo(String xlsPath,String[] first,String[] last) throws IOException, ParserConfigurationException, SAXException, DocumentException {
        FileWriter writer=null;
        FileInputStream fileIn = new FileInputStream(xlsPath);
        //根据指定的文件输入流导入Excel从而产生Workbook对象
        Workbook wb0 = new XSSFWorkbook(fileIn);
        //获取Excel文档中的第一个表单
        Sheet sht0 = wb0.getSheetAt(0);
        //对Sheet中的每一行进行迭代
        for (Row r : sht0) {
            for (Cell c : r) {
                if (first.length == last.length) {
                    String sub = handleString(c, first, last);
                    writer = new FileWriter("d:/2.txt", true);
                    //System.out.println("添加了一条");
                    writer.write(sub);
                    writer.close();
                }
                //System.out.println("111");
                writer = new FileWriter("d:/2.txt", true);
                writer.write("\n");
                writer.close();
            }
            fileIn.close();
        }
    }
    public static String handleString(Cell c,String[] first,String[] last) {
         StringBuffer sb= new StringBuffer("");
        for (int i = 0; i < first.length; i++) {
            String firstString = first[i];
            //System.out.println(firstString);
            String lastString = last[i];
            int firstStringLength = firstString.length();
            String cellValue = c.getStringCellValue();
            StringBuffer bufferCellValue = new StringBuffer(cellValue);
            // System.out.println(cellValue);
            int firstStringIndex = cellValue.indexOf(firstString);
            int lastStringIndex = cellValue.indexOf(lastString);
            StringBuffer subs = new StringBuffer(";"+bufferCellValue.substring(firstStringLength + firstStringIndex, lastStringIndex));
            sb.append(subs);

        }

        return sb.toString();
    }
}

3.根据报错看出是角标超长了,但是一直没找出原因,球大神们帮助

解决方案

你没说错误出现在哪一行,我就猜出现在这里。
StringBuffer subs = new StringBuffer(";"+bufferCellValue.substring(firstStringLength + firstStringIndex, lastStringIndex));

当firstString没有找到的时候,返回的是-1,如果firstString为""的话,相加等于-1,这个时候会报这个错误。
可能还有其他情况,我暂时没办法测试。

这篇关于java出现StringIndexOutOfBoundsException错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆