把CSV文件转换成TXT文件

csv转换成txt文件,这个代码的功能是把csv文件中的一些行取出来保存到txt文件中。

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

public class Preprocess {

    private static BufferedReader br = null;
    private static PrintWriter out = null;
    private static ArrayList<String[]> recordArr = new ArrayList<String[]>();
    private static ArrayList<double[]> recordArrdouble=new ArrayList<double[]>();
    private static ArrayList<int[]> recordArrint=new ArrayList<int[]>();

    public static void main(String [] args) {

        String line = "";
        String csvSplit = ",";
        //String fileToConvert = "pure";

            try {
                //out = new PrintWriter(new PrintWriter("F:/Common/Purefix/"+ fileToConvert +".txt","UTF-8"));
                //br = new BufferedReader(new FileReader("F:/Common/Purefix/" + fileToConvert + ".csv"));
                out=new PrintWriter(new PrintWriter("C:/Users/Administrator/Workspaces/MyEclipse Professional 2014/Datapreprocess/src/data1.txt","UTF-8"));
                br=new BufferedReader(new FileReader("C:/Users/Administrator/Workspaces/MyEclipse Professional 2014/Datapreprocess/src/data1.csv"));

                while((line = br.readLine()) != null ) {

                    String[] recCopyToBePushed = new String[128];
                    String[] recordExtracted = line.split(csvSplit, 129);
                    double[] recordDouble=new double[128];
                    for(int a=0;a<128;a++){
                        recordDouble[a]=0;
                    }
                    int[] recordInt=new int[128];
                    for(int a=0;a<128;a++){
                        recordInt[a]=0;
                    }
                    if(recordExtracted[128].endsWith("A")){
                        for(int x=0;x<recordExtracted.length-1;x++){
                            recordExtracted[x]=recordExtracted[x].replaceAll("inf", "1000000"); 
                            recCopyToBePushed[x]=recordExtracted[x];
                        }
                        recordArr.add(recCopyToBePushed);
                        recordArrdouble.add(recordDouble);
                        recordArrint.add(recordInt);
                    }
                    //for(int x = 0; x < recordExtracted.length; x++){
                    //  recCopyToBePushed[x] = recordExtracted[x];
                    //}             
                    //recordArr.add(recCopyToBePushed);             
                }

                for(int a=0;a<recordArr.size();a++){
                    for(int b=0;b<recordArr.get(a).length;b++){
                        recordArrdouble.get(a)[b]=Double.parseDouble(recordArr.get(a)[b]);
                        recordArrint.get(a)[b]=(int)recordArrdouble.get(a)[b];
                    }
                }


                for( int y = 0; y< recordArrint.size(); y++){
                    for(int z = 0; z < recordArrint.get(y).length; z++){
                        out.print(recordArrint.get(y)[z]+" ");
                    }
                    out.println("");
                }

            } catch(FileNotFoundException fnfe ){
                    fnfe.printStackTrace();
            } catch(IOException ie) {
                    ie.printStackTrace();
            } finally {
                if(br != null) {
                    try {
                        br.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                if(out != null) {
                    out.close();
                }
            }

            for(int i = 0; i < recordArrint.size(); i++){

                System.out.print("PROCESSING LINE :\t");

                for(int j = 0; j< recordArrint.get(i).length; j++){
                    System.out.print(recordArrint.get(i)[j] + "    ");  
                }
                System.out.println("");
            }

        System.out.println("Files Proccessed And Converted!");  
    }
    // End of main
}

以上就是把csv文件转换成txt文件数据。

  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值