请大家帮我改进一下java性能问题。高手快进来看看啊! 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); } 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 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 包下的类逐个熟悉一遍。 谢谢楼上两位。我已经将代码优化了,现在将两个方法合成一个了。但是还有没有办法再提高一点速度呢! /** * 将固定长度文件中的一行数据按照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; } 建议楼主将入参传入的rule的类型为List,如果传入的为ArrayList性能估计会好一些还有,由于你的lines使用了大量的add操作,建议使用LinkedList 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; }小改了一下,你可以试试提高了多少 一个关于Object类的问题。。 急!HtmlParser提取文本的问题,求教 byte i=-1;可以,但i=0xff;去不可以,int i=0xffffffff又可以,为什么? 客户端程序中的class文件可以封装起来吗? 在线等急 java能不能像vc一样能够在函数中修改参数值? java Properties问题 对象怎样序列化呀!!急急!! 求一段GBK to UTF-8的代码 小错误,请指点!!!急!!! 无法读值的简单问题,请帮忙 关于Java数组数据项保存地点的疑惑
throws UnsupportedEncodingException {
return new String(Arrays.copyOfRange(src, start, end), encode);
}
楼主对 JDK 提供的类库不够熟悉,建议先将 java.util 包下的类逐个熟悉一遍。
/**
* 将固定长度文件中的一行数据按照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;
}
还有,由于你的lines使用了大量的add操作,建议使用LinkedList
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;
}
小改了一下,你可以试试提高了多少