在java中将数据转换为csv文件?

kzipqqlq  于 2021-08-25  发布在  Java
关注(0)|答案(1)|浏览(444)

我正在尝试将数组中的数据转换为java中的csv文件。我有一个csv文件,其中的数据是行和列,需要修改。在修改数据并将其存储在简单数组(而不是arraylist)中之后,我想将其导出或存储在另一个文件中。但当我运行代码时,只有最后一项以csv格式打印。
这是我的密码:

import java.util.*;
import java.io.*;
import java.text.*;

public class transformation {
    public static final String delimiter = "[,./]";

    public static void read(String csvFile) {
        try {
            String line = "";
            String[] csvData = new String[94];
            File file = new File(csvFile);
            FileReader fr = new FileReader(file);
            BufferedReader br = new BufferedReader(fr);
            while ((line = br.readLine()) != null) {
            //Convert file to csv
                for (int i = 0; i < csvData.length; i++) {
                    System.out.print(csvData[i] + ",");
                }
                System.out.println();
                String filename = "C:/Users/dhanjay.bhardwaj/Desktop/Test.csv";
                try (PrintWriter pw = new PrintWriter(filename)) {
                    for (int j = 0; j < csvData.length; j++) {
                        pw.write(csvData[j] + ",");
                    }
                }
                catch (FileNotFoundException e) {
                    e.printStackTrace();
                }

            }
            br.close();
        }
        catch(IOException err) {
            err.printStackTrace();
        }
    }

    public static void main(String[] args) {
        String csvFile = "C:/Users/dhanjay.bhardwaj/Desktop/helloworld.csv";
        transformation.read(csvFile);
    }
}

我尝试了几种方法,但都不起作用,所有方法的输出都是一样的。

2nc8po8w

2nc8po8w1#

您不需要写入该文件。你需要更像编辑的东西:对不起,我注意力不集中了。尝试

import java.io.*;
import java.text.*;

public class transformation {
    public static final String delimiter = "[,./]";

    public static void read(String csvFile) {
        try {
            String[] csvData = new String[94];

            File file = new File(csvFile);
            FileReader fr = new FileReader(file);
            BufferedReader br = new BufferedReader(fr);

            String line = "";
            String[] tempArr;
            String id = "123";
            String defaultValue = "A";
            String defaultCode = "0000";
            String defaultCurrency = "USD";
            String defaultName = "RECON SYSTEM";

            Date date = new Date();
            String sysDate = new SimpleDateFormat("YYYY/MM/dd").format(date);
            String filename = "Test.csv";
            try (PrintWriter pw = new PrintWriter(filename)) {

                while ((line = br.readLine()) != null) {
                    tempArr = line.split(delimiter);

                    // Date-Format
                    tempArr[0] = tempArr[0] + tempArr[2];
                    tempArr[2] = tempArr[0].substring(0, tempArr[0].length() - tempArr[2].length());
                    tempArr[0] = tempArr[0].substring(tempArr[2].length());
                    //

                    // Code-Combination
                    String arrVar3 = tempArr[3];
                    String arrVar5 = tempArr[5];
                    String arrVar6 = tempArr[6];
                    String arrVar7 = tempArr[7];

                    tempArr[3] = "0" + arrVar6;
                    tempArr[4] = "00" + tempArr[4];
                    tempArr[5] = arrVar3;
                    tempArr[6] = arrVar7;
                    tempArr[9] = "0" + tempArr[9];
                    //

                    // Displaying .csv data
                    // for (String tempStr: tempArr) {
                    // System.out.print(tempStr + " ");
                    // }

                    // System.out.println();

                    // Display Transformation data in array
                    csvData[0] = "NEW";
                    csvData[1] = id;
                    csvData[2] = "20" + tempArr[0] + "/" + tempArr[1] + "/" + tempArr[2];
                    csvData[3] = "Application";
                    csvData[4] = "RECON SYSTEM";
                    csvData[5] = defaultCurrency;
                    csvData[6] = sysDate;
                    csvData[7] = defaultValue;
                    csvData[8] = tempArr[3];
                    csvData[9] = tempArr[4];
                    csvData[10] = tempArr[5];
                    csvData[11] = tempArr[6];
                    csvData[12] = tempArr[7];
                    csvData[13] = tempArr[8];
                    csvData[14] = tempArr[9];
                    csvData[15] = defaultCode;
                    csvData[16] = "";
                    csvData[17] = "";
                    csvData[18] = "";
                    csvData[19] = "";
                    csvData[20] = "";
                    csvData[21] = "";
                    csvData[22] = "";
                    csvData[23] = "";
                    csvData[24] = "";
                    csvData[25] = "";
                    csvData[26] = "";
                    csvData[27] = "";
                    csvData[28] = "";
                    csvData[29] = "";
                    csvData[30] = "";
                    csvData[31] = "";
                    csvData[32] = "";
                    csvData[33] = "";
                    csvData[34] = "";
                    csvData[35] = "";
                    csvData[36] = "";
                    csvData[37] = "";
                    csvData[38] = tempArr[10] + "." + tempArr[11];
                    csvData[39] = tempArr[12] + "." + tempArr[13];
                    csvData[40] = "";
                    csvData[41] = "";
                    csvData[42] = file.getName().replaceFirst("[.][^.]+$", "");
                    csvData[43] = "";
                    csvData[44] = "";
                    csvData[45] = defaultName;
                    csvData[46] = "";
                    csvData[47] = "";
                    csvData[48] = "";
                    csvData[49] = "";
                    csvData[50] = "";
                    csvData[51] = "604 T-Recs RECON SYSTEM";
                    csvData[52] = "";
                    csvData[53] = "";
                    csvData[54] = "";
                    csvData[55] = "";
                    csvData[56] = "";
                    csvData[57] = "";
                    csvData[58] = "";
                    csvData[59] = "";
                    csvData[60] = "";
                    csvData[61] = "";
                    csvData[62] = "";
                    csvData[63] = "";
                    csvData[64] = "";
                    csvData[65] = "";
                    csvData[66] = "";
                    csvData[67] = "";
                    csvData[68] = "";
                    csvData[69] = "";
                    csvData[70] = "";
                    csvData[71] = "";
                    csvData[72] = "";
                    csvData[73] = "";
                    csvData[74] = "";
                    csvData[75] = "";
                    csvData[76] = "";
                    csvData[77] = "";
                    csvData[78] = "";
                    csvData[79] = "";
                    csvData[80] = "";
                    csvData[81] = "";
                    csvData[82] = "";
                    csvData[83] = "";
                    csvData[84] = "";
                    csvData[85] = "";
                    csvData[86] = "";
                    csvData[87] = "";
                    csvData[88] = "";
                    csvData[89] = "";
                    csvData[90] = "";
                    csvData[91] = "";
                    csvData[92] = "";
                    csvData[93] = "";

                    System.out.println();
                    // Convert file to csv
                    String sep = "";
                    for (int i = 0; i < csvData.length; i++) {
                        pw.print(sep);
                        pw.print(csvData[i]);
                        sep = ",";
                    }
                    pw.println();

                }
                br.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        } catch (IOException err) {
            err.printStackTrace();
        }
    }

    public static void main(String[] args) {
        String csvFile = "helloworld.csv";
        transformation.read(csvFile);
    }
}

相关问题