星空网 > 软件开发 > 操作系统

文件处理工具类

文件处理:

常用操作:

  获得文件或文件夹的绝对路径和相对路径。  String path = File.getPath();//相对路径   String path = File.getAbsoultePath();//绝对路径     获得文件或文件夹的父目录  String parentPath = File.getParent();     获得文件或文件夹的名称  String Name = File.getName();     建立文件或文件夹  File.mkDir(); //建立文件夹   File.createNewFile();//建立文件     判断是文件或文件夹  File.isDirectory()     列出文件夹下的所有文件和文件夹名  File[] files = File.listFiles();     修改文件夹和文件名  File.renameTo(dest);     删除文件夹或文件  File.delete(); 

 增加:

//增加  //............................................分界线...............................................  /**   * 判断指定文件是否存在   * @param filePath   * @return   */  public static boolean isFileExist(String filePath) {    File file = new File(filePath);    return file.exists();  }    /**   * 创建一个文件,创建成功返回true   * @param filePath   * @return   */  public static boolean createFile(String filePath) {    try {      File file = new File(filePath);      if (!file.exists()) {        if (!file.getParentFile().exists()) {          //建立文件夹           file.getParentFile().mkdirs();        }        //创建一个空的文件        return file.createNewFile();      }    } catch (IOException e) {      e.printStackTrace();    }    return true;  }    /**   * 从一个输入流里写到一个指定的文件   * @param FilePath 要创建的文件的路径   * @param in   * @return   */  public static boolean writeFile(String FilePath, InputStream in) {    try {      //创建文件成功则继续,否则返回false      if (!createFile(FilePath)) {        return false;      }      FileOutputStream fos = new FileOutputStream(FilePath);      int readCount = 0;      int len = 1024;      byte[] buffer = new byte[len];      while ((readCount = in.read(buffer)) != -1) {        fos.write(buffer, 0, readCount);      }      fos.flush();      if (null != fos) {        fos.close();        fos = null;      }      if (null != in) {        in.close();        in = null;      }      return true;    } catch (IOException e) {      e.printStackTrace();    }    return false;  }    /**   * 将bitmap写入到指定路径的文件里   * @param bitmap  Bitmap对象   * @param destPath  指定的路径   * @param quality  压缩率,例如quality=30时,表示压缩70%; quality=100表示不压缩   */  public static void writeImage(Bitmap bitmap, String destPath, int quality) {    try {      FileAECS.deleteFile(destPath);      if (FileAECS.createFile(destPath)) {        FileOutputStream out = new FileOutputStream(destPath);        if (bitmap.compress(Bitmap.CompressFormat.JPEG, quality, out)) {          out.flush();          out.close();          out = null;        }      }    } catch (IOException e) {      e.printStackTrace();    }  }    /**   * 将数据写入一个文件   *   * @param destFilePath 要创建的文件的路径   * @param data     待写入的文件数据   * @param startPos   起始偏移量   * @param length    要写入的数据长度   * @return 成功写入文件返回true, 失败返回false   */  public static boolean writeFile(String destFilePath, byte[] data, int startPos, int length) {    try {      if (!createFile(destFilePath)) {        return false;      }      FileOutputStream fos = new FileOutputStream(destFilePath);      fos.write(data, startPos, length);      fos.flush();      if (null != fos) {        fos.close();        fos = null;      }      return true;    } catch (FileNotFoundException e) {      e.printStackTrace();    } catch (IOException e) {      e.printStackTrace();    }    return false;  }

删除:

//删除  //............................................分界线...............................................    /**   * 删除指定文件夹路径下所有文件,包括文件夹本身   * @param filePath 文件夹路径   */  public static void deleteAll(String filePath){    File file = new File(filePath);    deleteFiles(file);  }    /**   * 删除指定文件夹下所有文件,包括文件夹本身   * @param file File实例   */  public static void deleteFiles(File file) {    if (file.isDirectory()) {      File[] listFiles = file.listFiles();      for (int i = 0; i < listFiles.length; i++) {        deleteFiles(listFiles[i]);      }    }    file.delete();  }    /**   * 删除一个指定文件或文件夹   * @param filePath 需要被删除的文件或文件夹的路径   * @return   */  public static boolean deleteFile(String filePath) {    try {      File file = new File(filePath);      if (file.exists()) {        return file.delete();      }    } catch (Exception e) {      e.printStackTrace();    }    return false;  }

修改:

//修改  //............................................分界线..............................................  /**   * 批量更改指定文件夹下所有文件后缀   * @param file   * @param oldSuffix   * @param newSuffix   */  public static void ReFileNameSuffix(File file, String oldSuffix, String newSuffix) {    if (file.isDirectory()) {      File[] listFiles = file.listFiles();      for (int i = 0; i < listFiles.length; i++) {        ReFileNameSuffix(listFiles[i], oldSuffix, newSuffix);      }    } else {      file.renameTo(new File(file.getPath().replace(oldSuffix, newSuffix)));    }  }    /**   * 将一个文件拷贝到另外一个地方   * @param sourceFile  源文件地址   * @param destFile   目的地址   * @param shouldOverlay 是否覆盖   * @return   */  public static boolean copyFiles(String sourceFile, String destFile, boolean shouldOverlay) {    try {      if (shouldOverlay) {        deleteFile(destFile);      }      FileInputStream fi = new FileInputStream(sourceFile);      writeFile(destFile, fi);      return true;    } catch (FileNotFoundException e) {      e.printStackTrace();    }    return false;  }

读取:

//读取  //............................................分界线...............................................  /**   * 读取指定文件,返回以byte数组形式的数据   *   * @param filePath 要读取的文件路径名   * @return byte数组形式的数据   */  public static byte[] readFile(String filePath) {    try {      if (isFileExist(filePath)) {        FileInputStream fi = new FileInputStream(filePath);        return readInputStream(fi);      }    } catch (FileNotFoundException e) {      e.printStackTrace();    }    return null;  }    /**   * 从一个数量流里读取数据,返回以byte数组形式的数据。   * 需要注意的是,如果这个方法用在从本地文件读取数据时,一般不会遇到问题,但如果是用于网络操作,就经常会遇到一些麻烦(网络的不稳定性,available()方法的问题)。所以如果是网络流不应该使用这个方法。   *   * @param in 要读取的输入流   * @return   * @throws java.io.IOException   */  public static byte[] readInputStream(InputStream in) {    try {      ByteArrayOutputStream os = new ByteArrayOutputStream();      byte[] b = new byte[in.available()];      int length = 0;            while ((length = in.read(b)) != -1) {        os.write(b, 0, length);      }      b = os.toByteArray();      in.close();      in = null;      os.close();      os = null;      return b;    } catch (IOException e) {      e.printStackTrace();    }    return null;  }    /**   * 读取网络流   *   * @param in   * @return   */  public static byte[] readNetWorkInputStream(InputStream in) {    ByteArrayOutputStream os = null;    try {      os = new ByteArrayOutputStream();      int readCount = 0;      int len = 1024;      byte[] buffer = new byte[len];      while ((readCount = in.read(buffer)) != -1) {        os.write(buffer, 0, readCount);      }      in.close();      in = null;      return os.toByteArray();    } catch (IOException e) {      e.printStackTrace();    } finally {      if (null != os) {        try {          os.close();        } catch (IOException e) {          e.printStackTrace();        }        os = null;      }    }    return null;  }    /**   * 从resource的raw中读取文件数据,只能读取,不能写入   * @param Context   * @param test    例如R.raw.test   * @param Charset  编码格式 ,例如GBK、UTF-8、Unicode   * @return      String   */  public static String getRawTest(Context Context,int test,String Charset){    String String = null;    try{      //得到资源中的Raw数据流      InputStream in = Context.getResources().openRawResource(test);      //得到数据的大小      int length = in.available();         //缓存大小      byte [] buffer = new byte[length];      //读取数据      in.read(buffer);      //依test.txt的编码类型选择合适的编码,如果不调整会乱码      String = EncodingUtils.getString(buffer, Charset);      //关闭        in.close();      }catch(Exception e){       e.printStackTrace();          }    return String;  }    /**   * 从resource的asset中读取文件数据,只能读取,不能写入   * @param Context   * @param fileName    文件名   * @param Charset    编码格式,例如GBK、UTF-8、Unicode   * @return        String   */  public static String getAssetTest(Context Context,String fileName,String Charset){    String String = null;    try{      //得到资源中的asset数据流      InputStream in = Context.getResources().getAssets().open(fileName);      //得到数据的大小      int length = in.available();      //缓存大小      byte [] buffer = new byte[length];         //读取数据      in.read(buffer);           //依test.txt的编码类型选择合适的编码,如果不调整会乱码      String = EncodingUtils.getString(buffer, Charset);     }catch(Exception e){      e.printStackTrace();    }    return String;  }

sd卡处理相关:

//sd卡相关  //............................................分界线.........................................  /**   * 判断SD卡是否正常挂载   * @return true正常挂载   */  public static boolean hasSDCard() {    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {      return true;    } else {      return false;    }  }    /**   * 获得sd卡根目录即 /sdcard   * @return  File   */  public static File getSDCardRoot(){    if(hasSDCard()){      return Environment.getExternalStorageDirectory();    }    return null;  }

获取:

//获取  //............................................分界线.........................................  /**   * 获取文件夹大小   * @param file File实例   * @return long 单位为M   * @throws Exception   */  public static long getFolderSize(File file) throws Exception {    long size = 0;    File[] fileList = file.listFiles();    for (int i = 0; i < fileList.length; i++) {      if (fileList[i].isDirectory()) {        size = size + getFolderSize(fileList[i]);      } else {        size = size + fileList[i].length();      }    }    return size / (1024*1023);  }  /**   * 获得文件或文件夹的相对路径   * @param  file   * @return  String   */  public static String getFilePath(File file){    String str = file.getPath();    return str;      }    /**   * 获得文件或文件夹的绝对路径   * @param  file   * @return  String   */  public static String getFileAbsoultePath(File file){    String str = file.getAbsolutePath();    return str;  }

 




原标题:文件处理工具类

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

美国专线时间:https://www.goluckyvip.com/tag/38558.html
美国专线是什么:https://www.goluckyvip.com/tag/38559.html
美国专线物流:https://www.goluckyvip.com/tag/38560.html
美国专线物流商:https://www.goluckyvip.com/tag/38561.html
美国专线物流专线:https://www.goluckyvip.com/tag/38562.html
美国自建海外仓:https://www.goluckyvip.com/tag/38563.html
武陵山大裂谷周围景点 武陵山大裂谷周围景点图片:https://www.vstour.cn/a/411233.html
南美旅游报价(探索南美洲的旅行费用):https://www.vstour.cn/a/411234.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流