Efficiency of writing java IO files-comparison of several methods, javaio

Source: Internet
Author: User

Efficiency of writing java IO files-comparison of several methods, javaio

Various writing methods

/**
* 1 Write FileOutputStream by byte
*
* @ Param count Number of Write cycles
* @ Param str write a string
*/
Public void outputStreamTest (int count, String str ){
File f = new File ("f: test1.txt ");
OutputStream OS = null;
Try {
OS = new FileOutputStream (f );
For (int I = 0; I <count; I ++ ){
OS. write (str. getBytes ());
}
OS. flush ();
System. out. println ("file's long:" + f. length ());
} Catch (FileNotFoundException e ){
E. printStackTrace ();
} Catch (IOException e ){
E. printStackTrace ();
} Finally {
Try {
OS. close ();
} Catch (IOException e ){
E. printStackTrace ();
}
}
}

/**
* 2 Write BufferedOutputStream in byte Buffer
*
* @ Param count Number of Write cycles
* @ Param str write a string
*/
Public void bufferedOutputTest (int count, String str ){
File f = new File ("f: test2.txt ");
BufferedOutputStream bos = null;
Try {
OutputStream OS = new FileOutputStream (f );
Bos = new BufferedOutputStream (OS );
For (int I = 0; I <count; I ++ ){
Bos. write (str. getBytes ());
}
Bos. flush ();
} Catch (FileNotFoundException e ){
E. printStackTrace ();
} Catch (IOException e ){
E. printStackTrace ();
} Finally {
Try {
Bos. close ();
} Catch (IOException e ){
E. printStackTrace ();
}
}
}
 
/**
* 3 Write FileWriter by character
*
* @ Param count Number of Write cycles
* @ Param str write a string
*/
Public void fileWriteTest (int count, String str ){
File f = new File ("f: test.txt ");
Writer writer = null;
Try {
Writer = new FileWriter (f );
For (int I = 0; I <count; I ++ ){
Writer. write (str );
}
Writer. flush ();
} Catch (IOException e ){
E. printStackTrace ();
} Finally {
Try {
Writer. close ();
} Catch (Exception e ){
E. printStackTrace ();
}
}
}

/**
* 4 Write BufferedWriter by Character Buffer
*
* @ Param count Number of Write cycles
* @ Param str write a string
*/
Public void bufferedWriteTest (int count, String str ){
File f = new File ("f: test3.txt ");
OutputStreamWriter writer = null;
BufferedWriter bw = null;
Try {
OutputStream OS = new FileOutputStream (f );
Writer = new OutputStreamWriter (OS );
Bw = new BufferedWriter (writer );
For (int I = 0; I <count; I ++ ){
Bw. write (str );
}
Bw. flush ();
If (f. exists ()){
F. delete ();
}
} Catch (FileNotFoundException e ){
E. printStackTrace ();
} Catch (IOException e ){
E. printStackTrace ();
} Finally {
Try {
Bw. close ();
} Catch (IOException e ){
E. printStackTrace ();
}
}
}
 
/**
* 5 Write BufferedWriter and BufferedOutputStream by Character Buffer
*
* @ Param count Number of Write cycles
* @ Param str write a string
*/
Public void bufferedWriteAndBufferedOutputStreamTest (int count, String str ){
File f = new File ("f: test4.txt ");
BufferedOutputStream bos = null;
OutputStreamWriter writer = null;
BufferedWriter bw = null;
Try {
OutputStream OS = new FileOutputStream (f );
Bos = new BufferedOutputStream (OS );
Writer = new OutputStreamWriter (bos );
Bw = new BufferedWriter (writer );
For (int I = 0; I <count; I ++ ){
Bw. write (str );
}
Bw. flush ();
If (f. exists ()){
F. delete ();
System. out. println ("delete ---");
}
} Catch (FileNotFoundException e ){
E. printStackTrace ();
} Catch (IOException e ){
E. printStackTrace ();
} Finally {
Try {
Bw. close ();
} Catch (IOException e ){
E. printStackTrace ();
}
}
}
 
/**
* 6 Write BufferedWriter and FileWriter by Character Buffer
*
* @ Param count Number of Write cycles
* @ Param str write a string
*/
Public void bufferedWriteAndFileWriterTest (int count, String str ){
File f = new File ("f: test5.txt ");
FileWriter fw = null;
BufferedWriter bw = null;
Try {
Fw = new FileWriter (f );
Bw = new BufferedWriter (fw );
For (int I = 0; I <count; I ++ ){
Bw. write (str );
}
Bw. flush ();
} Catch (FileNotFoundException e ){
E. printStackTrace ();
} Catch (IOException e ){
E. printStackTrace ();
} Finally {
Try {
Bw. close ();
If (f. exists ()){
F. delete ();
}
} Catch (IOException e ){
E. printStackTrace ();
}
}
}


Test write class

Public static void main (String [] args ){
String str = "abcdefghiJKLMN! ";
Int count = 1000000;
TestOutputStream t = new TestOutputStream ();
 
// 1. fileWrite's time
Long start = System. currentTimeMillis ();
T. fileWriteTest (count, str );
Long end = System. currentTimeMillis ();
System. out. println ("fileWrite's time ---------" + (start-end ));
 
// 2. outputStreamTest's time
Start = System. currentTimeMillis ();
T. outputStreamTest (count, str );
End = System. currentTimeMillis ();
System. out. println ("outputStreamTest's time ---------" + (start-end ));

// 3. bufferedOutputTest's time
Start = System. currentTimeMillis ();
T. bufferedOutputTest (count, str );
End = System. currentTimeMillis ();
System. out. println ("bufferedOutputTest's time ---------" + (start-end ));

// 4. bufferedWriteTest's time
Start = System. currentTimeMillis ();
T. bufferedWriteTest (count, str );
End = System. currentTimeMillis ();
System. out. println ("bufferedWriteTest's time ---------" + (start-end ));

// 5. bufferedWrite And FileWriterTest's time
Start = System. currentTimeMillis ();
T. bufferedWriteAndFileWriterTest (count, str );
End = System. currentTimeMillis ();
System. out. println ("bufferedWrite And FileWriterTest's time ---------" + (start-end ));

// 6. bufferedWrite And BufferedOutputStreamTest's time
Start = System. currentTimeMillis ();
T. bufferedWriteAndBufferedOutputStreamTest (count, str );
End = System. currentTimeMillis ();
System. out. println ("bufferedWrite And BufferedOutputStreamTest's time ---------" + (start-end ));
}
 
/**
* Test results
*
* 1. file's long: 16 KB
*
FileWrite's time ---------- 36
Outputstream test's time ---------- 167
BufferedOutputTest's time ---------- 17
BufferedWriteTest's time ---------- 14
BufferedWrite And FileWriterTest's time ---------- 9
BufferedWrite And BufferedOutputStreamTest's time ---------- 12
*
* 2. file's long: 1600kb
*
FileWrite's time ---------- 69
Outputstream test's time ---------- 1282
BufferedOutputTest's time ---------- 68
BufferedWriteTest's time ---------- 40
BufferedWrite And FileWriterTest's time ---------- 52
BufferedWrite And BufferedOutputStreamTest's time ---------- 37
*
* 3. file's long: 16000kb
*
FileWrite's time ---------- 555
Outputstream test's time ---------- 12448
BufferedOutputTest's time ---------- 599
BufferedWriteTest's time ---------- 346
BufferedWrite And FileWriterTest's time ---------- 316
BufferedWrite And BufferedOutputStreamTest's time ---------- 358
*
* 4. file's long: 16.0kb
*
FileWrite's time ---------- 5203
Outputstream test's time ---------- 127182
BufferedOutputTest's time ---------- 5972
BufferedWriteTest's time ---------- 3445 optimal
BufferedWrite And FileWriterTest's time ---------- 5904
BufferedWrite And BufferedOutputStreamTest's time ---------- 5353

*
* 5. file's long: 1600000kb
*
FileWrite's time ---------- 50416
Outputstream test's time ---------- 1303242
BufferedOutputTest's time ---------- 60931
BufferedWriteTest's time ---------- 46697
BufferedWrite And FileWriterTest's time ---------- 48710
BufferedWrite And BufferedOutputStreamTest's time ---------- 64354
*/

Summary:

For classification by character and byte, except methods 1 and 2, the rest are written to files by character, and writing by character is generally faster than writing by byte. According to java API, the parent class of FileWriter is OutputStreamWriter, both of them are Writer classes. From this point, Method 4 and Method 6 are almost identical, and time is slightly different, but the internal mechanism is the same.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.