SHARE
TWEET

FileHandler.java

a guest Jan 17th, 2014 467 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.io.BufferedReader;
  2. import java.io.File;
  3. import java.io.FileWriter;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.InputStreamReader;
  7. import java.util.ArrayList;
  8.  
  9. import com.sforce.bulk.CsvWriter;
  10.  
  11.  
  12. public class FileHandler
  13. {
  14.    
  15.         public static ArrayList<String> getResultLines(ArrayList<String> textData){
  16.         ArrayList<String> resultLines = new ArrayList<String>();
  17.         int i =0;
  18.         boolean flag = false;
  19.        
  20.        
  21.         for(int j = 0; j < textData.size(); j++){
  22.                 //if line has text on it,
  23.                 if(textData.get(j) != null && textData.get(j).length() > 0){
  24.                         //if line is the start of a new record Standard object Id's start with "00" custom objects start with "a0"
  25.                         if(textData.get(j).startsWith("\"0") || textData.get(j).startsWith("\"a")){
  26.                                 //adds line to array, substitutes separators with unique separators and removes quotes
  27.                                 flag = false;
  28.                                 resultLines.add(textData.get(j));
  29.                                 resultLines.set(i, resultLines.get(i).replaceAll("\",\"", "___"));
  30.                                 resultLines.set(i, resultLines.get(i).replaceAll("\"", ""));
  31.                                 i++;
  32.                                 //sets flag based on if line has come back to fix a line break
  33.                                 if(!flag)
  34.                                         flag = true;
  35.                         }
  36.                         //if last line was a new record and this line isn't a new record, add to previous line
  37.                         else if(flag == true && !((textData.get(j).startsWith("\"0")) || (textData.get(j).startsWith("\"a")))){
  38.                                 resultLines.set(i-1, resultLines.get(i-1) + textData.get(j));
  39.                                 resultLines.set(i-1, resultLines.get(i-1).replaceAll("\",\"", "___"));
  40.                                 resultLines.set(i-1, resultLines.get(i-1).replaceAll("\"", ""));
  41.                         }
  42.                         //case for header line
  43.                         else{
  44.                                 resultLines.add(textData.get(j));
  45.                                 resultLines.set(i, resultLines.get(i).replaceAll("\",", "___"));
  46.                                 resultLines.set(i, resultLines.get(i).replaceAll("\"", ""));
  47.                                 i++;
  48.                         }
  49.                 }
  50.         }
  51.        
  52.         return resultLines;
  53.     }
  54.        
  55.     public static void writeCSVFromStream(InputStream in, String object) throws IOException{
  56.         //gets QueryResultStream from BulkExample and object name, creates BufferedReader and output file in specified folder
  57.         //makes output folder if it doesnt exist
  58.         File mkdir = new File("Output");
  59.         System.out.println("Was new Output folder created? "+mkdir.mkdirs());
  60.         BufferedReader read
  61.            = new BufferedReader(new InputStreamReader(in));
  62.         //writes csv file to output folder
  63.         FileWriter csvFile = new FileWriter(mkdir.getAbsolutePath()+"/"+object+".csv");
  64.         String line = "";
  65.         ArrayList<String> textData = new ArrayList<String>();
  66.         //makes array of results
  67.         while((line = read.readLine()) != null){
  68.                 if(line.length()>0){
  69.                         line.trim();
  70.                         textData.add(line);
  71.                 }
  72.            
  73.         }
  74.         //cleans results fixing description and new line problems
  75.         ArrayList<String> resultLines = getResultLines(textData);
  76.        
  77.         //null textData and suggest a Garbage Collection to collect dereferenced strings to try to optimize speed and conserve space
  78.         //textData = null;
  79.         //System.gc();
  80.        
  81.         //writes to csv
  82.         CsvWriter writer = new CsvWriter(resultLines.get(0).split("___"),csvFile);
  83.         for(int k = 1; k < resultLines.size();k++){
  84.                 String[] row = resultLines.get(k).split("___");
  85.                 writer.writeRecord(row);
  86.         }
  87.        
  88.         //closes stream
  89.         writer.endDocument();
  90.     }
  91. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top