Java中的IO的性能优化 [摘]
来源:互联网 发布:大智慧贵金属软件 编辑:程序博客网 时间:2024/05/27 12:22
下面方法不能读大文件
Java中的IO的性能优化
在使用IO的时候注意一下细节,能使性能得到很大的优化.
首先读写大文件,使用Buffer是肯定的了,使用方法,有下边两个
1 方法一:
2 public static void test1(String fileName) {
3 long start = System.currentTimeMillis();
4 try {
5 FileInputStream fis = new FileInputStream(fileName);
6 BufferedReader br = new BufferedReader( new InputStreamReader(fis) );
7 StringBuffer sb = new StringBuffer();
8 String str;
9 while ((str = br.readLine()) != null ) {
10 sb.append(str);
11 }
12 } catch (IOException e) {
13 }
14 long end = System.currentTimeMillis();
15 long time = end - start;
16 System.out.println(time);
17 }
18 方法二:
19 public static void test2(String fileName) {
20 long start = System.currentTimeMillis();
21 try {
22 FileInputStream fis = new FileInputStream(fileName);
23 byte buf[] = new byte [ 8192 ];
24 int n;
25 StringBuffer sb = new StringBuffer();
26 while ((n = fis.read(buf)) != - 1 ) {
27 sb.append(buf);
28 }
29 fis.close();
30 } catch (IOException e) {
31 }
32 long end = System.currentTimeMillis();
33 long time = end - start;
34 System.out.println(time);
35 }
2 public static void test1(String fileName) {
3 long start = System.currentTimeMillis();
4 try {
5 FileInputStream fis = new FileInputStream(fileName);
6 BufferedReader br = new BufferedReader( new InputStreamReader(fis) );
7 StringBuffer sb = new StringBuffer();
8 String str;
9 while ((str = br.readLine()) != null ) {
10 sb.append(str);
11 }
12 } catch (IOException e) {
13 }
14 long end = System.currentTimeMillis();
15 long time = end - start;
16 System.out.println(time);
17 }
18 方法二:
19 public static void test2(String fileName) {
20 long start = System.currentTimeMillis();
21 try {
22 FileInputStream fis = new FileInputStream(fileName);
23 byte buf[] = new byte [ 8192 ];
24 int n;
25 StringBuffer sb = new StringBuffer();
26 while ((n = fis.read(buf)) != - 1 ) {
27 sb.append(buf);
28 }
29 fis.close();
30 } catch (IOException e) {
31 }
32 long end = System.currentTimeMillis();
33 long time = end - start;
34 System.out.println(time);
35 }
方法一1937ms
方法二47ms
测试结果相差大概50倍左有,很大。
而我平时的习惯是方法一。问题主要出在readLine()上。
如果只是简单的文件拷贝,下边两种方法也不错
1 public static void test3() {
2 String cmd = " copy d:/out1.txt out2.txt " ;
3 try {
4 Runtime.getRuntime().exec(cmd);
5 } catch (Exception e) {
6 e.printStackTrace();
7 }
8 }
9 public static void test4() {
10 String inFileName = " d:/out1.txt " ;
11 String outFileName = " d:/out2.txt " ;
12 long start = System.currentTimeMillis();
13 try {
14 File inFile = new File(inFileName);
15 File outFile = new File(outFileName);
16 RandomAccessFile inRaf = new RandomAccessFile(inFile, " r " );
17 RandomAccessFile outRaf = new RandomAccessFile(outFile, " rw " );
18 FileChannel infc = inRaf.getChannel();
19 FileChannel outfc = outRaf.getChannel();
20 infc.transferTo( 0 , inFile.length(),outfc );
21 infc.close();
22 outfc.close();
23 } catch (Exception e) {
24 e.printStackTrace();
25 }
26 long end = System.currentTimeMillis();
27 long time = end - start;
28 System.out.println(time);
29 }
2 String cmd = " copy d:/out1.txt out2.txt " ;
3 try {
4 Runtime.getRuntime().exec(cmd);
5 } catch (Exception e) {
6 e.printStackTrace();
7 }
8 }
9 public static void test4() {
10 String inFileName = " d:/out1.txt " ;
11 String outFileName = " d:/out2.txt " ;
12 long start = System.currentTimeMillis();
13 try {
14 File inFile = new File(inFileName);
15 File outFile = new File(outFileName);
16 RandomAccessFile inRaf = new RandomAccessFile(inFile, " r " );
17 RandomAccessFile outRaf = new RandomAccessFile(outFile, " rw " );
18 FileChannel infc = inRaf.getChannel();
19 FileChannel outfc = outRaf.getChannel();
20 infc.transferTo( 0 , inFile.length(),outfc );
21 infc.close();
22 outfc.close();
23 } catch (Exception e) {
24 e.printStackTrace();
25 }
26 long end = System.currentTimeMillis();
27 long time = end - start;
28 System.out.println(time);
29 }
- Java中的IO的性能优化 [摘]
- Java中的IO的性能优化
- IO性能的优化
- JAVA,IO性能优化——缓冲区
- Java 6中的性能优化
- Java编程中的性能优化
- Java 性能的优化
- Java性能的优化
- Java性能的优化
- Java性能的优化
- Java性能的优化
- Java性能的优化
- Java性能的优化
- Java性能的优化
- Java性能的优化
- Java性能的优化
- Java性能的优化
- Java性能的优化
- 一些有用的网址
- 通过代理服务器下载网页
- 《C#高级编程》第9章:集合,ArrayList,Stack,Queue,SortedList
- 学习appfuse
- ASP.NET(C#)中使数据库中读取的文本换原格式显示的函数
- Java中的IO的性能优化 [摘]
- 使用strtotime作日期比较
- win2003改远程登陆端口号(修改3389默认端口)
- 如何学习vc++(vc的用处)
- 一百个最有用的网站地址
- MAX_BONUS
- FORTRAN语言编译器---词法分析(C语言简易版)
- 复习java: I/O
- meta标签之详解