I am able to get my SUBMIT folder to properly have 3 .csv files named properly and containing the correct student information, but when I attempt to copy the 2nd line of each .csv in the SUBMIT folder and print into the single text file in the OUTPUT folder I am getting this output in the file:
COURSE ID,TEAM ID,STUDENT FIRST NAME,STUDENT LAST NAME,STUDENT ID,ASSIGNMENT ID,DATE SUBMITTED,TIME SUBMITTED,SUBMITTED BYCOURSE ID,TEAM ID,STUDENT FIRST NAME,STUDENT LAST NAME,STUDENT ID,ASSIGNMENT ID,DATE SUBMITTED,TIME SUBMITTED,SUBMITTED BY
CMPS280-01,00,Keith,Dillinger,w0782345,H01,8/25/2017,1:23AM,Keith Dillinger
My code is:
import java.io.*;
public class HW01 {
public static void main(String args[]) throws IOException {
// Create a 1D array to hold header labels
String headerLabels[] =
{
"COURSE ID", "TEAM ID", "STUDENT FIRST NAME",
"STUDENT LAST NAME", "STUDENT ID", "ASSIGNMENT ID",
"DATE SUBMITTED", "TIME SUBMITTED", "SUBMITTED BY"
};
// Create a 2D array to hold header values
String headerValues[][] =
{
{"CMPS280-01", "03", "Jacob", "Harley", "w0389342", "H01", "8/23/2017", "7:24PM", "Jacob Harley"},
{"CMPS280-01", "01", "James", "Brown", "w0479045", "H01", "8/25/2017", "1:14PM", "James Brown"},
{"CMPS280-01", "00", "Keith", "Dillinger", "w0782345", "H01", "8/25/2017", "1:23AM", "Keith Dillinger"}
};
// Create string to change to next line of .csv file
String newLine = "";
// Loop to name .csv files
for (int i = 0; i < headerValues.length; i++){
// Create new .csv file and store in SUBMIT folder
String submitPath = "SUBMIT/"+headerValues[i][0]+"_"+headerValues[i][5]+"_"+headerValues[i][1]+"_"+headerValues[i][4]+".csv";
File submitFile = new File(submitPath);
PrintWriter submitWriter = new PrintWriter(submitFile);
// Print headerLabels to .csv files in SUBMIT folder
for (int j = 0; j < headerValues[i].length; j++){
submitWriter.print(headerLabels[j]);
if (j < headerLabels.length-1)
submitWriter.print(",");
}
// Change to line 2 and write header
submitWriter.println(newLine);
for (int j = 0; j < headerValues[i].length; j++){
submitWriter.print(headerValues[i][j]);
if (j < headerValues[i].length-1)
submitWriter.print(",");
}
submitWriter.close();
// Create new .csv file and store in OUTPUT folder
String outputPath = "OUTPUT/"+"All_submit"+"_"+headerValues[0][0]+"_"+headerValues[0][5]+".csv";
File outputFile = new File(outputPath);
PrintWriter outputWriter = new PrintWriter(outputFile);
// Print headerLabels to .csv file in OUTPUT folder
for (int j = 0; j < headerValues[i].length; j++){
outputWriter.print(headerLabels[j]);
if (j < headerLabels.length-1)
outputWriter.print(",");
}
// Create an array of files to store all objects from SUBMIT folder
File folder = new File("SUBMIT");
File[] fileList = folder.listFiles();
// Read information from the second line of all .csv files in SUBMIT folder
for (File f : fileList){
FileReader reader = new FileReader(f);
BufferedReader br = new BufferedReader(reader);
String line;
while ((line = br.readLine()) != null){
outputWriter.println(line);
System.out.println(line);
}
outputWriter.close();
reader.close();
br.close();
}
}
}
}
After writing a header line to the file in the OUTPUT
directory, you are copying all of the lines from the corresponding file in SUBMIT
, including its header line. It looks like you don't need to re-write the header line for the OUTPUT
file. Alternatively, you can skip the write of the header line when copying from the SUBMIT
file.
As an aside, the second time you write the header line, you are missing the line terminator.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.