To process a SQL file as an example
The row format for the source file is:
123456 7895433 xxxxx yyyyy zzzzz
The row separator is TAB.
The format of the rows in the file after conversion is:
123456,7895433,xxxxx,yyyyy,zzzzz
Packagetool;ImportJava.io.BufferedReader;ImportJava.io.BufferedWriter;ImportJava.io.File;ImportJava.io.FileOutputStream;ImportJava.io.FileReader;Importjava.io.IOException;ImportJava.io.OutputStreamWriter; Public classTransferfile { Public Static voidMain (string[] args)throwsIOException {File f=NewFile ("F:/mernis/data_dump.sql"); File FileWrite=NewFile ("F:/mernis/mernis-test.txt"); intCount=0; if(F.exists ()) {FileReader Read=NewFileReader (f); BufferedReader Buff=NewBufferedReader (read); BufferedWriter out=NewBufferedWriter (NewOutputStreamWriter (NewFileOutputStream (FileWrite,true))); BooleanIsend=false; intI=0; while(!isend) {String line; I++; Try{ Line=Buff.readline (); if(line==NULL) {Isend=true; } Else { if(i>=76&&i<=49611784) {string[] Linearr= Line.split ("\\t"); String NewLine=""; for(intj = 0; J < Linearr.length; J + +) { if(j<linearr.length) {newline=newline+linearr[j]+ ","; } ElseNewLine=newline+Linearr[j]; } out.write (NewLine+ "\ r \ n"); } Count++;//if (i==100) {//return; // } } } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); }} System.out.println ("File Total" +count+ "line"); Buff.close (); Out.close (); } Else{System.out.println ("The file you entered does not exist, please use it correctly:"); } }}Transferfile.java
The key codes are:
string[] Linearr = Line.split ("\\t"); String newline= ""; for (int j = 0; J < Linearr.length; J + +) { if (j<linearr.length) { newline =newline+linearr[j]+ ","; } Else newline=newline+linearr[j];}
In the process, I tried to replace tab with "," with Line.replace ("\\t", ","), but it was never successful, so we used the tab to split the string first and then use the comma to connect the method.
java-processing Large-capacity text files, the method of Geff to tab in line