如何从 CSV header 创建 pojo 类

How to create pojo classes from CSV header

我有 CSV 文件,其中包含名字、姓氏、薪水等字段名称。我想从这个文件生成 pojo class.there CSV 文件中没有数据类型信息,因此请考虑所有字段都是字符串类型。 有什么方法可以在构建时从 csv 文件创建 pojo class?

下面是 CSV 文件。

employees.csv

empId,firstName,lastName,title,salary 

由此我想生成如下的 pojo

class employees{
   private string empId;
   private string firstName;
   private string lastName;
   private string title;
   private string salary;

   // getters and setters 
    .
    .
    .
   //toString() method
}

You can use OpenCSV to process your CSV files and use javassist to dynamically create the Java Classes.

各自网站上都有例子。

I strongly recommend to look into

希望对您有所帮助!

最后我根据要求得到了解决方案我通过设置 classname、parentDirectory、packageName 和 CSV file.it 手动创建 pojo class 将创建 .java 在带有字段的指定目录文件中,setter,getters 和 toString method.but 如果文件中只有一个 header 行,它将起作用。
代码如下

 public static void createPojoFromCsvHeader(File csvInputFile,String directoryOfjavaFile,String packageName,String className)
  {    
      try(BufferedReader stream = new BufferedReader(new FileReader(csvInputFile))) {
        String packagePath=packageName.replace(".","/");
        String javaOutputDirPath=directoryOfjavaFile+"/"+packagePath+"/";
        System.out.println("creating directory ->"+javaOutputDirPath);
        File f=new File(javaOutputDirPath);
        if(f.mkdirs()){
            System.out.println("directory :"+javaOutputDirPath+" created succesfully..");
        }else{
            System.out.println("directory :"+javaOutputDirPath+" already exist..");
        }
        String javaOutputFilePath=directoryOfjavaFile+"/"+packagePath+"/"+className+".java";
        File javaOutPutFile=new File(javaOutputFilePath);
        javaOutPutFile.createNewFile();
        PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(javaOutputFilePath)));          
        System.out.println("generating class..");            
        out.println("package "+packageName+";");
        out.println("public class " + className + " {");
        String line = null;
        String[] fields = null;
        int rowNum = 0;
        while ((line = stream.readLine()) != null) {
            if (line.isEmpty() || line.startsWith("#")) {
                continue;
            } else {
                if (fields == null) {
                    fields = line.split(",");
                }

                rowNum++;
                String[] values = line.split(",");
                for (int i = 0; i < fields.length; i++) {
                    out.println("\t\tprivate String " + fields[i] + " = \""+ StringEscapeUtils.escapeJava(values[i])+ "\";");
                }

                for (int i = 0; i < fields.length; i++) {
                    String tempField=StringEscapeUtils.escapeJava(values[i]).substring(0, 1).toUpperCase()+StringEscapeUtils.escapeJava(values[i]).substring(1);

                    //getter method
                    out.println("");
                    out.println("\t\tpublic String  get"+tempField+ "(){");
                    out.println("\t\t\treturn this."+StringEscapeUtils.escapeJava(values[i])+";");
                    out.println("\t\t}");
                    //setter method
                    out.println("\t\tpublic void  set"+tempField+"(String "+ StringEscapeUtils.escapeJava(values[i])+"){");
                    out.println("\t\t\t this."+StringEscapeUtils.escapeJava(values[i])+" = "+ StringEscapeUtils.escapeJava(values[i])+";");
                    out.println("\t\t}");
                }

                out.println("");
                out.println("// toString() Method");
                out.println("\t\t public String toString(){");
                StringBuffer buffer=new StringBuffer();
                buffer.append("\"{");
                for (int i = 0; i < fields.length; i++) {
                    buffer.append("\\""+StringEscapeUtils.escapeJava(values[i])+"\\"=\"+"+StringEscapeUtils.escapeJava(values[i]));
                    if(i < fields.length-1){
                        buffer.append("+\",");
                    }
                    else{
                        buffer.append("+\"");
                    }
                }
                buffer.append("}\";");
                out.println("\t\t\t return "+buffer);
                out.println("\t\t}");
            }
            out.println("}");
            out.close();
        }
        System.out.println("no of lines fetch from csv :"+rowNum);
        }catch(IOException e)
      {
            e.printStackTrace();
      }

  }