You are on page 1of 3

import

import
import
import
import
import
import
import

java.io.BufferedReader;
java.io.File;
java.io.FileNotFoundException;
java.io.FileReader;
java.io.FileWriter;
java.io.IOException;
java.util.HashMap;
java.util.Map.Entry;

public class run {


public static void main(String[] args) {
// TODO Auto-generated method stub
String inputPath = "C:\\Users\\tubanana\\Desktop\\test.txt";
String outputFolder = "C:\\Users\\tubanana\\Desktop\\output";
// which column to decide where to split, in this case, in the e
xample
// the second column, as it starts from 0
int columnToSplit = 1;
// in here, u can set the csv (comma separated file) delimiter
// some use '\\|', some use tab
String delimiter = ",";
// Use the hash map to string key value pair
// key is the field that we use to seperate the file
// value is the file pointer to the file we are writing to
HashMap<String, FileWriter> FileDic = new HashMap<String, FileWr
iter>();
String currentFileName = "";
// preparation step : Reading the inputs
if(args.length<3)
{
System.out.println("USAGE:");
System.out.println("INPUTPATH OUTPUTFOLDER COLUMNTOSPLIT
DELIMITER<optional>");
return;
}
inputPath = args[0];
outputFolder = args[1];
columnToSplit = Integer.parseInt(args[2]);
if(args.length == 4)
delimiter = args[3];

// preparation step : create directory if not created


File theDir = new File(outputFolder);
// if the directory does not exist, create it
if (!theDir.exists()) {
System.out.println("creating directory: " + outputFolder);

boolean result = theDir.mkdir();


if(result) {
System.out.println(outputFolder+ "created");
}
}
//1. Read the input path
BufferedReader reader;
try {
reader = new BufferedReader(new FileReader(inputPath));
String line = null;
while ((line = reader.readLine()) != null) {
String[] Fields = line.split(delimiter);
// 2. checking for malformed records
if(Fields.length<columnToSplit+1)
{
System.out.println("Skipping this malformed reco
rd:" + line);
continue;
}
//ok! it is complete, so we take a look at the colum
n to seperate
// 3. checking if the key is already known, meaning
we have created a file for it before
currentFileName = Fields[columnToSplit];
if(!FileDic.containsKey(currentFileName))
{
String newFilePath = outputFolder + "\\" + curre
ntFileName + ".txt";
FileWriter fw = new FileWriter(newFilePath);
FileDic.put(currentFileName, fw);
// note: the file is kept open to reduce overhea
d of opening and closing of file
// so, if the program is aborted unexpectedly, t
he file will not be closed properly
}
//4. write the record to file
FileDic.get(currentFileName).write(line);
FileDic.get(currentFileName).write("\n");
}
// 5. After completing the job, need to close the files
for (Entry<String, FileWriter> entry : FileDic.entrySet(
))
{
System.out.println("finishing for " + entry.getK
ey());
entry.getValue().close();
}
System.out.println("Program ended, bye.");

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

You might also like