java-processing Large-capacity text files, the method of Geff to tab in line

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.