Search code examples
javacsvdelimited

CSV to tab delimited java in command line


Updated Code. This program should take the CSV file and separate it into TSV files by school,but I am not getting it to work. I am getting it to create the files correctly, but only one has any data in it...

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

Solution

  • The first thing, you have to do is reading the input file. I think, you need to read it line by line (depends on file structure).

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

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

    Next step is to seperate the data and sort it by school (if i understood your question well).

    For this you have to split the data and create a class to store the information:

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

    When you have created a Student object for each student in the list, you have to sort the students by school:

    You could implement compareable and use Collection.sort().

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

    Last thing is to print the output, for this you have to override the toString method of the student class:

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

    and iterate throug the list of your students and call the toString method:

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

    EDIT:

    If you need the output in a file and not in the console, just use a fileoutputStream and a bufferedwriter to print the output of the toString method in a file.