简体   繁体   English

根据Java中的列值拆分CSV文件

[英]splitting of csv file based on column value in java

I want to split csv file into multiple csv files depending on column value. 我想根据列值将csv文件拆分为多个csv文件。
Structure of csv file: Name,Id,Dept,Course CSV档案的结构:名称,编号,部门,课程

abc,1,CSE,Btech 
fgj,2,EE,Btech 

(Rows are not separated by ; at end) (行之间不以;分隔)

If value of Dept is CSE or ME , write it to file1.csv, if value is ECE or EE write it to file2.csv and so on. 如果Dept的值为CSE或ME,则将其写入file1.csv,如果值为ECE或EE,则将其写入file2.csv,依此类推。

Can I use drools for this purpose? 我可以为此使用流口水吗? I don't know drools much. 我不太了解流口水。

Any help how it can be done? 有什么帮助吗?

This is what I have done yet: 这是我所做的:

public void run() {

    String csvFile = "C:/csvFiles/file1.csv";
    BufferedReader br = null;
    BufferedWriter writer=null,writer2=null;
    String line = "";
    String cvsSplitBy = ",";
    String FileName = "C:/csvFiles/file3.csv";
    String FileName2 = "C:/csvFiles/file4.csv";

    try {

        writer = new BufferedWriter(new FileWriter(FileName));
        writer2 = new BufferedWriter(new FileWriter(FileName2));
        br = new BufferedReader(new FileReader(csvFile));
        while ((line = br.readLine()) != null) {

               String[] values=line.split(cvsSplitBy);

            if(values[2].equals("CSE"))
            {
                writer.write(line);
            }
            else if(values[2].equals("ECE"))
            {
                writer2.write(line);
            }


        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (br != null) {
            try {
                br.close();
                writer.flush();
                writer.close();
                writer2.close();

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

1) First find column index using header row or if header is not present then by index 1)首先使用标题行查找列索引,或者如果不存在标题则按索引查找

2) Follow below algorithm which will result map of key value where key is column by which split is performed 2)按照以下算法,将得出键值映射,其中键是执行拆分的列

global resultMap; 全局resultMap;

Method add(key,row) {
        data = (resultMap.containsKey(key))? resultMap.get(key):new ArrayList<String>();
        data.add(row);
        resultMap.put(key, data );
    }

Method getSplittedMap(List rows) {
        for (String currentRow : rows) {
            add(key, currentRow);
        }
        return resultMap;
    }

hope this helps. 希望这可以帮助。

FileOutputStream f_ECE = new FileOutputStream("provideloaction&filenamehere");
FileOutputStream f_CSE_ME = new FileOutputStream("provideloaction&filenamehere");
FileInputputStream fin = new FileinputStream("provideloaction&filenamehere");
int size = fin.available(); // find the length of file
byte b[] = new byte[size];
fin.read(b);
String s = new String(b); // file copied into string
String s1[] = s.split("\n");
for (int i = 0; i < s1.length; i++) {
    String s3[] = s1[i].split(",")
    if (s3[2].equals("ECE"))
        f_ECE.write(s1.getBytes());
    if (s3[2].equals("CSE") || s3.equals("EEE"))
        f_CSE_ME.write(payload.getBytes());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM