Copy a file from one location to another. : Location « Hardware « Android






Copy a file from one location to another.

 
//package bander.fileman.util;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

/** Utility class containing File-related helper functions. */
public class FileUtils {

  /**
   * Copy a file from one location to another.
   * 
   * @param sourceFile
   *            File to copy from.
   * @param destFile
   *            File to copy to.
   * @return True if successful, false otherwise.
   * @throws IOException
   */
  public static Boolean copyFile(File sourceFile, File destFile)
      throws IOException {
    if (!destFile.exists()) {
      destFile.createNewFile();

      FileChannel source = null;
      FileChannel destination = null;
      try {
        source = new FileInputStream(sourceFile).getChannel();
        destination = new FileOutputStream(destFile).getChannel();
        destination.transferFrom(source, 0, source.size());
      } finally {
        if (source != null)
          source.close();
        if (destination != null)
          destination.close();
      }
      return true;
    }
    return false;
  }

  /**
   * Read a text file into a String.
   * 
   * @param file
   *            File to read (will not seek, so things like /proc files are
   *            OK).
   * @return The contents of the file as a String.
   * @throws IOException
   */
  public static String readTextFile(File file) throws IOException {
    byte[] buffer = new byte[(int) file.length()];
    BufferedInputStream stream = new BufferedInputStream(
        new FileInputStream(file));
    stream.read(buffer);
    stream.close();

    return new String(buffer);
  }

}

   
  








Related examples in the same category

1.Location service and LocationManager
2.Location service
3.Using LocationManager
4.My location
5.Display GEO location
6.Using location service for the weather
7.Using Intent to go to a geo location
8.Location based service
9.My location and Google Map
10.Custom Location Overlay
11.Get my location
12.Geo location and Google Map
13.Location Tracking
14.A light pool of objects that can be resused to avoid allocation.
15.extends android.location.Location
16.Geo Location Util
17.C:\Java_Dev\WEB\dev\android\weatherforecastsystem-read-only\com\hci\pwf\LocationUtil.java
18.upload Data with Geo location
19.LocationManager.GPS_PROVIDER
20.Location util