Android Open Source - javocsoft-toolbox File Helper






From Project

Back to project page javocsoft-toolbox.

License

The source code is released under:

GNU General Public License

If you think the Android project javocsoft-toolbox listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package es.javocsoft.android.lib.toolbox.encoding;
// ww w . j a v  a 2  s.com
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.MessageFormat;

/**
 * Utilities for File manipulation. More information about this class is
 * available from <a target="_top" href=
 * "http://ostermiller.org/utils/FileHelper.html">ostermiller.org</a>.
 * 
 * @author Stephen Ostermiller
 *         http://ostermiller.org/contact.pl?regarding=Java+Utilities
 * @since ostermillerutils 1.00.00
 */
public class FileHelper
{

  /**
   * Move a file from one location to another. An attempt is made to rename
   * the file and if that fails, the file is copied and the old file deleted.
   * 
   * If the destination file already exists, an exception will be thrown.
   * 
   * @param from
   *            file which should be moved.
   * @param to
   *            desired destination of the file.
   * @throws java.io.IOException
   *             if an error occurs.
   * 
   * @since ostermillerutils 1.00.00
   */
  public static void move(File from, File to) throws IOException
  {
    move(from, to, false);
  }

  /**
   * Move a file from one location to another. An attempt is made to rename
   * the file and if that fails, the file is copied and the old file deleted.
   * 
   * @param from
   *            file which should be moved.
   * @param to
   *            desired destination of the file.
   * @param overwrite
   *            If false, an exception will be thrown rather than overwrite a
   *            file.
   * @throws java.io.IOException
   *             if an error occurs.
   * 
   * @since ostermillerutils 1.00.00
   */
  public static void move(File from, File to, boolean overwrite) throws IOException
  {
    if (to.exists())
    {
      if (overwrite)
      {
        if (!to.delete())
        {
          throw new IOException(MessageFormat.format("deleteerror %s", (Object[]) new String[]
          { to.toString() }));
        }
      } else
      {
        throw new IOException(MessageFormat.format("alreadyexistserror %s", (Object[]) new String[]
        { to.toString() }));
      }
    }

    if (from.renameTo(to))
      return;

    InputStream in = null;
    OutputStream out = null;
    try
    {
      in = new FileInputStream(from);
      out = new FileOutputStream(to);
      copy(in, out);
      in.close();
      in = null;
      out.flush();
      out.close();
      out = null;
      if (!from.delete())
      {
        throw new IOException(MessageFormat.format("deleteoriginalerror %s", (Object[]) new String[]
        { from.toString(), to.toString() }));
      }
    } finally
    {
      if (in != null)
      {
        in.close();
        in = null;
      }
      if (out != null)
      {
        out.flush();
        out.close();
        out = null;
      }
    }
  }

  /**
   * Buffer size when reading from input stream.
   * 
   * @since ostermillerutils 1.00.00
   */
  private final static int BUFFER_SIZE = 1024;

  /**
   * Copy the data from the input stream to the output stream.
   * 
   * @param in
   *            data source
   * @param out
   *            data destination
   * @throws java.io.IOException
   *             in an input or output error occurs
   * 
   * @since ostermillerutils 1.00.00
   */
  private static void copy(InputStream in, OutputStream out) throws IOException
  {
    byte[] buffer = new byte[BUFFER_SIZE];
    int read;
    while ((read = in.read(buffer)) != -1)
    {
      out.write(buffer, 0, read);
    }
  }
}




Java Source Code List

es.javocsoft.android.lib.toolbox.ToolBox.java
es.javocsoft.android.lib.toolbox.ads.AdBase.java
es.javocsoft.android.lib.toolbox.ads.AdFragment.java
es.javocsoft.android.lib.toolbox.ads.AdInterstitial.java
es.javocsoft.android.lib.toolbox.ads.InterstitialAdsListener.java
es.javocsoft.android.lib.toolbox.analytics.CampaignInfo.java
es.javocsoft.android.lib.toolbox.analytics.CustomCampaignTrackingReceiver.java
es.javocsoft.android.lib.toolbox.encoding.Base64DecodingException.java
es.javocsoft.android.lib.toolbox.encoding.Base64.java
es.javocsoft.android.lib.toolbox.encoding.FileHelper.java
es.javocsoft.android.lib.toolbox.facebook.FacebookLoginFragment.java
es.javocsoft.android.lib.toolbox.facebook.FacebookShareFragment.java
es.javocsoft.android.lib.toolbox.facebook.FbTools.java
es.javocsoft.android.lib.toolbox.facebook.beans.AppRequestBean.java
es.javocsoft.android.lib.toolbox.facebook.callback.OnAppRequestCancelledActionCallback.java
es.javocsoft.android.lib.toolbox.facebook.callback.OnAppRequestDeleteSuccessActionCallback.java
es.javocsoft.android.lib.toolbox.facebook.callback.OnAppRequestFailActionCallback.java
es.javocsoft.android.lib.toolbox.facebook.callback.OnAppRequestReceivedActionCallback.java
es.javocsoft.android.lib.toolbox.facebook.callback.OnAppRequestReceivedErrorActionCallback.java
es.javocsoft.android.lib.toolbox.facebook.callback.OnAppRequestSuccessActionCallback.java
es.javocsoft.android.lib.toolbox.facebook.callback.OnLoginActionCallback.java
es.javocsoft.android.lib.toolbox.facebook.callback.OnLogoutActionCallback.java
es.javocsoft.android.lib.toolbox.facebook.callback.OnShareCancelledActionCallback.java
es.javocsoft.android.lib.toolbox.facebook.callback.OnShareFailActionCallback.java
es.javocsoft.android.lib.toolbox.facebook.callback.OnShareSuccessActionCallback.java
es.javocsoft.android.lib.toolbox.facebook.exception.FBException.java
es.javocsoft.android.lib.toolbox.facebook.exception.FBSessionException.java
es.javocsoft.android.lib.toolbox.gcm.EnvironmentType.java
es.javocsoft.android.lib.toolbox.gcm.NotificationModule.java
es.javocsoft.android.lib.toolbox.gcm.core.CustomGCMBroadcastReceiver.java
es.javocsoft.android.lib.toolbox.gcm.core.CustomNotificationReceiver.java
es.javocsoft.android.lib.toolbox.gcm.core.GCMIntentService.java
es.javocsoft.android.lib.toolbox.gcm.core.beans.GCMDeliveryResponse.java
es.javocsoft.android.lib.toolbox.gcm.core.beans.GCMDeliveryResultItem.java
es.javocsoft.android.lib.toolbox.gcm.core.beans.GCMMessage.java
es.javocsoft.android.lib.toolbox.gcm.exception.GCMException.java
es.javocsoft.android.lib.toolbox.gcm.send.GCMHttpDelivery.java
es.javocsoft.android.lib.toolbox.io.IOUtils.java
es.javocsoft.android.lib.toolbox.io.Unzipper.java
es.javocsoft.android.lib.toolbox.media.MediaScannerNotifier.java
es.javocsoft.android.lib.toolbox.net.HttpOperations.java
es.javocsoft.android.lib.toolbox.sms.cmt.CMTInfoHelper.java
es.javocsoft.android.lib.toolbox.sms.cmt.CMTShortNumberInformation.java
es.javocsoft.android.lib.toolbox.sms.observer.SMSObserver.java