Advertisement
Guest User

Untitled

a guest
Dec 8th, 2011
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.26 KB | None | 0 0
  1. import java.io.*;
  2. import java.nio.channels.FileChannel;
  3. import java.util.Random;
  4.  
  5. import static java.nio.channels.Channels.*;
  6.  
  7. public class MainClass {
  8.  
  9.     static void genFile(File file){
  10.         try {
  11.             System.out.println("Generating file");
  12.             OutputStream stream = new FileOutputStream(file);
  13.             long size = 400000000;
  14.             long position = 0;
  15.             byte[] buffer = new byte[10*1024];
  16.             Random rnd = new Random();
  17.             for (int i = 0; i < buffer.length; i++){
  18.                 buffer[i] = (byte)rnd.nextInt();
  19.             }
  20.             while (position<size){
  21.                 stream.write(buffer);
  22.                 position += buffer.length;
  23.             }
  24.             stream.close();
  25.             System.out.println("Done");
  26.         } catch (IOException e) {
  27.             e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
  28.         }
  29.     }
  30.  
  31.     static void copyFileBlocks(File input, File output) throws IOException{
  32.         InputStream inp = new FileInputStream(input);
  33.         OutputStream outp = new FileOutputStream(output);
  34.         byte[] buffer = new byte[128*1024];
  35.         while (inp.available()>0){
  36.             int read = inp.read(buffer);
  37.             outp.write(buffer,0,read);
  38.         }
  39.         inp.close();
  40.         outp.close();
  41.     }
  42.  
  43.     static void copyFileNIO(File input, File output) throws IOException{
  44.         FileInputStream inp = new FileInputStream(input);
  45.         FileOutputStream outp = new FileOutputStream(output);
  46.         FileChannel reader = inp.getChannel();
  47.         FileChannel writer = outp.getChannel();
  48.         long position = 0;
  49.         while (position<reader.size()){
  50.             position+=reader.transferTo(position, 128*1024, writer);
  51.         }
  52.         inp.close();
  53.         outp.close();
  54.     }
  55.  
  56.     static void copyFileNIOSingle(File input, File output) throws IOException{
  57.         FileInputStream inp = new FileInputStream(input);
  58.         FileOutputStream outp = new FileOutputStream(output);
  59.         FileChannel reader = inp.getChannel();
  60.         FileChannel writer = outp.getChannel();
  61.         reader.transferTo(0, reader.size(), writer);
  62.         inp.close();
  63.         outp.close();
  64.     }
  65.  
  66.     public static void main(String[] argv){
  67.         try {
  68.             genFile(new File("input.dat"));
  69.             long tick;
  70.  
  71.             for (int i = 0; i < 5; i++){
  72.                 tick = System.currentTimeMillis();
  73.                 copyFileBlocks(new File("input.dat"), new File("output1.dat"));
  74.                 System.out.println("copyFileBlocks took "+(System.currentTimeMillis() - tick));
  75.  
  76.                 tick = System.currentTimeMillis();
  77.                 copyFileNIO(new File("input.dat"), new File("output2.dat"));
  78.                 System.out.println("copyFileNIO took "+(System.currentTimeMillis() - tick));
  79.  
  80.                 tick = System.currentTimeMillis();
  81.                 copyFileNIOSingle(new File("input.dat"), new File("output3.dat"));
  82.                 System.out.println("copyFileNIOSingle took "+(System.currentTimeMillis() - tick));
  83.             }
  84.         } catch (IOException e) {
  85.             e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
  86.         }
  87.  
  88.     }
  89. }
  90.  
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement