View Javadoc
1   /*******************************************************************************
2    *   Gisgraphy Project 
3    * 
4    *   This library is free software; you can redistribute it and/or
5    *   modify it under the terms of the GNU Lesser General Public
6    *   License as published by the Free Software Foundation; either
7    *   version 2.1 of the License, or (at your option) any later version.
8    * 
9    *   This library is distributed in the hope that it will be useful,
10   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   *   Lesser General Public License for more details.
13   * 
14   *   You should have received a copy of the GNU Lesser General Public
15   *   License along with this library; if not, write to the Free Software
16   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
17   * 
18   *  Copyright 2008  Gisgraphy project 
19   *  David Masclet <davidmasclet@gisgraphy.com>
20   *  
21   *  
22   *******************************************************************************/
23  package com.gisgraphy.importer;
24  
25  import java.io.BufferedInputStream;
26  import java.io.BufferedOutputStream;
27  import java.io.BufferedReader;
28  import java.io.File;
29  import java.io.FileInputStream;
30  import java.io.FileNotFoundException;
31  import java.io.FileOutputStream;
32  import java.io.IOException;
33  import java.io.InputStreamReader;
34  import java.io.OutputStreamWriter;
35  import java.util.ArrayList;
36  import java.util.List;
37  
38  import org.slf4j.Logger;
39  import org.slf4j.LoggerFactory;
40  
41  import com.gisgraphy.domain.valueobject.Constants;
42  
43  /**
44   * @author <a href="mailto:david.masclet@gisgraphy.com">David Masclet</a>
45   * 
46   */
47  public class FileSpliter {
48  
49      /**
50       * The logger
51       */
52      protected static final Logger logger = LoggerFactory.getLogger(FileSpliter.class);
53  
54      public int countLines(String filename) {
55  	int lines = 0;
56  	BufferedReader br = null;
57  	BufferedInputStream bis = null;
58  	try {
59  	    bis = new BufferedInputStream(new FileInputStream(filename));
60  	    br = new BufferedReader(new InputStreamReader(bis, Constants.CHARSET));
61  	    while (br.readLine() != null) {
62  		lines++;
63  	    }
64  	} catch (Exception e) {
65  	    logger.warn("can not count lines for " + filename + " : " + e.getMessage(), e);
66  	} finally {
67  	    if (bis != null) {
68  		try {
69  		    bis.close();
70  		} catch (IOException e) {
71  
72  		}
73  	    }
74  	    if (br != null) {
75  		try {
76  		    br.close();
77  		} catch (IOException e) {
78  
79  		}
80  	    }
81  	}
82  	return lines;
83      }
84  
85      public List<File> SplitByLength(File file, int splitlen) throws FileNotFoundException {
86  	if (file == null || !file.exists() || !file.isFile()) {
87  	    throw new FileNotFoundException("File "+file+" must be not null and exists");
88  	}
89  	if (splitlen <= 0) {
90  	    throw new IllegalArgumentException("we can not split file for null or negative length");
91  	}
92  	String filename = file.getName();
93  	 FileInputStream fin=null;
94  	BufferedReader br = null;
95  	BufferedInputStream bis = null;
96  	List<File> splitedFiles = new ArrayList<File>();
97  	try {
98  	    int i = 0;
99  	    fin = new FileInputStream(file.getAbsolutePath());
100 	    bis = new BufferedInputStream(fin);
101 	    br = new BufferedReader(new InputStreamReader(bis,
102 			Constants.CHARSET));
103 	    String line = br.readLine();
104 	    int len = 0;
105 	    while (line != null) {
106 		String filenameWOExtension = filename;
107 		String extension = "";
108 		int index = filename.lastIndexOf('.');
109 		if (index > 0) {
110 		    filenameWOExtension = filename.substring(0, index);
111 		    extension = filename.substring(index+1, (filename.length()));
112 		}
113 		String splitedFileName = filenameWOExtension + "." + (i + 1) + "." + extension;
114 
115 		String outputFileName = file.getParent()+File.separator+splitedFileName;
116 		logger.info("will split in a new splited file : "+outputFileName);
117 
118 		File outputFile = new File(outputFileName);
119 		splitedFiles.add(outputFile);
120 		BufferedOutputStream fw = new BufferedOutputStream(new FileOutputStream(outputFile));
121 		OutputStreamWriter writer = new OutputStreamWriter(fw);
122 		while (line != null && len < splitlen) {
123 		    writer.write(line+"\r\n");
124 		    line = br.readLine();
125 		    len++;
126 		}
127 		len = 0;
128 		fw.flush();
129 		writer.flush();
130 		writer.close();
131 		i++;
132 	    }
133 	    file.delete();
134 	    return splitedFiles;
135 	}
136 
137 	catch (IOException e) {
138 	    throw new FileNotFoundException();
139 	} finally {
140 	    if (fin!=null){
141 		try {
142 		    fin.close();
143 		} catch (IOException e) {
144 		}
145 	    }
146 	    if (br!=null){
147 		try {
148 		    br.close();
149 		} catch (IOException e) {
150 		}
151 	    }
152 	}
153     }
154 }