gpt4 book ai didi

java - 如何使用 BufferedReader 在 java 中复制并写入文本文件

转载 作者:行者123 更新时间:2023-12-02 12:18:54 27 4
gpt4 key购买 nike

我能够让我的 SUBMIT 文件夹正确地包含 3 个 .csv 文件,正确命名并包含正确的学生信息,但是当我尝试复制 SUBMIT 文件夹中每个 .csv 的第二行并打印到单个文本中时文件在 OUTPUT 文件夹中我在文件中得到以下输出:

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

我的代码是:

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();
}

}

}
}

最佳答案

将标题行写入 OUTPUT 目录中的文件后,您将复制 SUBMIT 中相应文件中的所有行,包括 标题行。看起来您不需要重新编写 OUTPUT 文件的标题行。或者,您可以在从 SUBMIT 文件复制时跳过标题行的写入。

顺便说一句,第二次编写标题行时,您缺少行终止符。

关于java - 如何使用 BufferedReader 在 java 中复制并写入文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45948657/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com