Created
May 25, 2013 22:56
-
-
Save powerlim2/5651054 to your computer and use it in GitHub Desktop.
CSV file combiner — this simple program takes all
textfiles in a folder and add all the text files into one giant textfile.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
/** | |
* Created with IntelliJ IDEA. | |
* User: joonhyunglim | |
* Date: 5/25/13 | |
* Time: 5:42 PM | |
* To change this template use File | Settings | File Templates. | |
*/ | |
public class CSVcombiner { | |
// this program is written to combine same format text files into one giant file | |
public static void main(String[] args) throws IOException { | |
long start = System.currentTimeMillis(); // this is for measuring the computation time. | |
double rand; | |
// write out the analyzed file to this address below | |
File fileout = new File("/Users/joonhyunglim/Desktop/MSiA/MSiA431/DATA/NASDAQ_daily_combined.csv"); | |
// you don't need to create the file previously, it will create the file if it does not exist | |
if (fileout.createNewFile()) { | |
System.out.println("File created"); | |
} else { | |
System.out.println("I found the file. I am writing to the existing file"); | |
} | |
BufferedWriter output = new BufferedWriter(new FileWriter(fileout)); | |
// read all the files in this folder. | |
File folder = new File("/Users/joonhyunglim/Desktop/MSiA/MSiA431/data/NASDAQ/NASDAQ"); | |
File[] listOfFiles = folder.listFiles(); | |
for (File file : listOfFiles) { | |
if (file.isFile()) { | |
int rownum = 0; | |
System.out.println("\nWorking file is: "+file); | |
BufferedReader input = new BufferedReader(new FileReader(file)); | |
input.readLine(); // take the header out. | |
// this bracket below is the part for actual analysis. | |
try { | |
String line; | |
while ((line = input.readLine()) != null) { | |
output.write(line); | |
output.newLine(); | |
rownum++; | |
} | |
input.close(); | |
System.out.println(file +" has been written." + " Size is: "+rownum); | |
} catch(IOException e) { | |
e.printStackTrace(); | |
} catch(IllegalArgumentException ae) { | |
ae.printStackTrace(); | |
} | |
} else { | |
System.out.println("Error: No file was found in this directory."); | |
} | |
} | |
output.close(); | |
long time = System.currentTimeMillis() - start; | |
System.out.println("\n**Execution Time is : "+time/1000+"seconds**"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment