Generate TXT files and compress them.

Source: Internet
Author: User
Code:
/**
* Generate a TXT file and compress it
*/
Public class result {
Private log;

Properties properties;

Private file filehave;

Private fileoutputstream writer;

Private printwriter PW;

Private writer out;

Private string mobiletype, time, ytime, basefilename;

Public result (){
Try {
Intital ();

} Catch (ioexception iox ){
System. Err. println (iox );
}

}

/**
* Initialization Function
* @ Return
* @ Throws ioexception
*/
Private void intital () throws ioexception {
Log = logfactory. getlog (result. Class );
File SDF = new file ("");
System. Out. println (SDF. getabsolutepath ());
Properties = new properties ();
Properties. Load (New fileinputstream ("config. properties "));
}
Private arraylist getmidcolresdata (string type, int purpose ){

Arraylist ret = new arraylist ();
Connection conn = getconn ();
Statement stmt = NULL;
Resultset rs = NULL;
Try {

...... Database operations

Rs. Close ();
Stmt. Close ();
Rs = NULL;
Stmt = NULL;

} Catch (exception e ){
Log. debug (E );
} Finally {
If (RS! = NULL ){
Rs = NULL;
}
If (stmt! = NULL ){
Stmt = NULL;
}
If (Conn! = NULL ){
Conn = NULL;
}
}
Return ret;
}

// Generate/Write a TXT file
Public void writetotxt (string type, int purpose ){
Simpledateformat formatter = new simpledateformat ("yyyymmdd ");
Time = formatter. Format (new date ());

Java. util. Date mydate = new java. util. Date ();
Long mytime = (mydate. gettime ()/1000)-60*60*24;
Mydate. settime (mytime * 1000 );
String ydate = formatter. Format (mydate );

// Generate a TXT file
Try {

If (mobiletype. Equals ("test ")){
If (purpose = 0 ){
Basefilename = "test _" + time + "_ 01 ";
} Else if (purpose = 1 ){
Basefilename = "test _" + time + "_ 02 ";
}
}

// Delete the TXT file from yesterday
Filehave = new file ("test _" + ydate + "_01.txt ");
If (filehave. exists ()){
Filehave. Delete ();
}
Filehave = new file ("test _" + ydate + "_02.txt ");
If (filehave. exists ()){
Filehave. Delete ();
}

Filehave = new file ("test _" + ydate + "_01.zip ");
If (filehave. exists ()){
Filehave. Delete ();
}
Filehave = new file ("test _" + ydate + "_02.zip ");
If (filehave. exists ()){
Filehave. Delete ();
}

Writer = new fileoutputstream (basefilename + ". txt ");

Out = new outputstreamwriter (writer, "UTF-8 ");

} Catch (ioexception iox ){
System. Err. println (iox );
}

// Write the TXT file
Try {
Arraylist list = getmidcolresdata (mobiletype, purpose );
// Create a data row
List Data = List;
For (Int J = 0; j <data. Size (); j ++ ){
String [] rows = (string []) data. Get (j );
For (int K = 0; k <rows. length; k ++ ){
Out. write (new string (rows [K]. getbytes ("ISO-8859-1"), "UTF-8"); // The Integrated string is reconverted once (it has been converted once when the database retrieves data)
}
}
Try {

Out. Flush ();
Writer. Close ();
} Catch (ioexception iox ){
System. Err. println (iox );
}
} Catch (exception e ){
Log. debug (E );
}
}
Public void ziptxt (){
// These are the files to include in the ZIP file
String [] filenames = new string [] {"test" + basefilename + ". txt "};

// Create a buffer for reading the files
Byte [] Buf = new byte [1, 1024];

Try {
// Create the ZIP file
String outfilename = "test" + basefilename + ". Zip ";
Zipoutputstream out = new zipoutputstream (New fileoutputstream (outfilename ));

// Compress the files
For (INT I = 0; I <filenames. length; I ++ ){
Fileinputstream in = new fileinputstream (filenames [I]);

// Add ZIP entry to output stream.
Out. putnextentry (New zipentry (filenames [I]);

// Transfer bytes from the file to the ZIP file
Int Len;
While (LEN = in. Read (BUF)> 0 ){
Out. Write (BUF, 0, Len );
}

// Complete the entry
Out. closeentry ();
In. Close ();
}

// Complete the ZIP file
Out. Close ();
} Catch (ioexception e ){
}
}
Public static void main (string ARGs []) {
Try {
System. Out. println ("create txtfile begin ");

Result test = new result ();

For (INT I = 0; I <2; I ++ ){
Test. writetotxt ("test", I); // generate a file
Test.zip txt (); // compress the file
}
For (INT I = 0; I <4; I ++ ){
Test. writetotxt ("what", I); // generate a file
Test.zip txt (); // compress the file
}
System. Out. println ("create txtfile end ");

} Catch (exception e ){
System. Out. println (E. tostring ());
}
}

}

Then use> javac-classpath...-encoding GBK result. Java

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.