try {
             startIndex = 0;
            for (int i = 1; i < rule.size(); i++) {
                startIndex += (Integer) rule.get(i - 1);
                endIndex += (Integer) rule.get(i);
                String item = subByte(byteline, startIndex, endIndex, enCode);
                lines.add(item);
            }

解决方案 »

  1.   

    public static String subByte(byte[] src, int start, int end, String encode)
       throws UnsupportedEncodingException {
            return new String(Arrays.copyOfRange(src, start, end), encode);
    }
    楼主对 JDK 提供的类库不够熟悉,建议先将 java.util 包下的类逐个熟悉一遍。
      

  2.   

    谢谢楼上两位。我已经将代码优化了,现在将两个方法合成一个了。但是还有没有办法再提高一点速度呢!
     /**
         * 将固定长度文件中的一行数据按照rule装入List
         * 
         * @param line
         *            数据行
         * @param rule
         *            数据读取规则
         * @param enCode
         *            字符集
         * @return 装入数据的list
         * @throws IOException
         *             IO例外
         * @throws SystemException
         *             系统例外
         */
        public static List<String> readFixLenRecord(String line, List rule, String enCode) throws IOException,
                SystemException {
            List<String> lines = new ArrayList<String>();
            int startIndex = 0;
            int endIndex = 0;
            byte[] byteline = line.getBytes(enCode);        try {            for (int i = 0; i < rule.size(); i++) {
                    if (startIndex + (Integer) rule.get(i) <= line.length()) {
                        String item = new String(byteline, startIndex, (Integer) rule.get(i), enCode);
                        lines.add(item);
                        startIndex += (Integer) rule.get(i);
                    }else{
                        String item = new String(byteline, startIndex, byteline.length-startIndex, enCode);
                        lines.add(item);
                        break;
                    }
                }        } catch (Exception e) {
                throw new SystemException("XX3BC-L0-04000", new Object[] { "ファイルを読み込む時" }, e);
            }
            return lines;
        }
      

  3.   

    建议楼主将入参传入的rule的类型为List,如果传入的为ArrayList性能估计会好一些
    还有,由于你的lines使用了大量的add操作,建议使用LinkedList
      

  4.   

    public static List<String> readFixLenRecord(String line, List rule,
    String enCode) throws IOException, SystemException {
    List<String> lines = new ArrayList<String>();
    int startIndex = 0;
    byte[] byteline = line.getBytes(enCode);


    for (int i = 0, size = rule.size(), length = line.length(); i < size; i++) {
    int j = (Integer) rule.get(i);
    if (startIndex +  j <= length) {
    String item = new String(byteline, startIndex,
    j, enCode);
    lines.add(item);
    startIndex += j;
    } else {
    String item = new String(byteline, startIndex,
    byteline.length - startIndex, enCode);
    lines.add(item);
    break;
    }
    } return lines;
    }
    小改了一下,你可以试试提高了多少