位置: 编程技术 - 正文

工具类(2)文件操作工具类(工具类的作用)

编辑:rootadmin

推荐整理分享工具类(2)文件操作工具类(工具类的作用),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:工具类的作用,工具类是啥,什么叫工具类,工具类怎么写,工具类怎么写,工具文里的工具都有啥,工具文里的工具都有啥,工具类是啥,内容如对您有帮助,希望把文章链接给更多的朋友!

这些工具类是由开源项目中获取得到

工具类(2)文件操作工具类(工具类的作用)

public class FileUtils {

/*** 写文本文件 在Android系统中,文件保存在 /data/data/PACKAGE_NAME/files 目录下* * @param context* @param msg*/public static void write(Context context, String fileName, String content) {if (content == null)content = "";try {FileOutputStream fos = context.openFileOutput(fileName,Context.MODE_PRIVATE);fos.write(content.getBytes());fos.close();} catch (Exception e) {e.printStackTrace();}}/*** 读取文本文件* * @param context* @param fileName* @return*/public static String read(Context context, String fileName) {try {FileInputStream in = context.openFileInput(fileName);return readInStream(in);} catch (Exception e) {e.printStackTrace();}return "";}public static String readInStream(InputStream inStream) {try {ByteArrayOutputStream outStream = new ByteArrayOutputStream();byte[] buffer = new byte[];int length = -1;while ((length = inStream.read(buffer)) != -1) {outStream.write(buffer, 0, length);}outStream.close();inStream.close();return outStream.toString();} catch (IOException e) {Log.i("FileTest", e.getMessage());}return null;}public static File createFile(String folderPath, String fileName) {File destDir = new File(folderPath);if (!destDir.exists()) {destDir.mkdirs();}return new File(folderPath, fileName &#; fileName);}/*** 向手机写图片* * @param buffer* @param folder* @param fileName* @return*/public static boolean writeFile(byte[] buffer, String folder,String fileName) {boolean writeSucc = false;boolean sdCardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);String folderPath = "";if (sdCardExist) {folderPath = Environment.getExternalStorageDirectory()&#; File.separator &#; folder &#; File.separator;} else {writeSucc = false;}File fileDir = new File(folderPath);if (!fileDir.exists()) {fileDir.mkdirs();}File file = new File(folderPath &#; fileName);FileOutputStream out = null;try {out = new FileOutputStream(file);out.write(buffer);writeSucc = true;} catch (Exception e) {e.printStackTrace();} finally {try {out.close();} catch (IOException e) {e.printStackTrace();}}return writeSucc;}/*** 根据文件绝对路径获取文件名* * @param filePath* @return*/public static String getFileName(String filePath) {if (StringUtils.isEmpty(filePath))return "";return filePath.substring(filePath.lastIndexOf(File.separator) &#; 1);}/*** 根据文件的绝对路径获取文件名但不包含扩展名* * @param filePath* @return*/public static String getFileNameNoFormat(String filePath) {if (StringUtils.isEmpty(filePath)) {return "";}int point = filePath.lastIndexOf('.');return filePath.substring(filePath.lastIndexOf(File.separator) &#; 1,point);}/*** 获取文件扩展名* * @param fileName* @return*/public static String getFileFormat(String fileName) {if (StringUtils.isEmpty(fileName))return "";int point = fileName.lastIndexOf('.');return fileName.substring(point &#; 1);}/*** 获取文件大小* * @param filePath* @return*/public static long getFileSize(String filePath) {long size = 0;File file = new File(filePath);if (file != null && file.exists()) {size = file.length();}return size;}/*** 获取文件大小* * @param size* 字节* @return*/public static String getFileSize(long size) {if (size <= 0)return "0";java.text.DecimalFormat df = new java.text.DecimalFormat("##.##");float temp = (float) size / ;if (temp >= ) {return df.format(temp / ) &#; "M";} else {return df.format(temp) &#; "K";}}/*** 转换文件大小* * @param fileS* @return B/KB/MB/GB*/public static String formatFileSize(long fileS) {java.text.DecimalFormat df = new java.text.DecimalFormat("#.");String fileSizeString = "";if (fileS < ) {fileSizeString = df.format((double) fileS) &#; "B";} else if (fileS < ) {fileSizeString = df.format((double) fileS / ) &#; "KB";} else if (fileS < ) {fileSizeString = df.format((double) fileS / ) &#; "MB";} else {fileSizeString = df.format((double) fileS / ) &#; "G";}return fileSizeString;}/*** 获取目录文件大小* * @param dir* @return*/public static long getDirSize(File dir) {if (dir == null) {return 0;}if (!dir.isDirectory()) {return 0;}long dirSize = 0;File[] files = dir.listFiles();for (File file : files) {if (file.isFile()) {dirSize &#;= file.length();} else if (file.isDirectory()) {dirSize &#;= file.length();dirSize &#;= getDirSize(file); // 递归调用继续统计}}return dirSize;}/*** 获取目录文件个数* * @param f* @return*/public long getFileList(File dir) {long count = 0;File[] files = dir.listFiles();count = files.length;for (File file : files) {if (file.isDirectory()) {count = count &#; getFileList(file);// 递归count--;}}return count;}public static byte[] toBytes(InputStream in) throws IOException {ByteArrayOutputStream out = new ByteArrayOutputStream();int ch;while ((ch = in.read()) != -1) {out.write(ch);}byte buffer[] = out.toByteArray();out.close();return buffer;}/*** 检查文件是否存在* * @param name* @return*/public static boolean checkFileExists(String name) {boolean status;if (!name.equals("")) {File path = Environment.getExternalStorageDirectory();File newPath = new File(path.toString() &#; name);status = newPath.exists();} else {status = false;}return status;}/*** 检查路径是否存在* * @param path* @return*/public static boolean checkFilePathExists(String path) {return new File(path).exists();}/*** 计算SD卡的剩余空间* * @return 返回-1,说明没有安装sd卡*/public static long getFreeDiskSpace() {String status = Environment.getExternalStorageState();long freeSpace = 0;if (status.equals(Environment.MEDIA_MOUNTED)) {try {File path = Environment.getExternalStorageDirectory();StatFs stat = new StatFs(path.getPath());long blockSize = stat.getBlockSize();long availableBlocks = stat.getAvailableBlocks();freeSpace = availableBlocks * blockSize / ;} catch (Exception e) {e.printStackTrace();}} else {return -1;}return (freeSpace);}/*** 新建目录* * @param directoryName* @return*/public static boolean createDirectory(String directoryName) {boolean status;if (!directoryName.equals("")) {File path = Environment.getExternalStorageDirectory();File newPath = new File(path.toString() &#; directoryName);status = newPath.mkdir();status = true;} elsestatus = false;return status;}/*** 检查是否安装SD卡* * @return*/public static boolean checkSaveLocationExists() {String sDCardStatus = Environment.getExternalStorageState();boolean status;if (sDCardStatus.equals(Environment.MEDIA_MOUNTED)) {status = true;} elsestatus = false;return status;}/*** 检查是否安装外置的SD卡* * @return*/public static boolean checkExternalSDExists() {Map<String, String> evn = System.getenv();return evn.containsKey("SECONDARY_STORAGE");}/*** 删除目录(包括:目录里的所有文件)* * @param fileName* @return*/public static boolean deleteDirectory(String fileName) {boolean status;SecurityManager checker = new SecurityManager();if (!fileName.equals("")) {File path = Environment.getExternalStorageDirectory();File newPath = new File(path.toString() &#; fileName);checker.checkDelete(newPath.toString());if (newPath.isDirectory()) {String[] listfile = newPath.list();// delete all files within the specified directory and then// delete the directorytry {for (int i = 0; i < listfile.length; i&#;&#;) {File deletedFile = new File(newPath.toString() &#; "/"&#; listfile[i].toString());deletedFile.delete();}newPath.delete();Log.i("DirectoryManager deleteDirectory", fileName);status = true;} catch (Exception e) {e.printStackTrace();status = false;}} elsestatus = false;} elsestatus = false;return status;}/*** 删除文件* * @param fileName* @return*/public static boolean deleteFile(String fileName) {boolean status;SecurityManager checker = new SecurityManager();if (!fileName.equals("")) {File path = Environment.getExternalStorageDirectory();File newPath = new File(path.toString() &#; fileName);checker.checkDelete(newPath.toString());if (newPath.isFile()) {try {Log.i("DirectoryManager deleteFile", fileName);newPath.delete();status = true;} catch (SecurityException se) {se.printStackTrace();status = false;}} elsestatus = false;} elsestatus = false;return status;}/*** 删除空目录* * 返回 0代表成功 ,1 代表没有删除权限, 2代表不是空目录,3 代表未知错误* * @return*/public static int deleteBlankPath(String path) {File f = new File(path);if (!f.canWrite()) {return 1;}if (f.list() != null && f.list().length > 0) {return 2;}if (f.delete()) {return 0;}return 3;}/*** 重命名* * @param oldName* @param newName* @return*/public static boolean reNamePath(String oldName, String newName) {File f = new File(oldName);return f.renameTo(new File(newName));}/*** 删除文件* * @param filePath*/public static boolean deleteFileWithPath(String filePath) {SecurityManager checker = new SecurityManager();File f = new File(filePath);checker.checkDelete(filePath);if (f.isFile()) {Log.i("DirectoryManager deleteFile", filePath);f.delete();return true;}return false;}/*** 清空一个文件夹* @param files*/public static void clearFileWithPath(String filePath) {List<File> files = FileUtils.listPathFiles(filePath);if (files.isEmpty()) {return;}for (File f : files) {if (f.isDirectory()) {clearFileWithPath(f.getAbsolutePath());} else {f.delete();}}}/*** 获取SD卡的根目录* * @return*/public static String getSDRoot() {return Environment.getExternalStorageDirectory().getAbsolutePath();}/*** 获取手机外置SD卡的根目录* * @return*/public static String getExternalSDRoot() {Map<String, String> evn = System.getenv();return evn.get("SECONDARY_STORAGE");}/*** 列出root目录下所有子目录* * @param path* @return 绝对路径*/public static List<String> listPath(String root) {List<String> allDir = new ArrayList<String>();SecurityManager checker = new SecurityManager();File path = new File(root);checker.checkRead(root);// 过滤掉以.开始的文件夹if (path.isDirectory()) {for (File f : path.listFiles()) {if (f.isDirectory() && !f.getName().startsWith(".")) {allDir.add(f.getAbsolutePath());}}}return allDir;}/*** 获取一个文件夹下的所有文件* @param root* @return*/public static List<File> listPathFiles(String root) {List<File> allDir = new ArrayList<File>();SecurityManager checker = new SecurityManager();File path = new File(root);checker.checkRead(root);File[] files = path.listFiles();for (File f : files) {if (f.isFile())allDir.add(f);else listPath(f.getAbsolutePath());}return allDir;}public enum PathStatus {SUCCESS, EXITS, ERROR}/*** 创建目录* * @param path*/public static PathStatus createPath(String newPath) {File path = new File(newPath);if (path.exists()) {return PathStatus.EXITS;}if (path.mkdir()) {return PathStatus.SUCCESS;} else {return PathStatus.ERROR;}}/*** 截取路径名* * @return*/public static String getPathName(String absolutePath) {int start = absolutePath.lastIndexOf(File.separator) &#; 1;int end = absolutePath.length();return absolutePath.substring(start, end);}/*** 获取应用程序缓存文件夹下的指定目录* @param context* @param dir* @return*/public static String getAppCache(Context context, String dir) {String savePath = context.getCacheDir().getAbsolutePath() &#; "/" &#; dir &#; "/";File savedir = new File(savePath);if (!savedir.exists()) {savedir.mkdirs();}savedir = null;return savePath;}}

Linux Mint配置android环境(java+eclipse+adt+android_sdk) 今天在Linuxmint上配置android,弄了好长时间才弄好,写下安装记录,也方便以后查看。哈哈!大家如果使用Linux应该知道,linux自带了OpenJDK的java,但是功

工具类(3)HTML相关的正则表达式工具类 该工具类由开源项目中获得publicclassHtmlRegexpUtils{privatefinalstaticStringregxpForHtml=([^]*);//过滤所有以开头以结尾的标签privatefinalstaticStringregxpForImgTag=\s*img\s([^

android怎么彻底关闭一个程序 本文为转载:

标签: 工具类的作用

本文链接地址:https://www.jiuchutong.com/biancheng/384293.html 转载请保留说明!

上一篇:Android的拖放技术(android拖拽)

下一篇:Linux Mint配置android环境(java+eclipse+adt+android_sdk)(linux如何配置)

  • 增值税发票管理办法
  • 药店的主营业务有哪些
  • 收到租赁费发票的账务处理
  • 在建工程在现金流量表中
  • 房地产企业实收资本要求
  • 公司卖房产怎么缴税
  • 企业所得税表样
  • 收到供应商赔偿的违约金
  • 资源税代扣代缴怎么操作
  • 结转损益后损益类科目为0吗
  • 短期借款计提利息例题
  • 福利费需要交纳个税吗
  • 个人所得税多缴了可以退回吗
  • 企业为什么会成为非正常户
  • 购买国债兑现时要交税吗
  • 电子税务局变更经营地址怎么操作
  • 土地出让金返还流程
  • 土地出让金11%抵扣进项税的账务处理?
  • 个体工商户怎么交税
  • 上年度职工工资总额
  • 股权无偿赠与子女怎么办理手续
  • 出口视同内销补缴增值税会计分录?
  • 公司用库存商品怎么做账
  • windows10如何开热点
  • 如何防止程序被关闭
  • 工会经费,职工福利费,教育经费的扣除标准
  • 找发票抵工资
  • 设立全资子公司的风险
  • 苹果微信朋友圈怎么发文字不带图片
  • ajax调用php函数
  • 餐费发票可以抵扣成本吗
  • .exe是啥
  • php中字符串函数
  • 收到短期借款会计分录怎么做
  • 涉税服务实务重点总结
  • php7数据库操作
  • php如何读取文件内容
  • 小规模纳税人免税额度是多少
  • php连接mysql数据库的几种方式及区别
  • vue自定义页面
  • 后端返回图片
  • 处置资产的账务处理
  • 在建工程减值准备借贷方向
  • 如何在织梦中设置图片加文字
  • 货已入库进项发票未到怎么办
  • 小规模纳税人销售商品会计分录
  • 预缴分包抵扣,所有分包都可以抵扣吗
  • 工业企业增值税税负多少合理
  • 商誉需要交税吗
  • mysql监控sql性能
  • 没有以前年度损益科目应该加在什么地方
  • 小规模纳税人附加税怎么算
  • 利润表中本月数,本期金额指什么
  • 办公装修费用多少
  • 公司买车可以抵扣企业所得税吗
  • 进项留底是什么科目
  • 资产负债表的资产按流动性大小排列
  • 公司租用房屋用交房产税吗
  • 外资企业对应的企业是什么
  • 五险一金会计科目分录
  • 企业出售产品
  • mysql macbook安装教程
  • 修改mysql用户权限
  • 批量更新数据库最多多少条
  • 升级完鸿蒙系统5G咋没有了
  • torch.cuda.set_device
  • 电脑xp系统如何
  • vmware下载不了
  • win 8系统怎么样
  • iptables: Unknown error 4294967295问题解决方法
  • nodejss
  • android自动开关机失效
  • 电脑自动检测硬盘怎么取消
  • diskpart ntfs
  • Protecting Content With AssetBundle[Unity]
  • jquery操作样式
  • 做网页的流程与步骤
  • 海淀区地税局电话
  • 股权转让查账重点查什么
  • 如何鼓励互联网企业发展
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设