Android Open Source - HexNanoController_Android File Helper






From Project

Back to project page HexNanoController_Android.

License

The source code is released under:

Code license GNU GPL v2 http://www.gnu.org/licenses/gpl.html Content license CC BY-NC-SA 4.0 http://creativecommons.org/licenses/by-nc-sa/4.0/

If you think the Android project HexNanoController_Android 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.hexairbot.hexmini.util;
//from  ww  w . j a  v a2s.  c  o m




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 android.content.Context;

import android.os.Environment;



public class FileHelper {

  private Context context;

  private String SDPATH;

  private String FILESPATH;



  public FileHelper(Context context) {

    this.context = context;

    SDPATH = Environment.getExternalStorageDirectory() + "/";

    FILESPATH = this.context.getFilesDir().getPath() + "/";

  }



  /**

   * (WSDaS
NR?^?e?N

   * 

   * @throws IOException

   */

  public File creatSDFile(String fileName) throws IOException {

    File file = new File(SDPATH + fileName);

    file.createNewFile();

    return file;

  }



  /**

   *  Rd?SDaS
N?v?e?N

   * 

   * @param fileName

   */

  public boolean delSDFile(String fileName) {

    File file = new File(SDPATH + fileName);

    if (file == null || !file.exists() || file.isDirectory())

      return false;

    file.delete();

    return true;

  }



  /**

   * (WSDaS
NR?^?vU_

   * 

   * @param dirName

   */

  public File creatSDDir(String dirName) {

    File dir = new File(SDPATH + dirName);

    dir.mkdir();

    return dir;

  }



  /**

   *  Rd?SDaS
N?v?vU_

   * 

   * @param dirName

   */

  public boolean delSDDir(String dirName) {

    File dir = new File(SDPATH + dirName);

    return delDir(dir);

  }



  /**

   * $R?e(WSDaS
N/f&TX[(W?e?NfileName

   * 

   * @param fileName

   */

  public boolean hasSDFile(String fileName) {

    File file = new File(SDPATH + fileName);

    return file.exists() && file.isFile();

  }

  

  

  /**

   * $R?e(WSDaS
N/f&TX[(W?vU_dirName

   * 

   * @param dirName

   */

  public boolean hasSDDir(String dirName) {

    File file = new File(SDPATH + dirName);

    return file.exists() && file.isDirectory();

  }

  

  /**

   * ?O9eSDaS
N?v?e?Nb?vU_
T

   * 

   * @param fileName

   */

  public boolean renameSDFile(String oldfileName, String newFileName) {

    File oleFile = new File(SDPATH + oldfileName);

    File newFile = new File(SDPATH + newFileName);

    return oleFile.renameTo(newFile);

  }



  /**

   * ?b?SDaS
N?vUS*N?e?N

   * 

   * @param path

   * @throws IOException

   */

  public boolean copySDFileTo(String srcFileName, String destFileName)

      throws IOException {

    File srcFile = new File(SDPATH + srcFileName);

    File destFile = new File(SDPATH + destFileName);

    return copyFileTo(srcFile, destFile);

  }



  /**

   * ?b?SDaS
Nc?[?vU_?v@b  g?e?N

   * 

   * @param srcDirName

   * @param destDirName

   * @return

   * @throws IOException

   */

  public boolean copySDFilesTo(String srcDirName, String destDirName)

      throws IOException {

    File srcDir = new File(SDPATH + srcDirName);

    File destDir = new File(SDPATH + destDirName);

    return copyFilesTo(srcDir, destDir);

  }



  /**

   * ?y?RSDaS
N?vUS*N?e?N

   * 

   * @param srcFileName

   * @param destFileName

   * @return

   * @throws IOException

   */

  public boolean moveSDFileTo(String srcFileName, String destFileName)

      throws IOException {

    File srcFile = new File(SDPATH + srcFileName);

    File destFile = new File(SDPATH + destFileName);

    return moveFileTo(srcFile, destFile);

  }



  /**

   * ?y?RSDaS
N?vc?[?vU_?v@b  g?e?N

   * 

   * @param srcDirName

   * @param destDirName

   * @return

   * @throws IOException

   */

  public boolean moveSDFilesTo(String srcDirName, String destDirName)

      throws IOException {

    File srcDir = new File(SDPATH + srcDirName);

    File destDir = new File(SDPATH + destDirName);

    return moveFilesTo(srcDir, destDir);

  }





  /*

   * \?e?N?QeQsdaS0?Y:writeSDFile("test.txt");

   */

  public Output writeSDFile(String fileName) throws IOException {

    File file = new File(SDPATH + fileName);

    FileOutputStream fos = new FileOutputStream(file);

    return new Output(fos);

  }



  /*

   * (W?S  g?e?N
N?~?~?Q?e?N0?Y:appendSDFile("test.txt");

   */

  public Output appendSDFile(String fileName) throws IOException {

    File file = new File(SDPATH + fileName);

    FileOutputStream fos = new FileOutputStream(file, true);

    return new Output(fos);

  }



  /*

   * ?NSDaS???S?e?N0?Y:readSDFile("test.txt");

   */

  public Input readSDFile(String fileName) throws IOException {

    File file = new File(SDPATH + fileName);

    FileInputStream fis = new FileInputStream(file);

    return new Input(fis);

  }

  

  

  /**

   * ?^?z?y  g?e?N

   * 

   * @param fileName

   * @return

   * @throws IOException

   */

  public File creatDataFile(String fileName) throws IOException {

    File file = new File(FILESPATH + fileName);

    file.createNewFile();

    return file;

  }



  /**

   * ?^?z?y  g?vU_

   * 

   * @param dirName

   * @return

   */

  public File creatDataDir(String dirName) {

    File dir = new File(FILESPATH + dirName);

    dir.mkdir();

    return dir;

  }



  /**

   *  Rd??y  g?e?N

   * 

   * @param fileName

   * @return

   */

  public boolean delDataFile(String fileName) {

    File file = new File(FILESPATH + fileName);

    return delFile(file);

  }



  /**

   *  Rd??y  g?vU_

   * 

   * @param dirName

   * @return

   */

  public boolean delDataDir(String dirName) {

    File file = new File(FILESPATH + dirName);

    return delDir(file);

  }

  

  /**

   * $R?e(WSDaS
N/f&TX[(W?e?NfileName

   * 

   * @param fileName

   */

  public boolean hasDataFile(String fileName) {

    File file = new File(FILESPATH + fileName);

    return file.exists() && file.isFile();

  }

  

  

  /**

   * $R?e(WSDaS
N/f&TX[(W?vU_dirName

   * 

   * @param dirName

   */

  public boolean hasDataDir(String dirName) {

    File file = new File(FILESPATH + dirName);

    return file.exists() && file.isDirectory();

  }



  /**

   * ?f9e?y  g?e?N
T

   * 

   * @param oldName

   * @param newName

   * @return

   */

  public boolean renameDataFile(String oldName, String newName) {

    File oldFile = new File(FILESPATH + oldName);

    File newFile = new File(FILESPATH + newName);

    return oldFile.renameTo(newFile);

  }



  /**

   * (W?y  g?vU_N??L??e?N
Y6R

   * 

   * @param srcFileName

   *            ? S+T???_?S?e?N
T

   * @param destFileName

   * @return

   * @throws IOException

   */

  public boolean copyDataFileTo(String srcFileName, String destFileName)

      throws IOException {

    File srcFile = new File(FILESPATH + srcFileName);

    File destFile = new File(FILESPATH + destFileName);

    return copyFileTo(srcFile, destFile);

  }



  /**

   * 
Y6R?y  g?vU_?c?[?vU_?v@b  g?e?N

   * 

   * @param srcDirName

   * @param destDirName

   * @return

   * @throws IOException

   */

  public boolean copyDataFilesTo(String srcDirName, String destDirName)

      throws IOException {

    File srcDir = new File(FILESPATH + srcDirName);

    File destDir = new File(FILESPATH + destDirName);

    return copyFilesTo(srcDir, destDir);

  }



  /**

   * ?y?R?y  g?vU_N?vUS*N?e?N

   * 

   * @param srcFileName

   * @param destFileName

   * @return

   * @throws IOException

   */

  public boolean moveDataFileTo(String srcFileName, String destFileName)

      throws IOException {

    File srcFile = new File(FILESPATH + srcFileName);

    File destFile = new File(FILESPATH + destFileName);

    return moveFileTo(srcFile, destFile);

  }



  /**

   * ?y?R?y  g?vU_N?vc?[?vU_N?v@b  g?e?N

   * 

   * @param srcDirName

   * @param destDirName

   * @return

   * @throws IOException

   */

  public boolean moveDataFilesTo(String srcDirName, String destDirName)

      throws IOException {

    File srcDir = new File(FILESPATH + srcDirName);

    File destDir = new File(FILESPATH + destDirName);

    return moveFilesTo(srcDir, destDir);

  }



  /*

   * \?e?N?QeQ?^(u?y  g?vfiles?vU_0?Y:writeFile("test.txt");

   */

  public Output wirteFile(String fileName) throws IOException {

    OutputStream os = context.openFileOutput(fileName,

        Context.MODE_WORLD_WRITEABLE);

    return new Output(os);

  }



  /*

   * (W?S  g?e?N
N?~?~?Q?e?N0?Y:appendFile("test.txt");

   */

  public Output appendFile(String fileName) throws IOException {

    OutputStream os = context.openFileOutput(fileName, Context.MODE_APPEND);

    return new Output(os);

  }



  /*

   * ?N?^(u?v?y  g?vU_files???S?e?N0?Y:readFile("test.txt");

   */

  public Input readFile(String fileName) throws IOException {

    InputStream is = context.openFileInput(fileName);

    return new Input(is);

  }

  

  

  

  /**********************************************************************************************************/

  /*********************************************************************************************************/

  /**

   *  Rd?N*N?e?N

   * 

   * @param file

   * @return

   */

  public boolean delFile(File file) {

    if (file.isDirectory())

      return false;

    return file.delete();

  }



  /**

   *  Rd?N*N?vU_??S?N/f^?zz?vU_  ?

   * 

   * @param dir

   */

  public boolean delDir(File dir) {

    if (dir == null || !dir.exists() || dir.isFile()) {

      return false;

    }

    for (File file : dir.listFiles()) {

      if (file.isFile()) {

        file.delete();

      } else if (file.isDirectory()) {

        delDir(file);// ?R_

      }

    }

    dir.delete();

    return true;

  }



  /**

   * ?b?N*N?e?N,srcFile?n?e?N?destFile?vh?e?N

   * 

   * @param path

   * @throws IOException

   */

  public boolean copyFileTo(File srcFile, File destFile) throws IOException {

    if (srcFile.isDirectory() || destFile.isDirectory())

      return false;// $R?e/f&T/f?e?N

    FileInputStream fis = new FileInputStream(srcFile);

    FileOutputStream fos = new FileOutputStream(destFile);

    int readLen = 0;

    byte[] buf = new byte[1024];

    while ((readLen = fis.read(buf)) != -1) {

      fos.write(buf, 0, readLen);

    }

    fos.flush();

    fos.close();

    fis.close();

    return true;

  }



  /**

   * ?b??vU_N?v@b  g?e?N0Rc?[?vU_

   * 

   * @param srcDir

   * @param destDir

   * @return

   * @throws IOException

   */

  public boolean copyFilesTo(File srcDir, File destDir) throws IOException {

    if (!srcDir.isDirectory() || !destDir.isDirectory())

      return false;// $R?e/f&T/f?vU_

    if (!destDir.exists())

      return false;// $R?e?vh?vU_/f&TX[(W

    File[] srcFiles = srcDir.listFiles();

    for (int i = 0; i < srcFiles.length; i++) {

      if (srcFiles[i].isFile()) {

        // ???_?vh?e?N

        File destFile = new File(destDir.getPath() + "\\"

            + srcFiles[i].getName());

        copyFileTo(srcFiles[i], destFile);

      } else if (srcFiles[i].isDirectory()) {

        File theDestDir = new File(destDir.getPath() + "\\"

            + srcFiles[i].getName());

        copyFilesTo(srcFiles[i], theDestDir);

      }

    }

    return true;

  }



  /**

   * ?y?RN*N?e?N

   * 

   * @param srcFile

   * @param destFile

   * @return

   * @throws IOException

   */

  public boolean moveFileTo(File srcFile, File destFile) throws IOException {

    boolean iscopy = copyFileTo(srcFile, destFile);

    if (!iscopy)

      return false;

    delFile(srcFile);

    return true;

  }



  /**

   * ?y?R?vU_N?v@b  g?e?N0Rc?[?vU_

   * 

   * @param srcDir

   * @param destDir

   * @return

   * @throws IOException

   */

  public boolean moveFilesTo(File srcDir, File destDir) throws IOException {

    if (!srcDir.isDirectory() || !destDir.isDirectory()) {

      return false;

    }

    File[] srcDirFiles = srcDir.listFiles();

    for (int i = 0; i < srcDirFiles.length; i++) {

      if (srcDirFiles[i].isFile()) {

        File oneDestFile = new File(destDir.getPath() + "\\"

            + srcDirFiles[i].getName());

        moveFileTo(srcDirFiles[i], oneDestFile);

        delFile(srcDirFiles[i]);

      } else if (srcDirFiles[i].isDirectory()) {

        File oneDestFile = new File(destDir.getPath() + "\\"

            + srcDirFiles[i].getName());

        moveFilesTo(srcDirFiles[i], oneDestFile);

        delDir(srcDirFiles[i]);

      }



    }

    return true;

  }

  

  public String getDataPath(){

    return FILESPATH;  

  }

  

  public String getDataString(){

    return SDPATH;

  }

}




Java Source Code List

.FileHelper.java
.Input.java
.Output.java
.Serializable.java
com.hexairbot.hexmini.HelpActivity.java
com.hexairbot.hexmini.HexMiniApplication.java
com.hexairbot.hexmini.HudActivity.java
com.hexairbot.hexmini.HudViewControllerDelegate.java
com.hexairbot.hexmini.HudViewController.java
com.hexairbot.hexmini.SettingsDialogDelegate.java
com.hexairbot.hexmini.SettingsDialog.java
com.hexairbot.hexmini.SettingsViewControllerDelegate.java
com.hexairbot.hexmini.SettingsViewController.java
com.hexairbot.hexmini.ViewController.java
com.hexairbot.hexmini.adapter.SettingsViewAdapter.java
com.hexairbot.hexmini.ble.BleConnectinManagerDelegate.java
com.hexairbot.hexmini.ble.BleConnectinManager.java
com.hexairbot.hexmini.ble.BluetoothLeService.java
com.hexairbot.hexmini.gestures.EnhancedGestureDetector.java
com.hexairbot.hexmini.modal.ApplicationSettings.java
com.hexairbot.hexmini.modal.Channel.java
com.hexairbot.hexmini.modal.OSDCommon.java
com.hexairbot.hexmini.modal.Transmitter.java
com.hexairbot.hexmini.sensors.DeviceOrientationChangeDelegate.java
com.hexairbot.hexmini.sensors.DeviceOrientationManager.java
com.hexairbot.hexmini.sensors.DeviceSensorManagerWrapper.java
com.hexairbot.hexmini.sensors.SensorManagerWrapper.java
com.hexairbot.hexmini.services.ConnectStateManager.java
com.hexairbot.hexmini.services.IpcControlService.java
com.hexairbot.hexmini.services.IpcProxy.java
com.hexairbot.hexmini.services.NavData.java
com.hexairbot.hexmini.services.OnIpcConnectChangedListener.java
com.hexairbot.hexmini.services.VIConfig.java
com.hexairbot.hexmini.ui.Button.java
com.hexairbot.hexmini.ui.Image.java
com.hexairbot.hexmini.ui.Indicator.java
com.hexairbot.hexmini.ui.Sprite.java
com.hexairbot.hexmini.ui.Text.java
com.hexairbot.hexmini.ui.ToggleButton.java
com.hexairbot.hexmini.ui.UIRenderer.java
com.hexairbot.hexmini.ui.control.CustomSeekBar.java
com.hexairbot.hexmini.ui.control.ViewPagerIndicator.java
com.hexairbot.hexmini.ui.gl.GLSprite.java
com.hexairbot.hexmini.ui.joystick.AcceleratorJoystick.java
com.hexairbot.hexmini.ui.joystick.AnalogueJoystick.java
com.hexairbot.hexmini.ui.joystick.JoystickBase.java
com.hexairbot.hexmini.ui.joystick.JoystickFactory.java
com.hexairbot.hexmini.ui.joystick.JoystickListener.java
com.hexairbot.hexmini.util.DebugHandler.java
com.hexairbot.hexmini.util.FontUtils.java
com.hexairbot.hexmini.util.SystemUiHiderBase.java
com.hexairbot.hexmini.util.SystemUiHiderHoneycomb.java
com.hexairbot.hexmini.util.SystemUiHider.java
com.hexairbot.hexmini.util.SystemUtil.java
com.hexairbot.hexmini.util.TextureUtils.java
fix.android.opengl.GLES20.java