0

我正在尝试进行外部合并排序。方法:打开“输出”文件夹中的所有文件,并获取第一行并对其进行排序,并将其写入“最终”文件,然后获取该文件的第二行并重复。我得到一个 StackOverflowError。这里我的文件大小大于内存。

 public class mergefile6 {
    public static ArrayList<String> al = new ArrayList<String>();
    static HashMap hm = new HashMap();
    public static String line;
    public static String[][] filepoint = new String[100][2];
    public static int fileline=1;  
    public static int i=0;  

    public static void main(String[] args) throws Exception{
        fileread();
    }
    public static void fileread() throws Exception{     
        FileReader fileReader = null;
        BufferedReader bufferedReader = null;       
    try {
                File folder = new File("./output/");        
                if (folder.isDirectory()) {
                    for (File file : folder.listFiles()) {
                            fileReader = new FileReader(file);
                            bufferedReader = new BufferedReader(fileReader);        
                            int lineCount = 0;
                        while ((line = bufferedReader.readLine())!=null) {
                                lineCount++;
                                if (1 == lineCount) {
                                    hm.put(line,file);
                                    al.add(line);
                                    filepoint[i][0]=file.toString();
                                filepoint[i][1]=Integer.toString(fileline);
                                    ++i;
                                }                                               
                            }               
                    }
                }
                if (null != fileReader){
                    try {
                            fileReader.close();
                    } catch (IOException e) {
                            e.printStackTrace();
                    }
        }
        if (null != bufferedReader){
                    try {
                            bufferedReader.close();
                    } catch (IOException e) {
                            e.printStackTrace();
                    }
        }
        Sorting(al);
        test(al);
    } catch (Exception e) {
    } finally { 
    }
  }
  public static void Sorting(ArrayList<String> al)throws Exception{
    int length = al.size();
    ArrayList<String> tmp = new ArrayList<String>(al);
    mergeSort(al, tmp,  0,  al.size() - 1);
  }
  private static void mergeSort(ArrayList<String> al, ArrayList<String> tmp, int left, int right){
    //sort code
  } 

public static void test(ArrayList<String> al) throws Exception{
    BufferedWriter bw = null;
    FileWriter fw = null;
        fw = new FileWriter("final",true);
        bw = new BufferedWriter(fw);
        bw.write(al.get(0)+" \n");
        //bw.flush();
        bw.close();
        fw.close();
        String filename = hm.get(al.get(0)).toString();
        hm.remove(al.get(0));
        al.remove(0);
        fileforward(filename,al);
}
public static void fileforward(String filename,ArrayList<String> al) throws Exception{
    long list;
    FileReader fr = null;
    BufferedReader br = null;
    fr = new FileReader(filename);
    br = new BufferedReader(fr);
    for(int j=0;j<i;++j){
        if(filepoint[j][0] == filename){
            fileline = Integer.parseInt(filepoint[j][1]); 
            list = br.skip(99*fileline);
            if((line = br.readLine())!=null){
                hm.put(line,filename);
                al.add(line);
                ++fileline;
                filepoint[j][1]=Integer.toString(fileline);
                br.close(); fr.close();
            }else{}             
        }
    }
    if(al.size()==3){
    Sorting(al);
    test(al);       }                               
}   
 }

什么可能导致此错误出现?

4

1 回答 1

1

fileforward()可能是和相互调用造成的溢出test()。我不知道尝试使用日志或打印来调试 ArrayList 的大小。如果它总是等于 3,那就是问题所在。

于 2016-03-26T20:29:22.000 回答