Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.IOException;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileWriter;
- import java.io.InputStreamReader;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class Merge {
- public static void main(String[] args) {
- // create String variables to store the filepaths to get files later
- // using relative path because I placed them in the src folder
- String fileOnePath = "src/Indiegogo002_2020-12-18.csv";
- String fileTwoPath = "src/Indiegogo002_2021-01-15.csv";
- // create String variable to establish what directory to put the merged file in
- String mergedFilePath = "src/Indiegogo002_merged_files";
- // creating an array called csvFiles that has a capacity of two
- // the first index of csvFiles [0] stores the first file from the fileOnePath
- // the second index of csvFiles [1] stores the second file from the fileTwoPath
- File[] csvFiles = new File[2];
- csvFiles[0] = new File(fileOnePath);
- csvFiles[1] = new File(fileTwoPath);
- // create a new file for the merged Files
- File mergedFile = new File(mergedFilePath);
- // calling my mergeFiles method to combine the files in the csvFiles array and create a new file
- mergeFiles(csvFiles, mergedFile);
- // using regex Pattern and Matcher to find keywords
- Pattern p = Pattern.compile("Games", Pattern.CASE_INSENSITIVE);
- Matcher m = p.matcher("Video Games");
- boolean matchFound = m.find();
- if (matchFound) {
- System.out.println("Match found");
- } else {
- System.out.println("Match not found.");
- }
- } // end of main class
- public static void mergeFiles(File[] files, File mergedFile) {
- // create FileWriter and BufferedWriter set to null so assigning what they write to can be done in try/catch block
- FileWriter csvData = null;
- // the BufferedWriter makes FileWriter more efficient because it writes large amounts of data at once
- BufferedWriter out = null;
- // the try block creates a FileWriter so that what the FileWriter writes to the mergedFile is appended to csvData
- try {
- csvData = new FileWriter(mergedFile, true);
- out = new BufferedWriter(csvData);
- } catch (IOException e) {
- e.printStackTrace();
- }
- // use for loop to iterate over csv files
- for (File indieGoGoCsv:files) {
- System.out.println("Merging: " + indieGoGoCsv.getName());
- FileInputStream inputStream;
- try {
- inputStream = new FileInputStream(indieGoGoCsv);
- BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
- String aLine;
- while ((aLine = in.readLine()) != null) {
- out.write(aLine);
- out.newLine();
- }
- in.close();
- } catch (IOException x) {
- x.printStackTrace();
- }
- } // end of for loop
- try {
- out.close();
- } catch (IOException x) {
- x.printStackTrace();
- }
- } // end of mergeFiles
- } // end of Merge class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement