Copy a file with FileOutputStream and FileInputStream : FileInputStream « File Input Output « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » File Input Output » FileInputStreamScreenshots 
Copy a file with FileOutputStream and FileInputStream
Copy a file with FileOutputStream and FileInputStream
   

/*
 * Copyright (c) 2004 David Flanagan.  All rights reserved.
 * This code is from the book Java Examples in a Nutshell, 3nd Edition.
 * It is provided AS-IS, WITHOUT ANY WARRANTY either expressed or implied.
 * You may study, use, and modify it for any non-commercial purpose,
 * including teaching and use in open-source projects.
 * You may distribute it non-commercially as long as you retain this notice.
 * For a commercial use license, or to purchase the book, 
 * please visit http://www.davidflanagan.com/javaexamples3.
 */

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * This class is a standalone program to copy a file, and also defines a static
 * copy() method that other programs can use to copy files.
 */
public class FileCopy {
  /** The main() method of the standalone program. Calls copy(). */
  public static void main(String[] args) {
    if (args.length != 2// Check arguments
      System.err.println("Usage: java FileCopy <source> <destination>");
    else {
      // Call copy() to do the copy; display any error messages
      try {
        copy(args[0], args[1]);
      catch (IOException e) {
        System.err.println(e.getMessage());
      }
    }
  }

  /**
   * The static method that actually performs the file copy. Before copying the
   * file, however, it performs a lot of tests to make sure everything is as it
   * should be.
   */
  public static void copy(String from_name, String to_namethrows IOException {
    File from_file = new File(from_name)// Get File objects from Strings
    File to_file = new File(to_name);

    // First make sure the source file exists, is a file, and is readable.
    // These tests are also performed by the FileInputStream constructor
    // which throws a FileNotFoundException if they fail.
    if (!from_file.exists())
      abort("no such source file: " + from_name);
    if (!from_file.isFile())
      abort("can't copy directory: " + from_name);
    if (!from_file.canRead())
      abort("source file is unreadable: " + from_name);

    // If the destination is a directory, use the source file name
    // as the destination file name
    if (to_file.isDirectory())
      to_file = new File(to_file, from_file.getName());

    // If the destination exists, make sure it is a writeable file
    // and ask before overwriting it. If the destination doesn't
    // exist, make sure the directory exists and is writeable.
    if (to_file.exists()) {
      if (!to_file.canWrite())
        abort("destination file is unwriteable: " + to_name);
      // Ask whether to overwrite it
      System.out.print("Overwrite existing file " + to_file.getName() "? (Y/N): ");
      System.out.flush();
      // Get the user's response.
      BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
      String response = in.readLine();
      // Check the response. If not a Yes, abort the copy.
      if (!response.equals("Y"&& !response.equals("y"))
        abort("existing file was not overwritten.");
    else {
      // If file doesn't exist, check if directory exists and is
      // writeable. If getParent() returns null, then the directory is
      // the current dir. so look up the user.dir system property to
      // find out what that is.
      String parent = to_file.getParent()// The destination directory
      if (parent == null// If none, use the current directory
        parent = System.getProperty("user.dir");
      File dir = new File(parent)// Convert it to a file.
      if (!dir.exists())
        abort("destination directory doesn't exist: " + parent);
      if (dir.isFile())
        abort("destination is not a directory: " + parent);
      if (!dir.canWrite())
        abort("destination directory is unwriteable: " + parent);
    }

    // If we've gotten this far, then everything is okay.
    // So we copy the file, a buffer of bytes at a time.
    FileInputStream from null// Stream to read from source
    FileOutputStream to = null// Stream to write to destination
    try {
      from new FileInputStream(from_file)// Create input stream
      to = new FileOutputStream(to_file)// Create output stream
      byte[] buffer = new byte[4096]// To hold file contents
      int bytes_read; // How many bytes in buffer

      // Read a chunk of bytes into the buffer, then write them out,
      // looping until we reach the end of the file (when read() returns
      // -1). Note the combination of assignment and comparison in this
      // while loop. This is a common I/O programming idiom.
      while ((bytes_read = from.read(buffer)) != -1)
        // Read until EOF
        to.write(buffer, 0, bytes_read)// write
    }
    // Always close the streams, even if exceptions were thrown
    finally {
      if (from != null)
        try {
          from.close();
        catch (IOException e) {
          ;
        }
      if (to != null)
        try {
          to.close();
        catch (IOException e) {
          ;
        }
    }
  }

  /** A convenience method to throw an exception */
  private static void abort(String msgthrows IOException {
    throw new IOException("FileCopy: " + msg);
  }
}

   
    
    
  
Related examples in the same category
1. Copy a file
2. Read file using FileInputStream
3. Skip n bytes while reading the file using FileInputStream
4. Copying One File to Another
5. Copying One File to Another with FileChannel
6. Read bytes and display their hexadecimal values.
7. Reading a File into a Byte Array: reads the entire contents of a file into a byte array
8. Use Java NIO to Copy File
9. Read one byte from a file
10. Read file character by character
11. Count characters with FileInputStream
12. Read and copy with FileInputStream and FileOutputStream
13. Read file in byte array using FileInputStream
14. Display file contents in hexadecimal
15. Resettable File InputStream
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.