CSV到命令行中的制表符分隔的java

时间:2016-01-22 15:27:10

标签: java csv delimited

更新的代码。该程序应该采用CSV文件并将其分成TSV文件,但是我没有让它工作。我正在让它正确创建文件,但只有一个文件中有任何数据......

public class Student implements Comparable<Student>{

    public int id = 0;
    public String name = "";
    public String school = "";


    public Student(int id, String name, String school){

        this.id = id;
        this.name = name;
        this.school = school;
    }

    public String toString(){
        return id+"\t"+name+"\t"+school;
    }

    @Override
    public int compareTo(Student o) {
        return  this.school.compareTo(o.school);
    }
}

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;


public class ReadCSV {

    public static String CSV_FILE_PATH = "/Users/eringray/Desktop/csvtotab/input.csv";

    public static void main(String[] args){

        try {

            BufferedReader br = new BufferedReader(new FileReader(CSV_FILE_PATH));
            BufferedWriter bw = new BufferedWriter(new FileWriter(CSV_FILE_PATH + ".tsv"));

            ArrayList<Student> list = new ArrayList<Student>();

            String line = "";
            while((line = br.readLine()) != null) {
                String[] values = line.split(",");

                if(values.length == 3) {
                    String idAsString = values[0];
                    String name = values[1];
                    String school = values[2];

                    int id = Integer.parseInt(idAsString);

                    Student s = new Student(id, name, school);

                    list.add(s);
                }
            }

            Collections.sort(list);

            String currentSchool = "";
            for(int i = 0; i < list.size(); i++){
                Student stu = list.get(i);
                if(currentSchool != stu.school){
                  currentSchool = stu.school; 
                  bw = new BufferedWriter(new FileWriter(CSV_FILE_PATH + stu.school + ".tsv"));
              }

              String lineText = stu.toString();
              bw.write(lineText);
              bw.newLine();
            }

            br.close();
            bw.close();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

1 个答案:

答案 0 :(得分:2)

首先,您需要做的是读取输入文件。 我想,你需要逐行阅读(取决于文件结构)。

https://docs.oracle.com/javase/7/docs/api/java/io/FileInputStream.html

https://docs.oracle.com/javase/7/docs/api/java/io/BufferedReader.html

下一步是分开数据并按学校排序(如果我理解你的问题)。

为此,您必须拆分数据并创建一个类来存储信息:

https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split(java.lang.String)

public Class Student{
   public String name = "";
   ....

   public Student(String name, String school, ...){}
}

为列表中的每个学生创建学生对象后,您必须按学校对学生进行排序:

您可以实现compareable并使用Collection.sort()。

https://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html

最后一件事是打印输出,为此你必须覆盖学生类的toString方法:

public String toString(){
   return this.id+"\t"+this.name+"\t"+this.school;
}

并遍历学生列表并调用toString方法:

System.out.println(students.get(i).toString());

修改

如果您需要输出文件而不是控制台,只需使用fileoutputStreambufferedwriter在文件中打印toString方法的输出。