`
_Yggd
  • 浏览: 86067 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

java文件操作的比较

    博客分类:
  • I/O
阅读更多
package IO;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class first {


public static void main(String[] args) throws IOException {
FileInputStream fin=new FileInputStream("C:/Users/RDGFT/Desktop/in.txt");
FileOutputStream fout=new FileOutputStream("C:/Users/RDGFT/Desktop/out.txt");
int i;
long a=System.currentTimeMillis();
while((i=fin.read())!=-1)
{
fout.write(i);
}
System.out.println("执行耗时 : "+(System.currentTimeMillis()-a)/1000f+" 秒 ");
fin.close();
fout.close();
}
//57.3s
}





package IO;


import java.io.*;

public class second {


public static void main(String[] args) throws IOException {

FileReader fin=new FileReader("C:/Users/RDGFT/Desktop/in.txt");
FileWriter fout=new FileWriter("C:/Users/RDGFT/Desktop/out.txt");
int i;
long a=System.currentTimeMillis();
while((i=fin.read())!=-1)
{
fout.write(i);
}
System.out.println("执行耗时 : "+(System.currentTimeMillis()-a)/1000f+" 秒 ");
fin.close();
fout.close();
}
//1.958 秒
}





package IO;
import java.io.*;
public class third {


public static void main(String[] args) throws IOException {
BufferedInputStream bin=new BufferedInputStream(new FileInputStream("C:/Users/RDGFT/Desktop/in.txt"));
BufferedOutputStream bout=new BufferedOutputStream(new FileOutputStream("C:/Users/RDGFT/Desktop/out.txt"));
long a=System.currentTimeMillis();
while(bin.available()>0)
{
bout.write(bin.read());
}
System.out.println("执行耗时 : "+(System.currentTimeMillis()-a)/1000f+" 秒 ");
bout.flush();
bin.close();
bout.close();
}
//35.592 秒

}





package IO;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class fourth {

/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new FileReader("C:/Users/RDGFT/Desktop/in.txt"));
BufferedWriter bw=new BufferedWriter(new FileWriter("C:/Users/RDGFT/Desktop/out.txt"));
int i;
long a=System.currentTimeMillis();
String str;
while((str=br.readLine())!=null)
{
bw.write(str);
}
System.out.println("执行耗时 : "+(System.currentTimeMillis()-a)/1000f+" 秒 ");
br.close();
bw.close();
}
//0.42 秒
}


以下是javaNIO中的内容




package NIO;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
public class first {
       public static void copyByMapped() throws Exception {
           FileInputStream fIn = new FileInputStream("C:/Users/RDGFT/Desktop/in.txt");
           FileOutputStream fOut = new FileOutputStream("C:/Users/RDGFT/Desktop/out.txt");
           FileChannel fIChan = fIn.getChannel();
           FileChannel fOChan = fOut.getChannel();
           long fSize = fIChan.size();
           MappedByteBuffer mBuf = fIChan.map(FileChannel.MapMode.READ_ONLY, 0, fSize);
           fOChan.write(mBuf); // this copies the file
           fIChan.close();
           fIn.close();
           fOChan.close();
           fOut.close();
       }
      
       public static void copyByChannel() throws Exception{
       FileInputStream fin = new FileInputStream("C:/Users/RDGFT/Desktop/in.txt" );
       FileOutputStream fout = new FileOutputStream("C:/Users/RDGFT/Desktop/out.txt");
       FileChannel fcin = fin.getChannel();
       FileChannel fcout = fout.getChannel();
       ByteBuffer buffer = ByteBuffer.allocate( 1024 );
       while (true) {
          buffer.clear();
          int r = fcin.read( buffer );
          if (r==-1) {
            break;
          }
          buffer.flip();
          fcout.write( buffer );
          }
       }
      
       public static void copyByFastChannel() throws Exception{
           FileInputStream fin = new FileInputStream("C:/Users/RDGFT/Desktop/in.txt");
           FileOutputStream fout = new FileOutputStream("C:/Users/RDGFT/Desktop/out.txt");
           FileChannel fcin = fin.getChannel();
           FileChannel fcout = fout.getChannel();
           ByteBuffer buffer = ByteBuffer.allocateDirect( 1024 );
           while (true) {
              buffer.clear();
              int r = fcin.read( buffer );
              if (r==-1) {
                break;
              }
              buffer.flip();
              fcout.write( buffer );
           }
       }
      
     
      
       public static void copyByBufferStream() throws Exception{
       FileInputStream fin = new FileInputStream("C:/Users/RDGFT/Desktop/in.txt" );
       BufferedInputStream bin = new BufferedInputStream(fin);
       FileOutputStream fout = new FileOutputStream("C:/Users/RDGFT/Desktop/out.txt");
       BufferedOutputStream bout = new BufferedOutputStream(fout);
      
       int r=0;
       byte ch[] = new byte[1024];
       while((r=bin.read(ch))!=-1){
       fout.write(ch);
       }
      
       fin.close();
       fout.close();
       }
      

      
       public static void copyByByteArrayFileStream() throws Exception{
       FileInputStream fin = new FileInputStream("C:/Users/RDGFT/Desktop/in.txt");
       FileOutputStream fout = new FileOutputStream("C:/Users/RDGFT/Desktop/out.txt");
      
       byte[] ch = new byte[1024];
       int r=0;
       while((r=fin.read(ch))!=-1){
       fout.write(ch);
       }
      
       fin.close();
       fout.close();
       }
      
       public static void copyByByteFileStream() throws Exception{
       FileInputStream fin = new FileInputStream("C:/Users/RDGFT/Desktop/in.txt");
       FileOutputStream fout = new FileOutputStream("C:/Users/RDGFT/Desktop/out.txt");
      
       int ch;
       while((ch=fin.read())!=-1){
       fout.write(ch);
       }
      
       fin.close();
       fout.close();
       }
      
      
       public static void main(String[] args)throws Exception {
       long start=0, end =0;
       start = System.currentTimeMillis();
       copyByMapped();
       end = System.currentTimeMillis();
       System.out.println((end-start) + "ms");
      
       start = System.currentTimeMillis();
       copyByChannel();
       end = System.currentTimeMillis();
       System.out.println((end-start) + "ms");
      
       start = System.currentTimeMillis();
       copyByFastChannel();
       end = System.currentTimeMillis();
       System.out.println((end-start) + "ms");
         
       start = System.currentTimeMillis();
       copyByBufferStream();
       end = System.currentTimeMillis();
       System.out.println((end-start) + "ms");
      
       start = System.currentTimeMillis();
       copyByByteArrayFileStream();
       end = System.currentTimeMillis();
       System.out.println((end-start) + "ms");
      
          start = System.currentTimeMillis();
       copyByByteFileStream();
       end = System.currentTimeMillis();
       System.out.println((end-start) + "ms");
       //一次时间为根据个人机器而有所差距
       /**
        * 42ms
316ms
199ms
156ms
227ms
        */
      
       }
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics