加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

JAVA遇到大批数据处理时会出现Java heap space的报错的解决方案

(2012-09-26 21:00:15)
标签:

java

heap

space

it

分类: Code

JAVA遇到大批数据处理时会出现Java heap space的报错的解决方案

 

Java heap space一直是困扰我们的一个问题。像Matlab就可以一次性读取5000*5000的数据到一个矩阵matrix中。然而Java确不行。

我遇到实验室处理一个“合并5000左右txt文档到一个txt文件中”的问题,相同的算法用Matlab就不会出现内存不足的问题,而JAVA则会报出:java.lang.OutOfMemoryError: Java heap space。从这里可以判断的说Matlab是为处理大型数据而设计(It is conceived for),所以其执行代码时对内存启动了自动的分配与管理。因为我的5000左右txt文档大约有180M,显而易见,同时读到内存肯定会出现内存空间不足的警告,所以也不怪JAVA。当然,你可以在运行JAVA代码之前提高内存分配值,但是这实际上不能从根本上解决问题。因为哪天万一还有更大的数据遇到时,你是不是还要继续增大内存?要知道现在一般电脑的内存也就2G,就算1G的也很普遍。

所以根本上我觉得是要改变你的算法。一个很自然的思路就是:数据大了就分配处理嘛。

所以我最后就是利用了递归的手段,将5000左右txt文档分批调入内存进行处理,处理一部分就释放一部分资源。这样不管是5000个数据文件还是7000,9000都没问题。试验测得我的情况是如果不进行分批处理而是一次性调入内存在合并文档,那最大文件数只能是2000,就是超过2000个数据文件就会出现java.lang.OutOfMemoryError: Java heap space这样的错误。

Matlab的缺点就是速度慢。所以处理海里数据为求速度还是可以试一下用JAVA实现的。我的实验结果表明,速度相差几十倍。

下面将一步一步介绍对这个同一问题不同的编程版本以及其结果:

1. 以我实验室的频谱测量仪(spectrometer)对激光所获的离散采样为背景。

本程序是属于数据处理程序,用JAVA所实现。

spectrometer中光栅的每次采样所获得的数据被保存在一个文本文件中, 分为2列:第一列为波长值,第二列为对应的光强值。 第1次采样文本文件被命名为“000000.txt”,第二个为“000001.txt”,依次类推。 比如若让spectrometer采样3mins,大概会出现4000次采样, 被保存的文件就为000000.txt~003999.txt

此程序使用的是二维LinkedList类型来拉取数据。 LinkedList:就是采用链表结构保存对象。 PS:JAVA中集合类分为List,Set,和Map。 List又有LinkedList和ArrayList; Set分为HashSet和TreeSet; Map分为HashMap和TreeMap。 顾名思义,Hash就是由哈希表提供支持,Tree树结构提供了排序和顺序输出的功能。

=>下载本程序所用到的存放离散采样数据文件的文件夹

 

  1. package com.han;  
  2.   
  3. import java.io.*;  
  4. import java.util.*;  
  5.   
  6.   
  7. public class CombinedTextSpectres  
  8.     static final int N=6;//N是保存的文件名称的长度   
  9.     static final int M=4520;//M是保存的文件数目   
  10.     @SuppressWarnings({ "unchecked""rawtypes" })  
  11.     public static void main(String[] args)  
  12.         // TODO Auto-generated method stub   
  13.         long startTime=System.currentTimeMillis();  
  14.         try  
  15.             //若是当前class类文件目录则用下面的getClass().getRessource()代码   
  16.             //File new_file=new File(new File(new test().getClass().getResource("").toURI()),"combinedFile.txt");          
  17.             File new_file=new File("/home/han/Desktop","combinedFile.txt");//定义合并后的文件的保存路径   
  18.             FileWriter fw new FileWriter(new_file);  
  19.             BufferedWriter bfw=new BufferedWriter(fw);  
  20.             List row=new LinkedList();  
  21.             int num_line 0 
  22.             for(int i=0;i
  23.                 List column=new LinkedList();  
  24.                   
  25.                 String filename="00"+i;//"00"是保存的文件的前缀     
  26.                 int temLength=filename.length();  
  27.                 for(int j=0;j
  28.                     filename="0"+filename;  
  29.                  
  30.                 filename=filename+".txt" 
  31.                   
  32.                 //定义存放离散采样数据文件的文件夹   
  33.                 File file=new File("/home/han/Ubuntu One/apresmidi sans pola",filename);  
  34.                 //File file=new File(new File(new test().getClass().getResource("").toURI()),filename);   
  35.                 FileReader fr=new FileReader(file);  
  36.                 BufferedReader bfr=new BufferedReader(fr);  
  37.   
  38.                 String s=null 
  39.                 while((s=bfr.readLine())!=null){  
  40.                     s=s.replace(',''.');  
  41.                     if(i==0){  
  42.                         column.add(s);  
  43.                     }else 
  44.                         String[] sArray=s.split("\t");  
  45.                         column.add(sArray[1]);  
  46.                      
  47.                  
  48.                 row.add(column);  
  49.                 num_line=column.size();  
  50.                 bfr.close();  
  51.                 fr.close();   
  52.              
  53.             System.out.println("Files are all viewed");  
  54.             for(int i=0;i
  55.                 Iterator it=row.iterator();  
  56.                 while(it.hasNext()){  
  57.                     List tempList=(List)it.next();  
  58.                     bfw.write((String)tempList.get(i));  
  59.                     bfw.write("\t");  
  60.                  
  61.                   
  62.                 bfw.newLine();  
  63.              
  64.             bfw.close();  
  65.             fw.close();  
  66.             System.out.println("OK");  
  67.         catch (Exception e)  
  68.             // TODO Auto-generated catch block   
  69.             e.printStackTrace();  
  70.          
  71.         long endTime=System.currentTimeMillis();  
  72.         System.out.println("耗费时间: "+(endTime-startTime)+ms");  
  73.      
  74.         
package com.han;

import java.io.*;
import java.util.*;


public class CombinedTextSpectres {
        static final int N=6;//N是保存的文件名称的长度
        static final int M=4520;//M是保存的文件数目
        @SuppressWarnings({ "unchecked", "rawtypes" })
        public static void main(String[] args) {
                // TODO Auto-generated method stub
                long startTime=System.currentTimeMillis();
                try {
                        //若是当前class类文件目录则用下面的getClass().getRessource()代码
                        //File new_file=new File(new File(new test().getClass().getResource("").toURI()),"combinedFile.txt");           
                        File new_file=new File("/home/han/Desktop","combinedFile.txt");//定义合并后的文件的保存路径
                        FileWriter fw = new FileWriter(new_file);
                        BufferedWriter bfw=new BufferedWriter(fw);
                        List row=new LinkedList();
                        int num_line = 0;
                        for(int i=0;i

运行结果:Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.ArrayList.subList(ArrayList.java:915)
at java.lang.String.split(String.java:2359)
at java.lang.String.split(String.java:2403)

at com.han.CombinedTextSpectres.main(CombinedTextSpectres.java:64)


2. 若减少要合并的离散数据文件个数,上面的程序是可以成功运行的。为方便读者,还是把数据源和代码分别列出如下:

=>下载本程序所用到的存放离散采样数据文件的文件夹

  1. package com.han;  
  2.   
  3. import java.io.*;  
  4. import java.util.*;  
  5.   
  6.   
  7. public class CombinedTextSpectres  
  8.     static final int N=6;//N是保存的文件名称的长度   
  9.     static final int M=357;//M是保存的文件数目   
  10.     @SuppressWarnings({ "unchecked""rawtypes" })  
  11.     public static void main(String[] args)  
  12.         // TODO Auto-generated method stub   
  13.         long startTime=System.currentTimeMillis();  
  14.         try  
  15.             //若是当前class类文件目录则用下面的getClass().getRessource()代码   
  16.             //File new_file=new File(new File(new test().getClass().getResource("").toURI()),"combinedFile.txt");          
  17.             File new_file=new File("/home/han/Desktop","combinedFile.txt");//定义合并后的文件的保存路径   
  18.             FileWriter fw new FileWriter(new_file);  
  19.             BufferedWriter bfw=new BufferedWriter(fw);  
  20.             List row=new LinkedList();  
  21.             int num_line 0 
  22.             for(int i=0;i
  23.                 List column=new LinkedList();  
  24.                   
  25.                 String filename="00"+i;//"00"是保存的文件的前缀     
  26.                 int temLength=filename.length();  
  27.                 for(int j=0;j
  28.                     filename="0"+filename;  
  29.                  
  30.                 filename=filename+".txt" 
  31.                   
  32.                 //定义存放离散采样数据文件的文件夹   
  33.                 File file=new File("/home/han/Ubuntu One/spectre sans pola",filename);  
  34.                 //File file=new File(new File(new test().getClass().getResource("").toURI()),filename);   
  35.                 FileReader fr=new FileReader(file);  
  36.                 BufferedReader bfr=new BufferedReader(fr);  
  37.   
  38.                 String s=null 
  39.                 while((s=bfr.readLine())!=null){  
  40.                     s=s.replace(',''.');  
  41.                     if(i==0){  
  42.                         column.add(s);  
  43.                     }else 
  44.                         String[] sArray=s.split("\t");  
  45.                         column.add(sArray[1]);  
  46.                      
  47.                  
  48.                 row.add(column);  
  49.                 num_line=column.size();  
  50.                 bfr.close();  
  51.                 fr.close();   
  52.              
  53.             System.out.println("Files are all viewed");  
  54.             for(int i=0;i
  55.                 Iterator it=row.iterator();  
  56.                 while(it.hasNext()){  
  57.                     List tempList=(List)it.next();  
  58.                     bfw.write((String)tempList.get(i));  
  59.                     bfw.write("\t");  
  60.                  
  61.                   
  62.                 bfw.newLine();  
  63.              
  64.             bfw.close();  
  65.             fw.close();  
  66.             System.out.println("OK");  
  67.         catch (Exception e)  
  68.             // TODO Auto-generated catch block   
  69.             e.printStackTrace();  
  70.          
  71.         long endTime=System.currentTimeMillis();  
  72.         System.out.println("耗费时间: "+(endTime-startTime)+ms");  
  73.      
  74.         
package com.han;

import java.io.*;
import java.util.*;


public class CombinedTextSpectres {
        static final int N=6;//N是保存的文件名称的长度
        static final int M=357;//M是保存的文件数目
        @SuppressWarnings({ "unchecked", "rawtypes" })
        public static void main(String[] args) {
                // TODO Auto-generated method stub
                long startTime=System.currentTimeMillis();
                try {
                        //若是当前class类文件目录则用下面的getClass().getRessource()代码
                        //File new_file=new File(new File(new test().getClass().getResource("").toURI()),"combinedFile.txt");           
                        File new_file=new File("/home/han/Desktop","combinedFile.txt");//定义合并后的文件的保存路径
                        FileWriter fw = new FileWriter(new_file);
                        BufferedWriter bfw=new BufferedWriter(fw);
                        List row=new LinkedList();
                        int num_line = 0;
                        for(int i=0;i

运行结果:Files are all viewed
OK
耗费时间: 14880 ms

附: 运行结果即合并后的单个文件


3. 下面的方法一样的使用二维LinkedList类型来读取数据。 但是在重新写入到单个合并文件时使用了StringBuilder, 经测试,效率和CombinedTextSpectres(第一版)差不多。

但是依然对于大数据如超过2000个文件要合并时则出现内存泄漏。

=>下载本程序所用到的存放离散采样数据文件的文件夹

  1. package com.han;  
  2.   
  3. import java.io.*;  
  4. import java.util.*;  
  5.   
  6.   
  7. public class CombinedTextSpectres_2  
  8.     static final int N=6;//N是保存的文件名称的长度   
  9.     static final int M=357;//M是保存的文件数目   
  10.     @SuppressWarnings({ "unchecked""rawtypes" })  
  11.     public static void main(String[] args)  
  12.         // TODO Auto-generated method stub   
  13.         long startTime=System.currentTimeMillis();  
  14.         try  
  15.             //若是当前class类文件目录则用下面的getClass().getRessource()代码   
  16.             //File new_file=new File(new File(new test().getClass().getResource("").toURI()),"combinedFile.txt");          
  17.             File new_file=new File("/home/han/Desktop","combinedFile.txt");  
  18.             FileWriter fw new FileWriter(new_file);  
  19.             BufferedWriter bfw=new BufferedWriter(fw);  
  20.             List row=new LinkedList();  
  21.             int num_line 0 
  22.             for(int i=0;i
  23.                 List column=new LinkedList();  
  24.                   
  25.                 String filename="00"+i;//"00"是保存的文件的前缀     
  26.                 int temLength=filename.length();  
  27.                 for(int j=0;j
  28.                     filename="0"+filename;  
  29.                  
  30.                 filename=filename+".txt" 
  31.                 //定义存放离散采样数据文件的文件夹   
  32.                 File file=new File("/media/96D0265ED0264539/Users/HAN/Documents/Thèse ISMO/DonneesLabo/10_10-14_10/spectre sans pola",filename);  
  33.                 //File file=new File(new File(new test().getClass().getResource("").toURI()),filename);   
  34.                 FileReader fr=new FileReader(file);  
  35.                 BufferedReader bfr=new BufferedReader(fr);  
  36.   
  37.                 String s=null 
  38.                 while((s=bfr.readLine())!=null){  
  39.                     s=s.replace(',''.');  
  40.                     if(i==0){  
  41.                         column.add(s);  
  42.                     }else 
  43.                         String[] sArray=s.split("\t");  
  44.                         column.add(sArray[1]);  
  45.                      
  46.                  
  47.                 row.add(column);  
  48.                 num_line=column.size();  
  49.                 bfr.close();  
  50.                 fr.close();   
  51.              
  52.             System.out.println("Files are all viewed");  
  53.             StringBuilder sb=new StringBuilder("");   
  54.             for(int i=0;i
  55.                 Iterator it=row.iterator();  
  56.                 while(it.hasNext()){  
  57.                     List tempList=(List)it.next();  
  58.                     sb.append((String)tempList.get(i));  
  59.                     sb.append("\t");  
  60.                  
  61.                 //以下这种应用.size()的遍历方法在大循环的时候效率要略低于上面的.iterator()方法   
  62.                   
  63.                 sb.append("\n");  
  64.              
  65.             bfw.write(sb.toString());  
  66.             bfw.close();  
  67.             fw.close();  
  68.             System.out.println("OK");  
  69.         catch (Exception e)  
  70.             // TODO Auto-generated catch block   
  71.             e.printStackTrace();  
  72.          
  73.         long endTime=System.currentTimeMillis();  
  74.         System.out.println("耗费时间: "+(endTime-startTime)+ms");  
  75.      
  76.         
package com.han;

import java.io.*;
import java.util.*;


public class CombinedTextSpectres_2 {
        static final int N=6;//N是保存的文件名称的长度
        static final int M=357;//M是保存的文件数目
        @SuppressWarnings({ "unchecked", "rawtypes" })
        public static void main(String[] args) {
                // TODO Auto-generated method stub
                long startTime=System.currentTimeMillis();
                try {
                        //若是当前class类文件目录则用下面的getClass().getRessource()代码
                        //File new_file=new File(new File(new test().getClass().getResource("").toURI()),"combinedFile.txt");           
                        File new_file=new File("/home/han/Desktop","combinedFile.txt");
                        FileWriter fw = new FileWriter(new_file);
                        BufferedWriter bfw=new BufferedWriter(fw);
                        List row=new LinkedList();
                        int num_line = 0;
                        for(int i=0;i


运行结果: Files are all viewed

OK
耗费时间: 15155 ms

附: 运行结果即合并后的单个文件


4. 下面的方法一样的使用经典固定长度数组,以及在重新写入到单个合并文件时使用了StringBuilder (其实因为BufferedWriter也是缓存,所以有无SringBuilder效率一样)。 经测试,效率比CombinedTextSpectres(第一版) 和CombinedTextSpectres_2(第二版)都要高出10倍!。

但是依然对于大数据如超过2000个文件要合并时则出现内存泄漏。

=>下载本程序所用到的存放离散采样数据文件的文件夹

 

  1. package com.han;  
  2.   
  3. import java.io.*;  
  4.   
  5. public class CombinedTextSpectres_3  
  6.     //此方法使用自动探测单个文件的行数(即spectrometre的采样点数),无SringBuilder    
  7.     static final int N=6;//N是保存的文件名称的长度   
  8.       
  9.     static final int M=357;//M是保存的文件数目    
  10.       
  11.     public static void main(String[] args)  
  12.         // TODO Auto-generated method stub   
  13.         long startTime=System.currentTimeMillis();  
  14.         try  
  15.             //定义存放离散采样数据文件的文件夹   
  16.             String dirPath="/media/96D0265ED0264539/Users/HAN/Documents/Thèse ISMO/DonneesLabo/10_10-14_10/spectre sans pola" 
  17.             File file_temp=new File(dirPath,"000000.txt");  
  18.             FileReader fr_temp=new FileReader(file_temp);  
  19.             BufferedReader bfr_temp=new BufferedReader(fr_temp);  
  20.             int NLine 0;//自动探测单个文件的行数(即spectrometre的采样点数)   
  21.             @SuppressWarnings("unused" 
  22.             String s_temp=null 
  23.             while((s_temp=bfr_temp.readLine())!=null){  
  24.                 NLine++;  
  25.              
  26.             bfr_temp.close();  
  27.             fr_temp.close();      
  28.             String[][] CombinedArray=new String[NLine][M+1];  
  29.               
  30.             File new_file=new File("/home/han/Desktop","combinedFile.txt");  
  31.             //若是当前class类文件目录则用下面的getClass().getRessource()代码   
  32.             //File new_file=new File(new File(new test().getClass().getResource("").toURI()),"combinedFile.txt");          
  33.             FileWriter fw new FileWriter(new_file);  
  34.             BufferedWriter bfw=new BufferedWriter(fw);  
  35.             for(int i=0;i
  36.                 String filename="00"+i;//"00"是保存的文件的前缀     
  37.                 int temLength=filename.length();  
  38.                 for(int j=0;j
  39.                     filename="0"+filename;  
  40.                  
  41.                 filename=filename+".txt" 
  42.                   
  43.                 File file=new File(dirPath,filename);  
  44.                 //File file=new File(new File(new test().getClass().getResource("").toURI()),filename);   
  45.                 FileReader fr=new FileReader(file);  
  46.                 BufferedReader bfr=new BufferedReader(fr);  
  47.   
  48.                 String s=null 
  49.                 int num_line 0 
  50.                 while((s=bfr.readLine())!=null){  
  51.                     s=s.replace(',''.');  
  52.                     String[] sArray=s.split("\t");  
  53.                     if(i==0){  
  54.                         CombinedArray[num_line][0]=sArray[0];  
  55.                         CombinedArray[num_line][1]=sArray[1];  
  56.                     }else 
  57.                         CombinedArray[num_line][i+1]=sArray[1];  
  58.                         
  59.                     num_line++;  
  60.                  
  61.                 bfr.close();  
  62.                 fr.close();   
  63.              
  64.             System.out.println("Files are all viewed");  
  65.             for(int i=0;i
  66.                 for(int j=0;j1;j++){  
  67.                     bfw.write(CombinedArray[i][j]);  
  68.                     bfw.write("\t");  
  69.                  
  70.                 bfw.newLine();  
  71.              
  72.             bfw.close();  
  73.             fw.close();  
  74.             System.out.println("OK");  
  75.         catch (Exception e)  
  76.             // TODO Auto-generated catch block   
  77.             e.printStackTrace();  
  78.          
  79.         long endTime=System.currentTimeMillis();  
  80.         System.out.println("耗费时间: "+(endTime-startTime)+ms");  
  81.      
  82.         
package com.han;

import java.io.*;

public class CombinedTextSpectres_3 {
        //此方法使用自动探测单个文件的行数(即spectrometre的采样点数),无SringBuilder 
        static final int N=6;//N是保存的文件名称的长度
        
        static final int M=357;//M是保存的文件数目 
        
        public static void main(String[] args) {
                // TODO Auto-generated method stub
                long startTime=System.currentTimeMillis();
                try {
                        //定义存放离散采样数据文件的文件夹
                        String dirPath="/media/96D0265ED0264539/Users/HAN/Documents/Thèse ISMO/DonneesLabo/10_10-14_10/spectre sans pola";
                        File file_temp=new File(dirPath,"000000.txt");
                        FileReader fr_temp=new FileReader(file_temp);
                        BufferedReader bfr_temp=new BufferedReader(fr_temp);
                        int NLine = 0;//自动探测单个文件的行数(即spectrometre的采样点数)
                        @SuppressWarnings("unused")
                        String s_temp=null;
                        while((s_temp=bfr_temp.readLine())!=null){
                                NLine++;
                        }
                        bfr_temp.close();
                        fr_temp.close();        
                        String[][] CombinedArray=new String[NLine][M+1];
                        
                        File new_file=new File("/home/han/Desktop","combinedFile.txt");
                        //若是当前class类文件目录则用下面的getClass().getRessource()代码
                        //File new_file=new File(new File(new test().getClass().getResource("").toURI()),"combinedFile.txt");           
                        FileWriter fw = new FileWriter(new_file);
                        BufferedWriter bfw=new BufferedWriter(fw);
                        for(int i=0;i

运行结果: Files are all viewed
OK
耗费时间: 1802 ms

附: 运行结果即合并后的单个文件


5. 利用了递归的手段,将5000左右txt文档分批调入内存进行处理, 处理一部分就释放一部分资源。这样不管是5000个数据文件还是7000,9000都没问题。 试验测得我的情况是如果不进行分批处理而是一次性调入内存在合并文档的话, 那最大文件数只能是2000,就是超过2000个数据文件就会出现 java.lang.OutOfMemoryError: Java heap space这样的错误。

代码中level变量就是每次分批处理的数目,本程序设为1000。

此方法使用自动探测单个文件的行数(即spectrometre的采样点数), 因为BufferedWriter也是缓存,所以有无SringBuilder效率差不多, 本程序因此写入到单个合并文件时没有使用StringBuilder。 但是写的时候也向系统申请了String[][] CombinedArray=new String[2048][Column]; 其中Column为批处理次数,比如3或4。由此可见虽然载入的还是“整个数据”, 但申请的String二维数组只有String[2048][4],系统处理速度和内存占用却大大改善!

=>下载本程序所用到的存放离散采样数据文件的文件夹

 

  1. package com.han;  
  2.   
  3. import java.io.*;  
  4.   
  5.   
  6. public class CombinedTextSpectres_4  
  7.     static int N=6;//N是保存的文件名称的长度   
  8.     static int M=4520;//M是保存的文件数目    
  9.     int level=1000;//每次分批处理的数目   
  10.     int filenumBegin;//每批处理时的第一个文件名   
  11.     int filenumEnd;//每批处理时的最后一个文件名   
  12.     String dirPath="/media/96D0265ED0264539/Users/HAN/Documents/Thèse ISMO/DonneesLabo/17_10-21_10/apresmidi sans pola";//定义存放离散采样数据文件的文件夹   
  13.     static File tempFile; //定义临时文件夹来存放每批合并后的结果文件   
  14.     static String destDirPath="/home/han/Desktop";//定义存放最终结果文件的文件夹   
  15.       
  16.     public CombinedTextSpectres_4(){  
  17.         try  
  18.             combine(M,0);  
  19.         catch (IOException e)  
  20.             // TODO Auto-generated catch block   
  21.             e.printStackTrace();  
  22.          
  23.      
  24.   
  25.       
  26.     void combine(int num, int loop) throws IOException{  
  27.         if(num>level){     
  28.             //如果剩余的文件数还是大于设定的level=1000的话,则继续往下递归   
  29.             combine(num-level,loop+1);  
  30.          
  31.         filenumBegin=loop*level;  
  32.         if(num<=level){  
  33.             filenumEnd=filenumBegin+num;  
  34.         }else 
  35.             filenumEnd=filenumBegin+level;  
  36.          
  37.           
  38.         //定义经典固定长度数组,因为比集合等动态数组效率要高。   
  39.         //并且此时也可以保证String[][] CombinedArray不会   
  40.         //超出内存,因为每次处理的不会超过level=1000   
  41.         String[][] CombinedArray=new String[2048][filenumEnd-filenumBegin];  
  42.           
  43.         for(int i=filenumBegin;i
  44.             String filename="00"+i;//"00"是保存的文件的前缀     
  45.             int temLength=filename.length();  
  46.             for(int j=0;j
  47.                 filename="0"+filename;  
  48.              
  49.             filename=filename+".txt" 
  50.             File file=new File(dirPath,filename);  
  51.             FileReader fr=new FileReader(file);  
  52.             BufferedReader bfr=new BufferedReader(fr);  
  53.             int num_line 0 
  54.             String s=null 
  55.             while((s=bfr.readLine())!=null){  
  56.                 if(i==0){  
  57.                     CombinedArray[num_line][0]=s.replace(',''.');  
  58.                 }else 
  59.                     String[] sArray=s.split("\t");  
  60.                     CombinedArray[num_line][i-filenumBegin]=sArray[1].replace(',''.');  
  61.                  
  62.                 num_line++;  
  63.              
  64.             bfr.close();  
  65.             fr.close();   
  66.          
  67.         //System.out.println("Files are all viewed");   
  68.           
  69.           
  70.         String filename=loop+".txt" 
  71.         tempFile=new File(dirPath,"temp");  
  72.         if(!tempFile.exists()){  
  73.             tempFile.mkdir();  
  74.          
  75.         File new_file=new File(tempFile,filename);  
  76.         FileWriter fw new FileWriter(new_file);  
  77.         BufferedWriter bfw=new BufferedWriter(fw);  
  78.         for(int i=0;i<</SPAN>2048;i++){  
  79.             for(int j=0;j
  80.                 bfw.write(CombinedArray[i][j]);  
  81.                 bfw.write("\t");  
  82.              
  83.             bfw.newLine();  
  84.          
  85.         CombinedArray=null;//让JAVA垃圾自动回收装置认为此CombinedArray变量可以回收   
  86.         bfw.close();  
  87.         fw.close();  
  88.         System.out.println(new_file);  
  89.         System.out.println("OK");  
  90.      
  91.     public static void main(String[] args)  
  92.         // TODO Auto-generated method stub   
  93.         long startTime=System.currentTimeMillis();  
  94.         new CombinedTextSpectres_4();  
  95.           
  96.         File[] FileArray=tempFile.listFiles();  
  97.         int Column=FileArray.length;  
  98.         String[][] CombinedArray=new String[2048][Column];  
  99.         try  
  100.             for(int i=0;i
  101.                 FileReader fr=new FileReader(FileArray[i]);  
  102.                 BufferedReader bfr=new BufferedReader(fr);  
  103.                 int num_line 0 
  104.                 String s=null   
  105.                 while((s=bfr.readLine())!=null){  
  106.                     CombinedArray[num_line][i]=s;  
  107.                     num_line++;  
  108.                  
  109.                 bfr.close();  
  110.                 fr.close();   
  111.              
  112.             File new_file=new File(destDirPath,"CombinedSpectres.txt");  
  113.             FileWriter fw new FileWriter(new_file);  
  114.             BufferedWriter bfw=new BufferedWriter(fw);  
  115.               
  116.             String ExperienceCondition="Integrated time is ms, with the HR Spectrometer, with polarizorH, using the syringe Rh6G+Glycol and the other is Huile" 
  117.             bfw.write(ExperienceCondition);  
  118.             bfw.newLine();  
  119.               
  120.             bfw.write("Wavelength (nm)\t");  
  121.             for(int i=0;i
  122.                 String filename="00"+i;//"00"是保存的文件的前缀     
  123.                 int temLength=filename.length();  
  124.                 for(int j=0;j
  125.                     filename="0"+filename;  
  126.                  
  127.                 filename=filename+".txt" 
  128.                 bfw.write(filename);  
  129.                 bfw.write("\t");  
  130.              
  131.             bfw.newLine();  
  132.               
  133.             for(int i=0;i<</SPAN>2048;i++){  
  134.                 for(int j=0;j
  135.                     bfw.write(CombinedArray[i][j]);  
  136.                     //bfw.write("\t");   
  137.                  
  138.                 bfw.newLine();  
  139.              
  140.             CombinedArray=null 
  141.             bfw.close();  
  142.             fw.close();  
  143.             System.out.println(new_file);  
  144.             System.out.println("OK");  
  145.         catch (Exception e)  
  146.             // TODO Auto-generated catch block   
  147.             e.printStackTrace();  
  148.          
  149.         long endTime=System.currentTimeMillis();  
  150.         System.out.println("耗费时间: "+(endTime-startTime)+ms");  
  151.      
  152.         
package com.han;

import java.io.*;


public class CombinedTextSpectres_4 {
        static int N=6;//N是保存的文件名称的长度
        static int M=4520;//M是保存的文件数目 
        int level=1000;//每次分批处理的数目
        int filenumBegin;//每批处理时的第一个文件名
        int filenumEnd;//每批处理时的最后一个文件名
        String dirPath="/media/96D0265ED0264539/Users/HAN/Documents/Thèse ISMO/DonneesLabo/17_10-21_10/apresmidi sans pola";//定义存放离散采样数据文件的文件夹
        static File tempFile; //定义临时文件夹来存放每批合并后的结果文件
        static String destDirPath="/home/han/Desktop";//定义存放最终结果文件的文件夹
        
        public CombinedTextSpectres_4(){
                try {
                        combine(M,0);
                } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
        }

        
        void combine(int num, int loop) throws IOException{
                if(num>level){  
                        //如果剩余的文件数还是大于设定的level=1000的话,则继续往下递归
                        combine(num-level,loop+1);
                }
                filenumBegin=loop*level;
                if(num<=level){
                        filenumEnd=filenumBegin+num;
                }else{
                        filenumEnd=filenumBegin+level;
                }
                
                //定义经典固定长度数组,因为比集合等动态数组效率要高。
                //并且此时也可以保证String[][] CombinedArray不会
                //超出内存,因为每次处理的不会超过level=1000
                String[][] CombinedArray=new String[2048][filenumEnd-filenumBegin];
                
                for(int i=filenumBegin;i<2048;i++){
                        for(int j=0;j<2048;i++){
                                for(int j=0;j
运行结果:/media/96D0265ED0264539/Users/HAN/Documents/Thèse ISMO/DonneesLabo/17_10-21_10/apresmidi sans pola/temp/4.txt
OK
/media/96D0265ED0264539/Users/HAN/Documents/Thèse ISMO/DonneesLabo/17_10-21_10/apresmidi sans pola/temp/3.txt
OK
/media/96D0265ED0264539/Users/HAN/Documents/Thèse ISMO/DonneesLabo/17_10-21_10/apresmidi sans pola/temp/2.txt
OK
/media/96D0265ED0264539/Users/HAN/Documents/Thèse ISMO/DonneesLabo/17_10-21_10/apresmidi sans pola/temp/1.txt
OK
/media/96D0265ED0264539/Users/HAN/Documents/Thèse ISMO/DonneesLabo/17_10-21_10/apresmidi sans pola/temp/0.txt
OK
/home/han/Desktop/CombinedSpectres.txt
OK
耗费时间: 30842 ms

附: 运行结果即合并后的单个文件

6. 但是考虑到上个程序最后写的时候也向系统申请了 String[][] CombinedArray=new String[2048][Column]; 其中Column为批处理次数,比如3或4。由此可见载入的还是“整个数据”, 虽然试验证明了系统处理速度和内存占用大大改善了,是可行的。但是, 在本程序中还是又采用了另一种方法:同时开通4个输入流分别指向4个临时文件, 写入最后的单个结果文件。效果证明也非常不错!

=>下载本程序所用到的存放离散采样数据文件的文件夹

 

  1. package com.han;  
  2.   
  3. import java.io.*;  
  4.   
  5.   
  6. public class CombinedTextSpectres_5  
  7.     static int N=6;//N是保存的文件名称的长度   
  8.     static int M=4520;//M是保存的文件数目    
  9.     int level=1000;//每次分批处理的数目   
  10.     int filenumBegin;//每批处理时的第一个文件名   
  11.     int filenumEnd;//每批处理时的最后一个文件名   
  12.     String dirPath="/media/96D0265ED0264539/Users/HAN/Documents/Thèse ISMO/DonneesLabo/17_10-21_10/apresmidi sans pola";//定义存放离散采样数据文件的文件夹   
  13.     static File tempFile; //定义临时文件夹来存放每批合并后的结果文件   
  14.     static String destDirPath="/home/han/Desktop";//定义存放最终结果文件的文件夹   
  15.       
  16.     public CombinedTextSpectres_5(){  
  17.         try  
  18.             combine(M,0);  
  19.         catch (IOException e)  
  20.             // TODO Auto-generated catch block   
  21.             e.printStackTrace();  
  22.          
  23.      
  24.   
  25.       
  26.     void combine(int num, int loop) throws IOException{  
  27.         if(num>level){     
  28.             //如果剩余的文件数还是大于设定的level=1000的话,则继续往下递归   
  29.             combine(num-level,loop+1);  
  30.          
  31.         filenumBegin=loop*level;  
  32.         if(num<=level){  
  33.             filenumEnd=filenumBegin+num;  
  34.         }else 
  35.             filenumEnd=filenumBegin+level;  
  36.          
  37.   
  38.         //定义经典固定长度数组,因为比集合等动态数组效率要高。   
  39.         //并且此时也可以保证String[][] CombinedArray不会   
  40.         //超出内存,因为每次处理的不会超过level=1000   
  41.         String[][] CombinedArray=new String[2048][filenumEnd-filenumBegin];  
  42.   
  43.         for(int i=filenumBegin;i
  44.             String filename="00"+i;//"00"是保存的文件的前缀     
  45.             int temLength=filename.length();  
  46.             for(int j=0;j
  47.                 filename="0"+filename;  
  48.              
  49.             filename=filename+".txt" 
  50.             File file=new File(dirPath,filename);  
  51.             FileReader fr=new FileReader(file);  
  52.             BufferedReader bfr=new BufferedReader(fr);  
  53.             int num_line 0 
  54.             String s=null 
  55.             while((s=bfr.readLine())!=null){  
  56.                 if(i==0){  
  57.                     CombinedArray[num_line][0]=s.replace(',''.');  
  58.                 }else 
  59.                     String[] sArray=s.split("\t");  
  60.                     CombinedArray[num_line][i-filenumBegin]=sArray[1].replace(',''.');  
  61.                  
  62.                 num_line++;  
  63.              
  64.             bfr.close();  
  65.             fr.close();   
  66.          
  67.         //System.out.println("Files are all viewed");   
  68.   
  69.         //建立临时文件夹存放每批合并后的结果文件   
  70.         String filename=loop+".txt" 
  71.         tempFile=new File(dirPath,"temp");  
  72.         if(!tempFile.exists()){  
  73.             tempFile.mkdir();  
  74.          
  75.         File new_file=new File(tempFile,filename);  
  76.         FileWriter fw new FileWriter(new_file);  
  77.         BufferedWriter bfw=new BufferedWriter(fw);  
  78.         for(int i=0;i<</SPAN>2048;i++){  
  79.             for(int j=0;j
  80.                 bfw.write(CombinedArray[i][j]);  
  81.                 bfw.write("\t");  
  82.              
  83.             bfw.newLine();  
  84.          
  85.         CombinedArray=null;//让JAVA垃圾自动回收装置认为此CombinedArray变量可以回收   
  86.         bfw.close();  
  87.         fw.close();  
  88.         System.out.println(new_file);  
  89.         System.out.println("OK");  
  90.      
  91.   
  92.     public static void main(String[] args)  
  93.         // TODO Auto-generated method stub   
  94.         long startTime=System.currentTimeMillis();  
  95.         new CombinedTextSpectres_5();  
  96.         try 
  97.             File new_file=new File(destDirPath,"CombinedSpectres.txt");  
  98.             FileWriter fw new FileWriter(new_file);  
  99.             BufferedWriter bfw=new BufferedWriter(fw);  
  100.               
  101.             String ExperienceCondition="Integrated time is ms, with the HR Spectrometer, with polarizorH, using the syringe Rh6G+Glycol and the other is Huile" 
  102.             bfw.write(ExperienceCondition);  
  103.             bfw.newLine();  
  104.               
  105.             bfw.write("Wavelength (nm)\t");  
  106.             for(int i=0;i
  107.                 String filename="00"+i;//"00"是保存的文件的前缀     
  108.                 int temLength=filename.length();  
  109.                 for(int j=0;j
  110.                     filename="0"+filename;  
  111.                  
  112.                 filename=filename+".txt" 
  113.                 bfw.write(filename);  
  114.                 bfw.write("\t");  
  115.              
  116.             bfw.newLine();  
  117.               
  118.             //再把中间临时文件都合并成最终的单个文件   
  119.             File[] FileArray=tempFile.listFiles();  
  120.             int Column=FileArray.length;  
  121.             FileReader[] fr=new FileReader[Column];  
  122.             BufferedReader[] bfr=new BufferedReader[Column];  
  123.               
  124.             for(int i=0;i
  125.                 fr[i]=new FileReader(FileArray[i]);  
  126.                 bfr[i]=new BufferedReader(fr[i]);  
  127.              
  128.             String s;  
  129.             while((s=bfr[0].readLine())!=null){  
  130.                 for(int i=0;i
  131.                     if(i==0){  
  132.                         bfw.write(s);  
  133.                     }else 
  134.                         bfw.write(bfr[i].readLine());  
  135.                      
  136.                  
  137.                 bfw.newLine();  
  138.              
  139.             for(int i=0;i
  140.                 bfr[i].close();  
  141.                 fr[i].close();            
  142.                 
  143.             bfw.close();  
  144.             fw.close();  
  145.             System.out.println(new_file);  
  146.             System.out.println("OK");  
  147.         catch (Exception e)  
  148.             // TODO Auto-generated catch block   
  149.             e.printStackTrace();  
  150.          
  151.         long endTime=System.currentTimeMillis();  
  152.         System.out.println("耗费时间: "+(endTime-startTime)+ms");  
  153.      
  154.         
package com.han;

import java.io.*;


public class CombinedTextSpectres_5 {
        static int N=6;//N是保存的文件名称的长度
        static int M=4520;//M是保存的文件数目 
        int level=1000;//每次分批处理的数目
        int filenumBegin;//每批处理时的第一个文件名
        int filenumEnd;//每批处理时的最后一个文件名
        String dirPath="/media/96D0265ED0264539/Users/HAN/Documents/Thèse ISMO/DonneesLabo/17_10-21_10/apresmidi sans pola";//定义存放离散采样数据文件的文件夹
        static File tempFile; //定义临时文件夹来存放每批合并后的结果文件
        static String destDirPath="/home/han/Desktop";//定义存放最终结果文件的文件夹
        
        public CombinedTextSpectres_5(){
                try {
                        combine(M,0);
                } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
        }

        
        void combine(int num, int loop) throws IOException{
                if(num>level){  
                        //如果剩余的文件数还是大于设定的level=1000的话,则继续往下递归
                        combine(num-level,loop+1);
                }
                filenumBegin=loop*level;
                if(num<=level){
                        filenumEnd=filenumBegin+num;
                }else{
                        filenumEnd=filenumBegin+level;
                }

                //定义经典固定长度数组,因为比集合等动态数组效率要高。
                //并且此时也可以保证String[][] CombinedArray不会
                //超出内存,因为每次处理的不会超过level=1000
                String[][] CombinedArray=new String[2048][filenumEnd-filenumBegin];

                for(int i=filenumBegin;i<2048;i++){
                        for(int j=0;j

运行结果: /media/96D0265ED0264539/Users/HAN/Documents/Thèse ISMO/DonneesLabo/17_10-21_10/apresmidi sans pola/temp/4.txt
OK
/media/96D0265ED0264539/Users/HAN/Documents/Thèse ISMO/DonneesLabo/17_10-21_10/apresmidi sans pola/temp/3.txt
OK
/media/96D0265ED0264539/Users/HAN/Documents/Thèse ISMO/DonneesLabo/17_10-21_10/apresmidi sans pola/temp/2.txt
OK
/media/96D0265ED0264539/Users/HAN/Documents/Thèse ISMO/DonneesLabo/17_10-21_10/apresmidi sans pola/temp/1.txt
OK
/media/96D0265ED0264539/Users/HAN/Documents/Thèse ISMO/DonneesLabo/17_10-21_10/apresmidi sans pola/temp/0.txt
OK
/home/han/Desktop/CombinedSpectres.txt
OK
耗费时间: 28230 ms

附: 运行结果即合并后的单个文件

到此,实验室中频谱数据处理就告一段落了。总结就是:改变算法非常重要。遇到问题,找到根源后,总会有改良的算法来解决它的。

******************************************************************************************************************************************************************************

不过,随着继续经验的积累,我发现全部使用流操作是可以的,即:用约5000个输入流同时分别指向每一个要合并的文件,然后用一个输出流指向合并结果文件。

虽然也许会遇到同时打开的流数限制(发现在Linux下是这样的,Windows尚未发现),但是可以再通过分批处理的思想来解决的。

在本程序中又采用了另一种方法:同时开通N个输入流分别指向N个要合并的数据文件, 写入最后的单个结果文件。效果证明运行时间差不多,但是占用内存更少。

 

PS: 但是发现了一个Linux下特有的问题(因为此问题在Windows下不存在): 当文件读到大约第4092个时,抛出了“文件找不到“的异常。 所以断定同时打开输入流的数目在Linux下此时被限制为4092。 所以此时必须分批来处理,每批处理数目自动设为能够同时打开的最大流数目 (实际发现是输入流和输出流的总数,而此数字可以通过抛出的异常信息获得)。

 

总结:这样此程序即可以在Linux中运行,又可以在Windows中运行,就是做到了真正的跨平台!


代码如下:

 

  1. package com.han;  
  2.   
  3. import java.io.*;  
  4.   
  5.   
  6. public class CombinedTextSpectres6  
  7.       
  8.     private final int N=6;//N是保存的文件名称的长度   
  9.     private final int M=4520;//M是保存的文件数目    
  10.     String ExperienceCondition="Integrated time is ms, with the HR Spectrometer, with polarizorH, using the syringe Rh6G+Glycol and the other is Huile" 
  11.     String dirPath="/media/96D0265ED0264539/Users/HAN/Documents/Thèse ISMO/DonneesLabo/17_10-21_10/apresmidi sans pola";//定义存放离散采样数据文件的文件夹   
  12.     //  String dirPath="/media/96D0265ED0264539/Users/HAN/Documents/Thèse ISMO/DonneesLabo/10_10-14_10/spectre sans pola";   
  13.     static String destDirPath="/home/han/Desktop";//定义存放最终结果文件的文件夹   
  14.     File new_file;  
  15.   
  16.     File[] FileArray;  
  17.     int Column;  
  18.     FileReader[] fr;  
  19.     BufferedReader[] bfr;  
  20.     FileWriter fw;  
  21.     BufferedWriter bfw;  
  22.       
  23.     public CombinedTextSpectres6() throws IOException{  
  24.         long startTime=System.currentTimeMillis();  
  25.         try 
  26.               
  27.             FileArray=new File(dirPath).listFiles();  
  28.             Column=FileArray.length;// 4000 limit   
  29.             fr=new FileReader[Column];//声明   
  30.             bfr=new BufferedReader[Column];  
  31.               
  32.             for(int i=0;i//初始化   
  33.                 fr[i]=new FileReader(FileArray[i]);  
  34.                 bfr[i]=new BufferedReader(fr[i]);  
  35.              
  36.               
  37.             new_file=new File(destDirPath,"CombinedSpectres.txt");  
  38.             fw new FileWriter(new_file);  
  39.             bfw=new BufferedWriter(fw);  
  40.               
  41.             bfw.write(ExperienceCondition);  
  42.             bfw.newLine();  
  43.               
  44.             bfw.write("Wavelength (nm)\t");  
  45.             for(int i=0;i
  46.                 String filename="00"+i;//"00"是保存的文件的前缀     
  47.                 int temLength=filename.length();  
  48.                 for(int j=0;j
  49.                     filename="0"+filename;  
  50.                  
  51.                 filename=filename+".txt" 
  52.                 bfw.write(filename);  
  53.                 bfw.write("\t");  
  54.              
  55.             bfw.newLine();  
  56.               
  57.             String s;  
  58.             while((s=bfr[0].readLine())!=null){  
  59.                 for(int i=0;i
  60.                     if(i==0){  
  61.                         bfw.write(s.replace(',''.'));  
  62.                         bfw.write("\t");  
  63.                     }else 
  64.                         bfw.write(bfr[i].readLine().split("\t")[1].replace(',''.'));  
  65.                         bfw.write("\t");  
  66.                      
  67.                  
  68.                 bfw.newLine();  
  69.              
  70.             for(int i=0;i
  71.                 bfr[i].close();  
  72.                 fr[i].close();            
  73.                 
  74.             bfw.close();  
  75.             fw.close();  
  76.             System.out.println(new_file);  
  77.             System.out.println("OK");  
  78.         catch (Exception e)  
  79.             // TODO Auto-generated catch block   
  80.             String strFile=e.getMessage();  
  81.             String fileName=strFile.substring(dirPath.length()+1dirPath.length()+7);  
  82.             int maxNumFile=Integer.parseInt(fileName);  
  83.             maxNumFile=maxNumFile-6;//for "bfr[i].close();" successful   
  84.               
  85.             for(int i=0;i//close all input streams opened for the aims of releasing the resources.   
  86.                 bfr[i].close();  
  87.                 fr[i].close();            
  88.                 
  89.             System.out.printf("The maximum opened file number limited by the current computer environment is %d\n",maxNumFile-1);  
  90.             System.out.println("So we have to retreat the problem by considering the limitation of the computer resources.");  
  91.             new LinuxLimit(maxNumFile-1).run();  
  92.          
  93.         long endTime=System.currentTimeMillis();  
  94.         System.out.println("耗费时间: "+(endTime-startTime)+ms");  
  95.      
  96.     class LinuxLimit{  
  97.         private final int maxNumFile;  
  98.         File tempDir=new File(destDirPath,"temp");  
  99.   
  100.         LinuxLimit(int maxNumFile){  
  101.             this.maxNumFile=maxNumFile;  
  102.          
  103.         void run(){   
  104.             int fileStart=0 
  105.             int fileEnd=0 
  106.             int num=0 
  107.             String filename=num+".txt" 
  108.             if(!tempDir.exists()){  
  109.                 tempDir.mkdir();  
  110.              
  111.             Column=maxNumFile;  
  112.             try 
  113.                 while(Column>=maxNumFile){  
  114.                     fileEnd=fileStart+maxNumFile;  
  115.                     fw=new FileWriter(new File(tempDir,filename));  
  116.                     bfw=new BufferedWriter(fw);  
  117.                     System.out.println(fileStart);  
  118.                     System.out.println(fileEnd);  
  119.                     for(int i=fileStart;i//初始化   
  120.                         fr[i-fileStart]=new FileReader(FileArray[i]);  
  121.                         bfr[i-fileStart]=new BufferedReader(fr[i-fileStart]);  
  122.                      
  123.                     String s;  
  124.                     while((s=bfr[0].readLine())!=null){  
  125.                         for(int i=0;i
  126.                             if(i==0 && num==0){  
  127.                                 bfw.write(s.replace(',''.'));  
  128.                                 bfw.write("\t");  
  129.                             }else if(i==0 && num!=0){  
  130.                                 bfw.write(s.split("\t")[1].replace(',''.'));  
  131.                                 bfw.write("\t");  
  132.                             }else if(i!=0){  
  133.                                 bfw.write(bfr[i].readLine().split("\t")[1].replace(',''.'));  
  134.                                 bfw.write("\t");  
  135.                              
  136.                          
  137.                         bfw.newLine();  
  138.                      
  139.                     for(int i=0;i
  140.                         bfr[i].close();  
  141.                         fr[i].close();            
  142.                      
  143.                     bfw.close();  
  144.                     fw.close();   
  145.                     fileStart=fileEnd;  
  146.                     Column=M-fileEnd;         
  147.                     num++;  
  148.                     filename=num+".txt" 
  149.                  
  150.                 if(Column!=0){  
  151.                     fw=new FileWriter(new File(tempDir,filename));  
  152.                     bfw=new BufferedWriter(fw);  
  153.                     for(int i=fileStart;i//初始化 M=fileStart+Column   
  154.                         fr[i-fileStart]=new FileReader(FileArray[i]);  
  155.                         bfr[i-fileStart]=new BufferedReader(fr[i-fileStart]);  
  156.                      
  157.                     String s;  
  158.                     while((s=bfr[0].readLine())!=null){  
  159.                         for(int i=0;i
  160.                             if(i==0){  
  161.                                 bfw.write(s.split("\t")[1].replace(',''.'));  
  162.                                 bfw.write("\t");  
  163.                             }else 
  164.                                 bfw.write(bfr[i].readLine().split("\t")[1].replace(',''.'));  
  165.                                 bfw.write("\t");  
  166.                              
  167.                          
  168.                         bfw.newLine();  
  169.                      
  170.                     for(int i=0;i
  171.                         bfr[i].close();  
  172.                         fr[i].close();            
  173.                      
  174.                     bfw.close();  
  175.                     fw.close();                   
  176.                  
  177.   
  178.                   
  179.                 FileArray=tempDir.listFiles();  
  180.                 Column=FileArray.length;   
  181.                 fr=new FileReader[Column];//声明   
  182.                 bfr=new BufferedReader[Column];  
  183.                 new_file=new File(destDirPath,"CombinedSpectres.txt");  
  184.                 fw new FileWriter(new_file);  
  185.                 bfw=new BufferedWriter(fw);  
  186.                 for(int i=0;i
  187.                     System.out.println(FileArray[i]);  
  188.                  
  189.                 for(int i=0;i//初始化   
  190.                     fr[i]=new FileReader(FileArray[i]);  
  191.                     bfr[i]=new BufferedReader(fr[i]);  
  192.                  
  193.                   
  194.                   
  195.                 bfw.write(ExperienceCondition);  
  196.                 bfw.newLine();  
  197.                   
  198.                 bfw.write("Wavelength (nm)\t");  
  199.                 for(int i=0;i
  200.                     String fname="00"+i;//"00"是保存的文件的前缀    
  201.                     int temLength=fname.length();  
  202.                     for(int j=0;j
  203.                         fname="0"+fname;  
  204.                      
  205.                     fname=fname+".txt" 
  206.                     bfw.write(fname);  
  207.                     bfw.write("\t");  
  208.                  
  209.                 bfw.newLine();  
  210.                   
  211.                 String s;  
  212.                 while((s=bfr[0].readLine())!=null){  
  213.                     for(int i=0;i
  214.                         if(i==0){  
  215.                             bfw.write(s);  
  216.                             bfw.write("\t");  
  217.                         }else 
  218.                             bfw.write(bfr[i].readLine());  
  219.                             bfw.write("\t");  
  220.                          
  221.                      
  222.                     bfw.newLine();  
  223.                  
  224.                 for(int i=0;i
  225.                     bfr[i].close();  
  226.                     fr[i].close();            
  227.                     
  228.                 bfw.close();  
  229.                 fw.close();  
  230.                 for(File e:FileArray){  
  231.                     e.delete();  
  232.                  
  233.                 tempDir.delete();  
  234.                 System.out.println("The temp folder has been deleted.");  
  235.             }catch(Exception e){  
  236.                 e.printStackTrace();  
  237.              
  238.          
  239.      
  240.     public static void main(String[] args)  
  241.         // TODO Auto-generated method stub         
  242.         try  
  243.             new CombinedTextSpectres6();  
  244.         catch (IOException e)  
  245.             // TODO Auto-generated catch block   
  246.             e.printStackTrace();  
  247.          
  248.   
  249.      
  250.     
package com.han;

import java.io.*;


public class CombinedTextSpectres6 {
        
        private final int N=6;//N是保存的文件名称的长度
        private final int M=4520;//M是保存的文件数目 
        String ExperienceCondition="Integrated time is 1 ms, with the HR Spectrometer, with polarizorH, using the syringe Rh6G+Glycol and the other is Huile";
        String dirPath="/media/96D0265ED0264539/Users/HAN/Documents/Thèse ISMO/DonneesLabo/17_10-21_10/apresmidi sans pola";//定义存放离散采样数据文件的文件夹
        //      String dirPath="/media/96D0265ED0264539/Users/HAN/Documents/Thèse ISMO/DonneesLabo/10_10-14_10/spectre sans pola";
        static String destDirPath="/home/han/Desktop";//定义存放最终结果文件的文件夹
        File new_file;

        File[] FileArray;
        int Column;
        FileReader[] fr;
        BufferedReader[] bfr;
        FileWriter fw;
        BufferedWriter bfw;
        
        public CombinedTextSpectres6() throws IOException{
                long startTime=System.currentTimeMillis();
                try{
                        
                        FileArray=new File(dirPath).listFiles();
                        Column=FileArray.length;// 4000 limit
                        fr=new FileReader[Column];//声明
                        bfr=new BufferedReader[Column];
                        
                        for(int i=0;i=maxNumFile){
                                        fileEnd=fileStart+maxNumFile;
                                        fw=new FileWriter(new File(tempDir,filename));
                                        bfw=new BufferedWriter(fw);
                                        System.out.println(fileStart);
                                        System.out.println(fileEnd);
                                        for(int i=fileStart;i

运行结果:The maximum opened file number limited by the current computer environment is : 4091
So we have to retreat the problem by considering the limitation of the computer resources.
0
4091
/home/han/Desktop/temp/0.txt
/home/han/Desktop/temp/1.txt
The temp folder has been deleted.
耗费时间: 17582 ms

程序中所要用到的数据文件以及合并后的结果文件都和前面是相同的。

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有