将.dat文件转换为.csv文件时出现问题

时间:2019-04-12 14:45:03

标签: java file csv

我正在尝试将.dat文件转换为csv文件,但是我遇到了问题。下面是我的代码:

public class Main {
   public static void main(String[] args) {
       DatToCsv dataToCsv = new DatToCsvImpl(); //creating object of DataToCsvImpl class
       try {
           // Three parameter should be giving to convertDatToCsv function namely input file location and output file
           // location and fixed column size respectively
           dataToCsv.convertDatToCsv("testing.dat", "testing.csv", 5);
       } catch (FileNotFoundException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       } catch (IOException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       }
   }
}

代码2:

public interface DatToCsv {
   public void convertDatToCsv(String datFileLocation,String outputFile, int column)throws IOException;
}

代码3:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class DatToCsvImpl implements DatToCsv {
   @Override
   public void convertDatToCsv(String datFileLocation, String outputFile, int column) throws IOException {
       // creating instance of file writer, buffer reader and writer
       String st;
       FileWriter writer;
       BufferedWriter bw;
       BufferedReader br;
       String csvData = "";
       int count = 0;
       try {
           File file = new File(datFileLocation); // .dat file location
           writer = new FileWriter(outputFile); // .csv file location
           bw = new BufferedWriter(writer); // giving writer to buffer writer constructor to initilize
           br = new BufferedReader(new FileReader(file)); // giving input file (.dat) file to buffer reader
           StringBuilder sb = new StringBuilder();
           /*
           * Using while loop (as long as) upto end of file getting data by line by line
           */
           while ((st = br.readLine()) != null) {
               /*
               * replacing multiple space into single space and comma separated of single
               * space
               */
               st = st.trim().replaceAll(" +", ",");
               // if first line of file consider to be heading
               if (count == 0) {
                   csvData = st;
               } else {
                   csvData = csvData + "," + st;
               }
               count++;
           }
           // converting all comma seperated value to string array
           String csvArray[] = csvData.split(",");
           int runningCount = 1;
           for (String str : csvArray) {
               if (runningCount == column) { // compare fixed column size to running count every fixed column size new
                                               // line added \n
                   sb.append(str + "\n");
                   runningCount = 1;
               } else {
                   sb.append(str + ","); // otherwise comma added at the end of every key
                   runningCount++;
               }
           }
           bw.write(sb.toString());
           /*
           * flush is because if data 1024bytes not it not store to csv file so we need to
           * explicitly mention push data to csv
           */
           bw.flush();
           writer.close();
           br.close();
           bw.close();
       } catch (IOException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       }
   }
}

无论出于什么原因,它都不起作用,也不接受我的输入,并且找不到输出。我已经尝试使用Google搜索来查找输入和输出,但是我仍然无法做到。任何帮助表示赞赏,谢谢

0 个答案:

没有答案