首页 > 解决方案 > 将 .dat 文件转换为 .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();
       }
   }
}

无论出于何种原因,它都不起作用,它不接受我的输入,并且找不到输出。我曾尝试在谷歌上搜索以寻求帮助以查找输入和输出,但我仍然无法做到。感谢任何帮助谢谢

标签: javafilecsv

解决方案


推荐阅读