Tuesday, 22nd June 2010
The new frontier for learning Java

Split File

From WikiJava

Jump to: navigation, search
The author suggests:

buy 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.

Contents

the article

Image:250px-Subversion.png
You can download the complete code of this article from the Subversion repository at this link

Using the username:readonly and password: readonly

See the using the SVN repository instructions page for more help about this.

This little example comprises three methods:

public static void main(String[] args)
responsible of checking the input from the prompt and for getting it into the proper variables
public static void getData
responsible for reading the input file and splitting it into it's chunks of fixed dimension
private static void saveFile(StringBuffer stringBuffer, String filename)
which is used to save the StringBuffer into a file.

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.java

package 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

Comments from the users

To be notified via mail on the updates of this discussion you can login and click on watch at the top of the page


Title (required):

Website:

Comment: