我想用java将csv文件转换为固定宽度文件

时间:2016-03-15 02:27:13

标签: java

我想将CSV文件转换为使用Java中的文件固定

我曾尝试将文本从CSV复制到文本文件,但这不能解决我的问题。我想转换为固定宽度文件,这是要求

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

public class ConvertCSVFile1 {

    public static void main(String[] args) throws IOException {

        BufferedReader br = new BufferedReader (new FileReader ("c:\\sample1.csv"));
          int i;
          ArrayList<String> list = new ArrayList<String> ();

          boolean a = true;
          PrintWriter out = new PrintWriter (new PrintWriter ("C:\\Users\\sampletest.txt"), a);

          try {
              String splitBy = ",";
              String line;
              String temp;
              //ArrayList<String> list = new ArrayList<String>();
             // BufferedReader br = new BufferedReader(new FileReader("c:\\sample1.csv"));
              while((line = br.readLine()) != null){
                   String[] b = line.split(splitBy);
                   temp = b[0]+","+b[1]+","+b[2]+","+b[3];

                   list.add(temp);
              } 


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

      for (i = 0; i < list.size (); i++) {
          out.println (list.get (i));
      }

      out.flush ();
      out.close ();
}

}

输入

a   f   w   b   numinst af  ub
1RW 800 64  22  1   48:02:00    TRUE
1RW 800 16  39  1   48:02:00    TRUE
1RW 800 640 330 1   48:02:00    TRUE
1RW 800 40  124 1   48:02:00    TRUE
1RW 800 32  104 1   48:02:00    TRUE
1RW 800 8   104 1   48:02:00    TRUE
1R1W    800 65536   39  1   96:96   TRUE
1R1W    800 2048    39  1   96:96   TRUE
1R1W    800 8192    39  1   48:48:00    TRUE

0 个答案:

没有答案