00001
00002 import java.io.IOException;
00003 import java.io.FileReader;
00004 import java.io.BufferedReader;
00005 import java.util.ArrayList;
00006 import java.io.*;
00007
00008
00009 public class TransposeCSVMatrix {
00010
00014 public static void main(String[] args) throws IOException {
00015
00016 if(args.length<1) {return;}
00017
00018
00019 ArrayList<ArrayList<String>> matrix = new ArrayList<ArrayList<String>>();
00020 BufferedReader input = new BufferedReader(new FileReader(args[0]));
00021 String line="";
00022
00023
00024
00025 while (( line = input.readLine()) != null){
00026 ArrayList<String> row= new ArrayList<String>();
00027 for(String f : line.split(","))
00028 row.add(f);
00029 matrix.add(row);
00030 }
00031
00032 String outfile = "transposed.csv";
00033 if(args.length>1) {outfile=args[1];}
00034
00035
00036 PrintWriter out = new PrintWriter(new FileWriter(outfile));
00037
00038 for(int col=0;col<matrix.get(0).size(); col++) {
00039
00040 for(int row=0;row<matrix.size();row++) {
00041 ArrayList<String> r = matrix.get(row);
00042 out.print(r.get(col)+",");
00043 }
00044 out.print("\n");
00045 }
00046 out.close();
00047 }
00048
00049 }