我在写个project~关于词典方面的,需要读入数据(数据大小约27M),然后我用bufferedReader将数据每行转成字符串存在String[]数组里~~~~当我调试程序的时候就出现了如题的错误。搜索了下相关知识,虽然可以调heap space初始大小,但终究不是解决问题的根本途径(别人电脑上的初始值还是没变啊~~)请问大家应该如何处理,我这样读入数据的方式怎样做可以更好一些??(因为为了即时性(速度),所以读入到String数组中,而不是每次查词都打开文件、读入文件再进行搜索。)
请大家赐教~~感激不尽~~~~~~~~~~~~~~~~~~~~~~~~~~下附出错的相关源代码:这是我用于读取文件的类public class Location {
private String[] s;
private int num_line = 0;
private int real_start = 0; //前面声明的行数
//(最终real_start对应的值即为第一行有用数据在s数组中的下标)
//初始化File中的每一行字符串,并存入s数组中
public Location(File f) throws IOException {
//统计行数
BufferedReader br = new BufferedReader(new FileReader(f));
while(br.readLine() != null) {
++num_line;
}
br.close();
//存入到s数组中
br = new BufferedReader(new FileReader(f));
s = new String[num_line+1];
int cnt = 0; // 当前处理的行
while((s[cnt] = br.readLine()) != null) {
if(s[cnt].startsWith(" ")) {
++real_start;
}
++cnt;
}
br.close();
}
在另一个类中连续调用Location从而读入数据(总计约27M)public void init() throws IOException{
index_noun = new Location(new File("./dict/index.noun"));
index_verb = new Location(new File("./dict/index.verb"));
index_adj = new Location(new File("./dict/index.adj"));
index_adv = new Location(new File("./dict/index.adv"));
data_noun = new Location(new File("./dict/data.noun"));
data_verb = new Location(new File("./dict/data.verb"));
data_adj = new Location(new File("./dict/data.adj"));
data_adv = new Location(new File("./dict/data.adv"));
lex_file = new Location(new File("./dict/lex_file.txt"));
}
请大家赐教~~感激不尽~~~~~~~~~~~~~~~~~~~~~~~~~~下附出错的相关源代码:这是我用于读取文件的类public class Location {
private String[] s;
private int num_line = 0;
private int real_start = 0; //前面声明的行数
//(最终real_start对应的值即为第一行有用数据在s数组中的下标)
//初始化File中的每一行字符串,并存入s数组中
public Location(File f) throws IOException {
//统计行数
BufferedReader br = new BufferedReader(new FileReader(f));
while(br.readLine() != null) {
++num_line;
}
br.close();
//存入到s数组中
br = new BufferedReader(new FileReader(f));
s = new String[num_line+1];
int cnt = 0; // 当前处理的行
while((s[cnt] = br.readLine()) != null) {
if(s[cnt].startsWith(" ")) {
++real_start;
}
++cnt;
}
br.close();
}
在另一个类中连续调用Location从而读入数据(总计约27M)public void init() throws IOException{
index_noun = new Location(new File("./dict/index.noun"));
index_verb = new Location(new File("./dict/index.verb"));
index_adj = new Location(new File("./dict/index.adj"));
index_adv = new Location(new File("./dict/index.adv"));
data_noun = new Location(new File("./dict/data.noun"));
data_verb = new Location(new File("./dict/data.verb"));
data_adj = new Location(new File("./dict/data.adj"));
data_adv = new Location(new File("./dict/data.adv"));
lex_file = new Location(new File("./dict/lex_file.txt"));
}
解决方案 »
- OpenSwing 怎么用?
- NoClassDefFoundError问题,奇怪!!!(java call matlab)
- 急求:用java能不能实现由字符数组复制给字符串?由于是初学者,在线等待
- String正则扣取指定的内容?
- selenium 怎么处理弹出验证输入用户名和密码?
- java 中设置窗口大小
- 有好一点的财会论坛吗?搜了半天也没到1个,能请教财务知识的那种,想转行了
- 在APPLET中如何实现对磁盘文件的读写,如有办法请给一个相关代码!请求帮助!一定给分!
- String类的indexof , lastindexof, substring, regionMatches, concat 各有什么功能?
- 请问java里面,什么叫做签名多态性方法?
- JavaSE小程序问题,求高手指点
- gbk编码的文字,包括数字,字母,汉字,特殊字符等,怎么转成utf-8编码?
建议不用String数组存
用List吧,应该不会存在OOM问题,同时不需要确定行数~
while((s = br.readLine()) != null) {
if(s.startsWith(" ")) {
++real_start;
}
++cnt;
}如果文件的内容需要保存到缓存中,那我建议不要用String[],而改用StringBuffer[]。因为new 一个String在缓存中是定长的,而StringBuffer则是变长的。用StringBuffer[]会节省缓存空间。
以上希望等对你有所帮助。
用StringBuffer怎么读~毕竟readline的返回值是String~是说将所有的字符串都改成Stringbuffer么?
StringBuffer strB = new StringBuffer();
strB.append("");
给你个建议:用XML来来读写和保存你的文件。