文件操作类

推荐安卓开发神器(里面有各种UI特效和android代码库实例)

import android.graphics.Bitmap;

import android.graphics.Bitmap.CompressFormat;

import android.text.TextUtils;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.OutputStream;

import java.io.PrintStream;

import java.nio.channels.FileChannel;

import java.nio.channels.WritableByteChannel;

 

public class FileUtil

{

  public static File copyFile(String paramString1, String paramString2)

  {

    File localFile;

    try

    {

      Object localObject1 = new File(paramString1);

      if (!((File)localObject1).exists())

      {

        localFile = null;

      }

      else

      {

        localFile = new File(paramString2);

        if (!localFile.exists())

          localFile.createNewFile();

        localObject1 = new FileInputStream((File)localObject1);

        Object localObject2 = new FileOutputStream(localFile);

        localObject1 = ((FileInputStream)localObject1).getChannel();

        localObject2 = ((FileOutputStream)localObject2).getChannel();

        while (true)

        {

          if (((FileChannel)localObject1).position() == ((FileChannel)localObject1).size())

          {

            ((FileChannel)localObject1).close();

            ((FileChannel)localObject2).close();

            localFile = localFile;

            break label170;

          }

          if (((FileChannel)localObject1).size() - ((FileChannel)localObject1).position() >= 20971520L)

            break;

          int i = (int)(((FileChannel)localObject1).size() - ((FileChannel)localObject1).position());

          ((FileChannel)localObject1).transferTo(((FileChannel)localObject1).position(), i, (WritableByteChannel)localObject2);

          ((FileChannel)localObject1).position(((FileChannel)localObject1).position() + i);

        }

      }

    }

    catch (Exception localException)

    {

      while (true)

      {

        localFile = null;

        break;

        int j = 20971520;

      }

    }

    label170: return (File)(File)localFile;

  }

 

  public static boolean copyFile(String paramString, FileOutputStream paramFileOutputStream)

  {

    int j;

    try

    {

      File localFile = new File(paramString);

      int i;

      if (!localFile.exists())

      {

        i = 0;

      }

      else

      {

        FileChannel localFileChannel2 = new FileInputStream(i).getChannel();

        FileChannel localFileChannel1 = paramFileOutputStream.getChannel();

        while (true)

        {

          if (localFileChannel2.position() == localFileChannel2.size())

          {

            localFileChannel2.close();

            localFileChannel1.close();

            j = 1;

            break label133;

          }

          if (localFileChannel2.size() - localFileChannel2.position() >= 20971520L)

            break;

          int k = (int)(localFileChannel2.size() - localFileChannel2.position());

          localFileChannel2.transferTo(localFileChannel2.position(), k, j);

          localFileChannel2.position(localFileChannel2.position() + k);

        }

      }

    }

    catch (Exception localException)

    {

      while (true)

      {

        j = 0;

        break;

        int m = 20971520;

      }

    }

    label133: return j;

  }

 

  public static boolean deleteDirectory(File paramFile)

  {

    if ((paramFile != null) && (paramFile.isDirectory()))

    {

      File[] arrayOfFile = paramFile.listFiles();

      int j = arrayOfFile.length;

      int i = 0;

      while (true)

        if (i >= j)

        {

          if (paramFile.delete())

            i = 1;

          else

            i = 0;

        }

        else

        {

          if (!arrayOfFile[i].isDirectory())

          {

            if (!arrayOfFile[i].delete())

            {

              i = 0;

              break;

            }

          }

          else

            if (!deleteDirectory(arrayOfFile[i]))

              break label81;

          i++;

          continue;

          label81: i = 0;

        }

      return i;

    }

    throw new IllegalArgumentException("Argument " + paramFile + " is not a directory. ");

  }

 

  public static boolean deleteDirectory(String paramString)

  {

    return deleteDirectory(new File(paramString));

  }

 

  public static boolean emptyDirectory(File paramFile)

  {

    File[] arrayOfFile = paramFile.listFiles();

    int i = 1;

    for (int j = 0; ; j++)

    {

      if (j >= arrayOfFile.length)

        return i;

      if (arrayOfFile[j].delete())

        continue;

      i = 0;

    }

  }

 

  public static boolean emptyDirectory(String paramString)

  {

    return emptyDirectory(new File(paramString));

  }

 

  public static byte[] getFileData(String paramString)

  {

    Object localObject;

    if (TextUtils.isEmpty(paramString))

      localObject = null;

    while (true)

    {

      return localObject;

      localObject = new File(paramString);

      if (!((File)localObject).exists())

      {

        localObject = null;

        continue;

      }

      try

      {

        FileInputStream localFileInputStream = new FileInputStream((File)localObject);

        localObject = new byte[localFileInputStream.available()];

        Exception localException2;

        try

        {

          localFileInputStream.read(localObject, 0, localObject.length);

          localFileInputStream.close();

        }

        catch (Exception localException2)

        {

          localObject = localObject;

          localException2 = localException1;

        }

        localException2.printStackTrace();

        localObject = localObject;

      }

      catch (Exception localException3)

      {

        while (true)

          localObject = null;

      }

    }

  }

 

  public static String getFileName(String paramString)

  {

    return new File(paramString).getName();

  }

 

  public static String getFilePath(String paramString)

  {

    return new File(paramString).getAbsolutePath();

  }

 

  public static long getFileSize(String paramString)

  {

    return new File(paramString).length();

  }

 

  public static String getFileType(File paramFile)

  {

    return getTypePart(paramFile.getName());

  }

 

  public static String getNamePart(String paramString)

  {

    int i = getPathLsatIndex(paramString);

    int k = paramString.length();

    String str;

    if (i != -1)

    {

      if (i != k - 1)

      {

        str = paramString.substring(i + 1);

      }

      else

      {

        int j = getPathLsatIndex(paramString, str - 1);

        if (j != -1)

          str = paramString.substring(j + 1, str);

        else if (k != 1)

          str = paramString.substring(0, str);

        else

          str = paramString;

      }

    }

    else

      str = paramString;

    return str;

  }

 

  public static String getNamePartWithoutType(String paramString)

  {

    String str1 = getNamePart(paramString);

    String str2 = getTypePart(paramString);

    return str1.substring(0, str1.length() - (1 + str2.length()));

  }

 

  public static int getPathIndex(String paramString)

  {

    int i = paramString.indexOf('/');

    if (i == -1)

      i = paramString.indexOf('\\');

    return i;

  }

 

  public static int getPathIndex(String paramString, int paramInt)

  {

    int i = paramString.indexOf('/', paramInt);

    if (i == -1)

      i = paramString.indexOf('\\', paramInt);

    return i;

  }

 

  public static int getPathLsatIndex(String paramString)

  {

    int i = paramString.lastIndexOf('/');

    if (i == -1)

      i = paramString.lastIndexOf('\\');

    return i;

  }

 

  public static int getPathLsatIndex(String paramString, int paramInt)

  {

    int i = paramString.lastIndexOf('/', paramInt);

    if (i == -1)

      i = paramString.lastIndexOf('\\', paramInt);

    return i;

  }

 

  public static String getPathPart(String paramString)

  {

    int k = getPathLsatIndex(paramString);

    int i = paramString.length();

    String str2;

    if (k != -1)

    {

      if (k != i - 1)

      {

        String str1 = paramString.substring(0, k);

      }

      else

      {

        int j = getPathLsatIndex(paramString, k - 1);

        if (j != -1)

          str2 = paramString.substring(0, j);

        else

          str2 = "";

      }

    }

    else

      str2 = "";

    return str2;

  }

 

  public static String getSubpath(String paramString1, String paramString2)

  {

    int i = paramString2.indexOf(paramString1);

    String str;

    if (i == -1)

      str = paramString2;

    else

      str = paramString2.substring(1 + (str + paramString1.length()));

    return str;

  }

 

  public static String getTypePart(String paramString)

  {

    int j = paramString.lastIndexOf('.');

    int i = paramString.length();

    String str;

    if ((j != -1) && (j != i - 1))

      str = paramString.substring(j + 1, i);

    else

      str = "";

    return str;

  }

 

  public static String getUNIXfilePath(String paramString)

  {

    return toUNIXpath(new File(paramString).getAbsolutePath());

  }

 

  public static boolean isFileExist(String paramString)

  {

    return new File(paramString).isFile();

  }

 

  public static boolean makeDirectory(File paramFile)

  {

    File localFile = paramFile.getParentFile();

    boolean bool;

    if (localFile == null)

      bool = false;

    else

      bool = bool.mkdirs();

    return bool;

  }

 

  public static boolean makeDirectory(String paramString)

  {

    return makeDirectory(new File(paramString));

  }

 

  public static boolean renameFile(String paramString1, String paramString2)

  {

    return new File(paramString1).renameTo(new File(paramString2));

  }

 

  public static String toUNIXpath(String paramString)

  {

    return paramString.replace('\\', '/');

  }

 

  public static void touch(File paramFile)

  {

    long l = System.currentTimeMillis();

    if (!paramFile.exists())

    {

      System.err.println("file not found:" + paramFile.getName());

      System.err.println("Create a new file:" + paramFile.getName());

    }

    try

    {

      boolean bool = paramFile.createNewFile();

      if ((!bool) || (!paramFile.setLastModified(l)));

      return;

    }

    catch (IOException localIOException)

    {

      while (true)

        localIOException.printStackTrace();

    }

  }

 

  public static void touch(String paramString)

  {

    touch(new File(paramString));

  }

 

  public static void touch(File[] paramArrayOfFile)

  {

    for (int i = 0; ; i++)

    {

      if (i >= paramArrayOfFile.length)

        return;

      touch(paramArrayOfFile);

    }

  }

 

  public static void touch(String[] paramArrayOfString)

  {

    File[] arrayOfFile = new File[paramArrayOfString.length];

    for (int i = 0; ; i++)

    {

      if (i >= paramArrayOfString.length)

      {

        touch(arrayOfFile);

        return;

      }

      arrayOfFile[i] = new File(paramArrayOfString[i]);

    }

  }

 

  public static String trimType(String paramString)

  {

    int i = paramString.lastIndexOf(".");

    String str;

    if (i == -1)

      str = paramString;

    else

      str = paramString.substring(0, str);

    return str;

  }

 

  public void saveBitmap(Bitmap paramBitmap, String paramString)

    throws IOException

  {

    Object localObject = new File(paramString);

    try

    {

      localObject = new FileOutputStream((File)localObject);

      if (paramBitmap.compress(Bitmap.CompressFormat.JPEG, 100, (OutputStream)localObject))

      {

        ((FileOutputStream)localObject).flush();

        ((FileOutputStream)localObject).close();

      }

      return;

    }

    catch (FileNotFoundException localFileNotFoundException)

    {

      while (true)

        localFileNotFoundException.printStackTrace();

    }

    catch (IOException localIOException)

    {

      while (true)

        localIOException.printStackTrace();

    }

  }

}

 

你可能感兴趣的:(文件操作)