1

I need to create a setter, so that I can change the name of the merged file as I please. The exportToSingleFile() method takes the arrray input and creates a .csv file for each student and the mergeFile() method reads all of the files and merges the information into 1 output file. I need to rename this output file.

My main method is as follows:

import java.lang.*;
import java.io.*;

public class HW2_Main {
    public static void main(String[] args) throws IOException {

        myFile studentFiles = new myFile();
        studentFiles.exportToSingleFile();
        studentFiles.mergeFile();
        studentFiles.setFileName("Can't Figure Out");

    }
}

This is the new class myFile that I am getting all of my information from to create the objects:

class myFile{

    // INPUTS
    String[] headers = {"COURSE ID", "TEAM ID", "STUDENT FIRST NAME", "STUDENT LAST NAME",
                        "STUDENT ID", "ASSIGNMENT ID", "DATE SUBMITTED", "TIME SUBMITTED", "SUBMITTED BY"};

    String[][] values = {
                        {"CMPS280-01", "PELICAN01", "FIRST-1", "LAST-1",
                         "W1234567", "H01", "08/22/17", "23:53", "W1234567"},
                        {"CMPS280-01", "PELICAN01", "FIRST-2", "LAST-2",
                         "W1234566", "H01", "08/22/17", "23:54", "W1234566"},
                        {"CMPS280-01", "PELICAN01", "FIRST-3", "LAST-3",
                         "W1234568", "H01", "08/22/17", "20:15", "W1234568"}};

    myFile(){


    }
    void exportToSingleFile() throws IOException{

        /*WRITE STUDENT INFO TO TEXT FILES*/
        for (int i = 0; i < values.length; i++) {

            //Generate file name for each student and prepare for writing to file
            PrintWriter printToFile = new PrintWriter(new File("INPUT/"+values[i][1]+"_"+values[i][2]+"_"+values[i][3]+".csv"));

            //Write headers to the file
            for (int j = 0; j < headers.length - 1; j++) {
                printToFile.print(headers[j] + ",");
            }
                printToFile.println(headers[headers.length - 1]);

            //Write values to the file
            for (int j = 0; j < headers.length - 1; j++) {
                printToFile.print(values[i][j] + ",");
            }
                printToFile.print(values[i][headers.length - 1]);

        printToFile.close();
        }//end for
    }
    void mergeFile() throws IOException{

        PrintWriter printToFileAllSubmit = new PrintWriter(new File("OUTPUT/All_Submit.csv"));

        //Write headers to the file All_Submit_
            for (int j = 0; j < headers.length - 1; j++) {
                printToFileAllSubmit.print(headers[j] + ",");
            }
                printToFileAllSubmit.println(headers[headers.length - 1]);

        //Get list of files in folder SUBMIT
        File[] fileList = new File("INPUT").listFiles();
            for (File f : new File("INPUT").listFiles()){
            //System.out.println(f.getName());
                BufferedReader bufRead = new BufferedReader(new FileReader(f));

                    //Read successive lines
                    while (bufRead.readLine() != null) {
                        printToFileAllSubmit.println(bufRead.readLine());
                    }
                    bufRead.close();
            }
        printToFileAllSubmit.close();

    }
    void setFileName(String newFileName){


    }
}
simplest
  • 217
  • 1
  • 5
  • 16
  • Possible duplicate of [Rename a file using Java](https://stackoverflow.com/questions/1158777/rename-a-file-using-java) – Naman Sep 25 '17 at 06:01
  • Why don't you set the name before you merge it? – Kayaman Sep 25 '17 at 06:09
  • I was wondering how to do it in this specific case, I realized after posting that I could split the initialization. – simplest Sep 25 '17 at 06:31
  • I think it just simple like rename a file. You can follow that : https://www.mkyong.com/java/how-to-rename-file-in-java/ – BIZ Sep 25 '17 at 07:04

0 Answers0