亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

java出現(xiàn)StringIndexOutOfBoundsException錯(cuò)誤
天蓬老師
天蓬老師 2017-04-18 10:26:11
0
1
679

1.使用poi以及dom4j讀取一個(gè)有4000多條xml格式的excel如下:

需要取出具體幾個(gè)節(jié)點(diǎn)里面的值然后輸出到一個(gè)txt中,代碼運(yùn)行出現(xiàn)StringIndexOutOfBoundsException錯(cuò)誤。寫(xiě)出的txt中有幾百條正確數(shù)據(jù),代碼如下:

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);
        //根據(jù)指定的文件輸入流導(dǎo)入Excel從而產(chǎn)生Workbook對(duì)象
        Workbook wb0 = new XSSFWorkbook(fileIn);
        //獲取Excel文檔中的第一個(gè)表單
        Sheet sht0 = wb0.getSheetAt(0);
        //對(duì)Sheet中的每一行進(jìn)行迭代
        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.根據(jù)報(bào)錯(cuò)看出是角標(biāo)超長(zhǎng)了,但是一直沒(méi)找出原因,球大神們幫助

天蓬老師
天蓬老師

歡迎選擇我的課程,讓我們一起見(jiàn)證您的進(jìn)步~~

reply all(1)
PHPzhong

You didn’t say which line the error appeared on, so I just guessed it was here.
StringBuffer subs = new StringBuffer(";"+bufferCellValue.substring(firstStringLength + firstStringIndex, lastStringIndex));

When firstString is not found, -1 is returned. If firstString is "", the sum equals -1, and this error will be reported at this time.
There may be other situations, which I cannot test for the time being.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template