Advertisement
Guest User

Untitled

a guest
Dec 21st, 2014
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5.  
  6. public class FileCopyNoBuffer{
  7. public static void main(String[] args){
  8. FileInputStream in = null;
  9. FileOutputStream out = null;
  10. Long startTime, elapsedTime;
  11.  
  12. //String inFileStr = "C:\project\books\java-Preparation\main_docs\practice.jpg";
  13. //String outFileStr = "C:\project\books\java-Preparation\main_docs\practice_out.jpg";
  14. String fileStr = "C:\project\books\java-Preparation\main_docs\practice.jpg";
  15.  
  16. File file = new File(fileStr);
  17. System.out.println("File size before - r/w is: " + file.length() + " bytes");
  18. try{
  19. in = new FileInputStream(fileStr);
  20. out = new FileOutputStream(fileStr);
  21.  
  22. startTime = System.nanoTime();
  23. int byteRead;
  24.  
  25. while((byteRead = in.read()) != -1){
  26. out.write(byteRead);
  27. }
  28.  
  29. elapsedTime = System.nanoTime() - startTime;
  30. System.out.println("Elapsed Time is: " + (elapsedTime/1000000.0) + " msec");
  31. System.out.println("File size after - r/w is: " + file.length() + " bytes");
  32.  
  33. }catch(IOException ex){
  34. ex.printStackTrace();
  35. }finally{
  36. try{
  37. if(in != null){
  38. in.close();
  39. }
  40. if(out != null){
  41. out.close();
  42. }
  43. }catch(IOException ex){
  44. ex.printStackTrace();
  45. }
  46. }
  47.  
  48.  
  49. }
  50. }
  51.  
  52. File size before - r/w is: 1115512 bytes
  53. Elapsed Time is: 0.040711 msec
  54. File size after - r/w is: 0 bytes
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement