Android Open Source - JiangHomeStyle_Android_Phone File Operation Tools






From Project

Back to project page JiangHomeStyle_Android_Phone.

License

The source code is released under:

Apache License

If you think the Android project JiangHomeStyle_Android_Phone 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.cidesign.jianghomestylephone.tools;
//  w  w  w.  j ava2  s .  c  om
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

import android.content.Context;
import android.os.Environment;
import android.os.StatFs;
import android.util.Log;

public class FileOperationTools
{
  private static final String TAG = FileOperationTools.class.getSimpleName();

  /**
   * ???SD???????? true?????? false:??????
   * 
   * @return
   */
  public static boolean hasSdcard()
  {
    String status = Environment.getExternalStorageState();

    if (status.equals(Environment.MEDIA_MOUNTED))
    {
      return true;
    }
    else
    {
      return false;
    }
  }

  /**
   * 
   * @param path
   *            ???????
   */
  public static void isExist(String path)
  {
    File file = new File(path);
    // ??????????????,?????????????????
    if (!file.exists())
    {
      file.mkdir();
    }
  }

  /**
   * ???SD???????????
   * 
   * @return
   */
  public long getSDFreeSize()
  {
    // ???SD???????
    File path = Environment.getExternalStorageDirectory();
    StatFs sf = new StatFs(path.getPath());
    // ???????????????(Byte)
    long blockSize = sf.getBlockSize();
    // ???????????????
    long freeBlocks = sf.getAvailableBlocks();
    // ????SD????????
    // return freeBlocks * blockSize; //???Byte
    // return (freeBlocks * blockSize)/1024; //???KB
    return (freeBlocks * blockSize) / 1024 / 1024; // ???MB
  }

  /**
   * SD????????
   * 
   * @return
   */
  public long getSDAllSize()
  {
    // ???SD???????
    File path = Environment.getExternalStorageDirectory();
    StatFs sf = new StatFs(path.getPath());
    // ???????????????(Byte)
    long blockSize = sf.getBlockSize();
    // ??????????????
    long allBlocks = sf.getBlockCount();
    // ????SD?????
    // return allBlocks * blockSize; //???Byte
    // return (allBlocks * blockSize)/1024; //???KB
    return (allBlocks * blockSize) / 1024 / 1024; // ???MB
  }

  private static void createDirectory(String directory, String subDirectory)
  {
    String dir[];
    File fl = new File(directory);
    try
    {
      if (subDirectory == "" && fl.exists() != true)
        fl.mkdir();
      else if (subDirectory != "")
      {
        dir = subDirectory.replace('\\', '/').split("/");
        for (int i = 0; i < dir.length; i++)
        {
          File subFile = new File(directory + File.separator + dir[i]);
          if (subFile.exists() == false)
            subFile.mkdir();
          directory += File.separator + dir[i];
        }
      }
    }
    catch (Exception ex)
    {
      System.out.println(ex.getMessage());
    }
  }

  public static void unZip(String zipFileName, String outputDirectory)
  {
    org.apache.tools.zip.ZipFile zipFile = null;
    try
    {
      zipFile = new org.apache.tools.zip.ZipFile(zipFileName);
      java.util.Enumeration e = zipFile.getEntries();
      org.apache.tools.zip.ZipEntry zipEntry = null;
      createDirectory(outputDirectory, "");
      while (e.hasMoreElements())
      {
        zipEntry = (org.apache.tools.zip.ZipEntry) e.nextElement();
        if (zipEntry.isDirectory())
        {
          String name = zipEntry.getName();
          name = name.substring(0, name.length() - 1);
          File f = new File(outputDirectory + File.separator + name);
          f.mkdir();
        }
        else
        {
          String fileName = zipEntry.getName();
          fileName = fileName.replace('\\', '/');
          if (fileName.indexOf("/") != -1)
          {
            createDirectory(outputDirectory, fileName.substring(0, fileName.lastIndexOf("/")));
            fileName = fileName.substring(fileName.lastIndexOf("/") + 1, fileName.length());
          }

          File f = new File(outputDirectory + File.separator + zipEntry.getName());

          f.createNewFile();
          InputStream in = zipFile.getInputStream(zipEntry);
          FileOutputStream out = new FileOutputStream(f);

          byte[] by = new byte[1024];
          int c;
          while ((c = in.read(by)) != -1)
          {
            out.write(by, 0, c);
          }
          out.close();
          in.close();
        }
      }
    }
    catch (Exception ex)
    {
      System.out.println(ex.getMessage());
    }
    finally
    {
      if (zipFile != null)
      {
        try
        {
          zipFile.close();
        }
        catch (IOException e)
        {
          e.printStackTrace();
        }
      }
    }
  }

  public static void copyAssets(Context ctx)
  {
    String[] files;
    try
    {
      files = ctx.getResources().getAssets().list("");
    }
    catch (IOException e1)
    {
      return;
    }

    for (int i = 0; i < files.length; i++)
    {
      try
      {
        String fileName = files[i];
        if (fileName.compareTo("images") == 0 || fileName.compareTo("sounds") == 0 || fileName.compareTo("webkit") == 0
            || fileName.compareTo("filelist.xml") == 0 || fileName.compareTo("bg1.jpg") == 0
            || fileName.compareTo("bg2.jpg") == 0 || fileName.compareTo("bg3.jpg") == 0
            || fileName.compareTo("bg4.jpg") == 0 || fileName.compareTo("bg5.jpg") == 0)
        {
          continue;
        }

        File outFile = new File(StorageUtils.FILE_TEMP_ROOT, fileName);
        if (outFile.exists())
          continue;

        InputStream in = ctx.getAssets().open(fileName);
        OutputStream out = new FileOutputStream(outFile);

        // Transfer bytes from in to out
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0)
        {
          out.write(buf, 0, len);
        }

        in.close();
        out.close();
      }
      catch (FileNotFoundException e)
      {
        e.printStackTrace();
      }
      catch (IOException e)
      {
        e.printStackTrace();
      }
    }
  }
}




Java Source Code List

com.cidesign.jianghomestylephone.DetailActivity.java
com.cidesign.jianghomestylephone.JiangActivity.java
com.cidesign.jianghomestylephone.MainActivity.java
com.cidesign.jianghomestylephone.SplashActivity.java
com.cidesign.jianghomestylephone.adapter.CommunityViewpagerAdapter.java
com.cidesign.jianghomestylephone.adapter.HumanityViewpagerAdapter.java
com.cidesign.jianghomestylephone.adapter.LandscapeViewpagerAdapter.java
com.cidesign.jianghomestylephone.adapter.LayoutCaculateAdapter.java
com.cidesign.jianghomestylephone.adapter.StoryViewpagerAdapter.java
com.cidesign.jianghomestylephone.async.AsyncDownTask.java
com.cidesign.jianghomestylephone.async.AsyncInitCommunityData.java
com.cidesign.jianghomestylephone.async.AsyncInitData.java
com.cidesign.jianghomestylephone.async.AsyncInitHomeData.java
com.cidesign.jianghomestylephone.async.AsyncInitHumanityData.java
com.cidesign.jianghomestylephone.async.AsyncInitLandscapeData.java
com.cidesign.jianghomestylephone.async.AsyncInitStoryData.java
com.cidesign.jianghomestylephone.db.DatabaseConfigUtil.java
com.cidesign.jianghomestylephone.db.DatabaseHelper.java
com.cidesign.jianghomestylephone.entity.ArticleEntity.java
com.cidesign.jianghomestylephone.entity.FileListEntity.java
com.cidesign.jianghomestylephone.entity.LayoutEntity.java
com.cidesign.jianghomestylephone.entity.RelativeLayoutRulesEntity.java
com.cidesign.jianghomestylephone.http.ArticalOperation.java
com.cidesign.jianghomestylephone.http.DownLoadThread.java
com.cidesign.jianghomestylephone.service.DownloadService.java
com.cidesign.jianghomestylephone.tools.CategoryDataLoadingLogic.java
com.cidesign.jianghomestylephone.tools.FileOperationTools.java
com.cidesign.jianghomestylephone.tools.JiangCategory.java
com.cidesign.jianghomestylephone.tools.LayoutMarginSetting.java
com.cidesign.jianghomestylephone.tools.LoadingDataFromDB.java
com.cidesign.jianghomestylephone.tools.LoadingImageTools.java
com.cidesign.jianghomestylephone.tools.MD5Tools.java
com.cidesign.jianghomestylephone.tools.StorageUtils.java
com.cidesign.jianghomestylephone.tools.TimeTools.java
com.cidesign.jianghomestylephone.tools.WidgetCache.java
com.cidesign.jianghomestylephone.tools.XmlParseTools.java
com.cidesign.jianghomestylephone.version.NetworkTool.java
com.cidesign.jianghomestylephone.version.VersionConfig.java
com.cidesign.jianghomestylephone.version.VersionUpdate.java
com.cidesign.jianghomestylephone.widget.CommunityRelativeLayout.java
com.cidesign.jianghomestylephone.widget.CustomScrollView.java
com.cidesign.jianghomestylephone.widget.HScrollViewTouchLogic.java
com.cidesign.jianghomestylephone.widget.HumanityRelativeLayout.java
com.cidesign.jianghomestylephone.widget.LandscapeRelativeLayout.java
com.cidesign.jianghomestylephone.widget.PopMenu.java
com.cidesign.jianghomestylephone.widget.StoryRelativeLayout.java
org.apache.tools.zip.AbstractUnicodeExtraField.java
org.apache.tools.zip.AsiExtraField.java
org.apache.tools.zip.CentralDirectoryParsingZipExtraField.java
org.apache.tools.zip.ExtraFieldUtils.java
org.apache.tools.zip.FallbackZipEncoding.java
org.apache.tools.zip.GeneralPurposeBit.java
org.apache.tools.zip.JarMarker.java
org.apache.tools.zip.NioZipEncoding.java
org.apache.tools.zip.Simple8BitZipEncoding.java
org.apache.tools.zip.UnicodeCommentExtraField.java
org.apache.tools.zip.UnicodePathExtraField.java
org.apache.tools.zip.UnixStat.java
org.apache.tools.zip.UnparseableExtraFieldData.java
org.apache.tools.zip.UnrecognizedExtraField.java
org.apache.tools.zip.UnsupportedZipFeatureException.java
org.apache.tools.zip.Zip64ExtendedInformationExtraField.java
org.apache.tools.zip.Zip64Mode.java
org.apache.tools.zip.Zip64RequiredException.java
org.apache.tools.zip.ZipConstants.java
org.apache.tools.zip.ZipEightByteInteger.java
org.apache.tools.zip.ZipEncodingHelper.java
org.apache.tools.zip.ZipEncoding.java
org.apache.tools.zip.ZipEntry.java
org.apache.tools.zip.ZipExtraField.java
org.apache.tools.zip.ZipFile.java
org.apache.tools.zip.ZipLong.java
org.apache.tools.zip.ZipOutputStream.java
org.apache.tools.zip.ZipShort.java
org.apache.tools.zip.ZipUtil.java