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

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

最佳答案

您要做的第一件事是读取输入文件。 我认为,您需要逐行阅读它(取决于文件结构)。

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, ...){}
}

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

您可以实现可比较并使用 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());

编辑:

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

关于java - CSV 到命令行中制表符分隔的 java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34950187/

相关文章:

Java 在运行时打开实时文件

java - Jersey 2 + HK2 - @ApplicationScoped 不工作

java - 即使将交互模式设置为 True,Z3 也会失败

excel - 将多个 csv 文件中的数据导入一个 Excel 工作表并计算平均值

python - 如何使用 Pandas 将巨大的 CSV 转换为 SQLite?

mysql - 在数据库中存储游戏玩家进度 I​​D - MySQL 中的逗号分隔数据与规范化 MySQL 记录(关于服务器性能)

java - 将分隔文件转换为树

sql - T-SQL 如何将逗号分隔的数字字符串转换为整数

java - Spring数据: Define <mongo:options> using java based config

java - 我有一个带有一些格式的跨文本。如何根据该格式将其转换为字符串?