Advertisement
LostInMyThoughts137

Merge

Feb 10th, 2021
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.37 KB | None | 0 0
  1.  
  2. import java.io.BufferedReader;
  3. import java.io.BufferedWriter;
  4. import java.io.IOException;
  5. import java.io.File;
  6. import java.io.FileInputStream;
  7. import java.io.FileWriter;
  8. import java.io.InputStreamReader;
  9. import java.util.regex.Matcher;
  10. import java.util.regex.Pattern;
  11.  
  12. public class Merge {
  13.  
  14.     public static void main(String[] args) {
  15.  
  16.         // create String variables to store the filepaths to get files later
  17.         // using relative path because I placed them in the src folder
  18.         String fileOnePath = "src/Indiegogo002_2020-12-18.csv";
  19.         String fileTwoPath = "src/Indiegogo002_2021-01-15.csv";
  20.  
  21.         // create String variable to establish what directory to put the merged file in
  22.         String mergedFilePath = "src/Indiegogo002_merged_files";
  23.  
  24.         // creating an array called csvFiles that has a capacity of two
  25.         // the first index of csvFiles [0] stores the first file from the fileOnePath
  26.         // the second index of csvFiles [1] stores the second file from the fileTwoPath
  27.         File[] csvFiles = new File[2];
  28.         csvFiles[0] = new File(fileOnePath);
  29.         csvFiles[1] = new File(fileTwoPath);
  30.  
  31.         // create a new file for the merged Files
  32.         File mergedFile = new File(mergedFilePath);
  33.  
  34.         // calling my mergeFiles method to combine the files in the csvFiles array and create a new file
  35.         mergeFiles(csvFiles, mergedFile);
  36.  
  37.         // using regex Pattern and Matcher to find keywords
  38.         Pattern p = Pattern.compile("Games", Pattern.CASE_INSENSITIVE);
  39.         Matcher m = p.matcher("Video Games");
  40.         boolean matchFound = m.find();
  41.  
  42.         if (matchFound) {
  43.             System.out.println("Match found");
  44.         } else {
  45.             System.out.println("Match not found.");
  46.         }
  47.  
  48.     } // end of main class
  49.  
  50.     public static void mergeFiles(File[] files, File mergedFile) {
  51.  
  52.         // create FileWriter and BufferedWriter set to null so assigning what they write to can be done in try/catch block
  53.         FileWriter csvData = null;
  54.         // the BufferedWriter makes FileWriter more efficient because it writes large amounts of data at once
  55.         BufferedWriter out = null;
  56.  
  57.         // the try block creates a FileWriter so that what the FileWriter writes to the mergedFile is appended to csvData
  58.         try {
  59.             csvData = new FileWriter(mergedFile, true);
  60.             out = new BufferedWriter(csvData);
  61.         } catch (IOException e) {
  62.             e.printStackTrace();
  63.         }
  64.  
  65.         // use for loop to iterate over csv files
  66.         for (File indieGoGoCsv:files) {
  67.             System.out.println("Merging: " + indieGoGoCsv.getName());
  68.             FileInputStream inputStream;
  69.             try {
  70.                 inputStream = new FileInputStream(indieGoGoCsv);
  71.                 BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
  72.  
  73.                 String aLine;
  74.                 while ((aLine = in.readLine()) != null) {
  75.                     out.write(aLine);
  76.                     out.newLine();
  77.                 }
  78.                 in.close();
  79.             } catch (IOException x) {
  80.                 x.printStackTrace();
  81.             }
  82.         } // end of for loop
  83.  
  84.         try {
  85.             out.close();
  86.         } catch (IOException x) {
  87.             x.printStackTrace();
  88.         }
  89.     } // end of mergeFiles
  90.  
  91. } // end of Merge class
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement