Android Open Source - WebImageView File Utils






From Project

Back to project page WebImageView.

License

The source code is released under:

MIT License

If you think the Android project WebImageView 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 com.rsv.utils;
//  w  w  w. j  av a  2  s .  co m
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.nio.channels.FileChannel;

public final class FileUtils {

  public static final Object[] DATA_LOCK = new Object[0];

  /**
   * Copy file, return true on success, false on failure.
   * 
   * @param src
   * @param dst
   * @return
   */
  public static boolean copyFile(final File src, final File dst) {
    boolean result = false;
    FileChannel inChannel = null;
    FileChannel outChannel = null;
    synchronized (DATA_LOCK) {
      try {
        inChannel = new FileInputStream(src).getChannel();
        outChannel = new FileOutputStream(dst).getChannel();
        inChannel.transferTo(0, inChannel.size(), outChannel);
        result = true;
      } catch (IOException e) {

      } finally {
        if (inChannel != null && inChannel.isOpen()) {
          try {
            inChannel.close();
          } catch (IOException e) {
            // ignore
          }
        }
        if (outChannel != null && outChannel.isOpen()) {
          try {
            outChannel.close();
          } catch (IOException e) {
            // ignore
          }
        }
      }
    }
    return result;
  }

  /**
   * Replace entire File with contents of String, return true on success,
   * false on failure.
   * 
   * @param fileContents
   * @param file
   * @return
   */
  public static boolean writeStringAsFile(final String fileContents, final File file) {
    boolean result = false;
    try {
      synchronized (DATA_LOCK) {
        if (file != null) {
          file.createNewFile(); // ok if returns false, overwrite
          Writer out = new BufferedWriter(new FileWriter(file), 1024);
          out.write(fileContents);
          out.close();
          result = true;
        }
      }
    } catch (IOException e) {
      LogUtils.logException(e);
    }
    return result;
  }

  /**
   * Append String to end of File, return true on success, false on failure.
   * 
   * @param appendContents
   * @param file
   * @return
   */
  public static boolean appendStringToFile(final String appendContents, final File file) {
    boolean result = false;
    try {
      synchronized (DATA_LOCK) {
        if ((file != null) && file.canWrite()) {
          file.createNewFile(); // ok if returns false, overwrite
          Writer out = new BufferedWriter(new FileWriter(file, true), 1024);
          out.write(appendContents);
          out.close();
          result = true;
        }
      }
    } catch (IOException e) {
      LogUtils.logException(e);
    }
    return result;
  }

  /**
   * Read file as String, return null if file is not present or not readable.
   * 
   * @param file
   * @return
   */
  public static String readFileAsString(final File file) {
    StringBuilder sb = null;
    try {
      synchronized (DATA_LOCK) {
        if ((file != null) && file.canRead()) {
          sb = new StringBuilder();
          String line = null;
          BufferedReader in = new BufferedReader(new FileReader(file), 1024);
          while ((line = in.readLine()) != null) {
            sb.append(line + System.getProperty("line.separator"));
          }
          in.close();
        }
      }
    } catch (IOException e) {
      LogUtils.logException(e);
    }
    if (sb != null) {
      return sb.toString();
    }
    return null;
  }

  /**
   * Call sync on a FileOutputStream to ensure it is written to disk
   * immediately (write, flush, close, etc, don't guarantee physical disk
   * write on buffered file systems).
   * 
   * @param stream
   * @return
   */
  public static boolean syncStream(FileOutputStream fos) {
    try {
      if (fos != null) {
        fos.getFD().sync();
      }
      return true;
    } catch (IOException e) {
      LogUtils.logException(e);
    }
    return false;
  }
}




Java Source Code List

com.rsv.cache.BaseFileCache.java
com.rsv.cache.BaseMemCache.java
com.rsv.cache.LmtSizeMemCache.java
com.rsv.cache.LmtSpaceFileCache.java
com.rsv.comp.IProgressListener.java
com.rsv.comp.ImageLoader.java
com.rsv.config.ConfigReader.java
com.rsv.config.Constants.java
com.rsv.utils.FileUtils.java
com.rsv.utils.HttpClientUtils.java
com.rsv.utils.IOUtils.java
com.rsv.utils.ImgUtils.java
com.rsv.utils.LogUtils.java
com.rsv.utils.OSUtils.java
com.rsv.utils.StorageUtils.java
com.rsv.widget.WebImageView.java