Split FileFrom WikiJavabuy this book
This little example shows how to use the GetFileAsString example for creating a little program to split a big text file into chunks of fixed number of rows.
the article
This little example comprises three methods:
Interesting basic concepts worth mentioning here is the IOException that can be thrown from the getData method. In the example I catched the exception in the getData method and thrown it again in the same place adding an extra comment. Of course catching and rethrowing the same exception can be considered completely useless. I prefer to do it anyway because I wanted to have the exception explicitly thrown in the method. Split.javapackage org.wikijava.file.split; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; /** * * reads a the content of a file splits it into many files * according to the number of lines * * @author Giulio */ public class Split { private final static String NEWLINE = System.getProperty("line.separator"); /** * reads a the content of a file and returns is as a String * * @param filename * the filename to read * @throws IOException */ public static void getData(String filename, int lines) throws IOException { try { //opens the file in a string buffer BufferedReader bufferedReader = new BufferedReader(new FileReader( filename)); StringBuffer stringBuffer = new StringBuffer(); //performs the splitting String line; int i = 0; int counter = 1; while ((line = bufferedReader.readLine()) != null) { stringBuffer.append(line); stringBuffer.append(NEWLINE); i++; if (i >= lines) { //saves the lines in the file saveFile(stringBuffer, filename + counter); //creates a new buffer, so the old can get garbage collected. stringBuffer = new StringBuffer(); i = 0; counter++; } } bufferedReader.close(); } catch (IOException e) { throw new IOException("unable to read from file: " + filename); } } /** * * saves the stringBuffer into a file * * @param stringBuffer the string buffer * @param filename the file name */ private static void saveFile(StringBuffer stringBuffer, String filename) { String path = (new File("")).getAbsolutePath(); File file = new File(path + "/" + filename); FileWriter output = null; try { output = new FileWriter(file); output.write(stringBuffer.toString()); System.out.println("file " + path + filename + " written"); } catch (IOException e) { e.printStackTrace(); } finally { try { output.close(); } catch (IOException e) { // do nothing the file wasn't been even opened } } } /** * * reads the parameters and passes to the responsible method (getData) * * @param args */ public static void main(String[] args) { if (args.length != 2) { System.err .println("usage: Split [number of lines] [filenameToSplit]"); return; } String fileName = args[1]; int lines = Integer.parseInt(args[0]); try { getData(fileName, lines); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } See Also |
