介绍
介绍
福哥整理了APP里面操作文件/文件夹的方法,今天分享一下
福哥封装了一个对象 TFIO,包括了常用的功能
创建文件夹
写文件
读文件
判断类型
删除文件
删除文件夹
教程
TFIOItem
福哥建立了一个IOItem用来保存遍历元素
class TFIOItem{ private String name; private String extension; private String absPath; public TFIOItem(){ } public void Set(String fileName, String fileAbsPath){ Integer fep, fnp; absPath = fileAbsPath; name = fileName; fep = name.lastIndexOf("."); extension = (fep != -1) ? name.substring(fep) : null ; } public String GetName(){ return name; } public String GetExtension(){ return extension; } public String GetAbsPath(){ return absPath; } }
TFIO
福哥建立了一个TFIO对象,用来操作文件/文件夹
public class TFIO { private TFIOItem[] items; public TFIO(){ } protected void OnLoadItem(TFIOItem item){ } public Boolean IsImage(Integer idx){ String fileExt; fileExt = items[idx].GetExtension().toLowerCase(); if(fileExt.equals(".jpg") || fileExt.equals(".png") || fileExt.equals(".gif") || fileExt.equals(".bmp") || fileExt.equals(".jpeg") || fileExt.equals(".pjpeg")){ return true; } return false; } public Boolean IsDirectory(String absPath){ File dir = new File(absPath); return (dir.exists() && dir.isDirectory()); } public Boolean IsFile(String absPath){ File file = new File(absPath); return (file.exists() && file.isFile()); } public TFIOItem GetItem(Integer idx){ return items[idx]; } public TFIOItem[] GetItems(){ return items; } public Integer GetCount(){ return (items != null) ? items.length : 0 ; } public Boolean CreateDirectory(String absDirPath){ File dir; dir = new File(absDirPath); dir.mkdirs(); return dir.isDirectory(); } public Boolean RemoveDirectory(String absDirPath){ File dir; dir = new File(absDirPath); return dir.delete(); } public Boolean RemoveFile(String absFilePath){ File file; file = new File(absFilePath); return file.delete(); } public String GetContents(String absFilePath){ BufferedReader reader; String line, contents; try { reader = new BufferedReader(new FileReader(absFilePath)); contents = ""; while ((line = reader.readLine()) != null) { contents += line; } reader.close(); return contents; } catch (Exception e){ e.printStackTrace(); } return null; } public Integer PutContents(String absFilePath, String contents){ FileOutputStream fileWriter; try { fileWriter = new FileOutputStream(absFilePath); fileWriter.write(contents.getBytes()); fileWriter.flush(); fileWriter.close(); return contents.length(); } catch (Exception e){ e.printStackTrace(); } return 0; } public Integer LoadDirectories(String rootDirAbsPath){ File dir; Integer i, l, fl; dir = new File(rootDirAbsPath); File[] subFile = dir.listFiles(); if(subFile != null && subFile.length > 0){ l = subFile.length; fl = 0; // count dirs for (i = 0; i < l; i++) { if (subFile[i].isDirectory()) { fl++; } } if(fl > 0){ items = new TFIOItem[fl]; // save dirs fl = 0; for(i=0;i<l;i++){ if(subFile[i].isDirectory()){ items[fl] = new TFIOItem(); items[fl].Set(subFile[i].getName(), subFile[i].getAbsolutePath()); OnLoadItem(items[fl]); fl ++; } } return fl; } } return 0; } public Integer LoadFiles(String rootDirAbsPath){ File dir; Integer i, l, fl; dir = new File(rootDirAbsPath); File[] subFile = dir.listFiles(); if(subFile != null && subFile.length > 0){ l = subFile.length; fl = 0; // count dirs for (i = 0; i < l; i++) { if (subFile[i].isFile()) { fl++; } } if(fl > 0){ items = new TFIOItem[fl]; // save dirs fl = 0; for(i=0;i<l;i++){ if(subFile[i].isFile()){ items[fl] = new TFIOItem(); items[fl].Set(subFile[i].getName(), subFile[i].getAbsolutePath()); OnLoadItem(items[fl]); fl ++; } } return fl; } } return 0; } }
使用
建立文件夹
通过 CreateDirectory 建立文件夹,通过 IsDirectory 检测结果
TFIO tfio = new TFIO(); testDir = "/mnt/sdcard/tongfu.net"; tfio.CreateDirectory(testDir); tfio.IsDirectory(testDir);
建立文件
通过 PutContents 写入内容到文件,通过 IsFile 检测结果
TFIO tfio = new TFIO(); testFile = "/mnt/sdcard/tongfu.net/io.txt"; tfio.PutContents(testFile, "TFIO"); tfio.IsFile(testFile); tfio.GetContents(testFile);
删除文件
通过 RemoveFile 删除文件
TFIO tfio = new TFIO(); testFile = "/mnt/sdcard/tongfu.net/io.txt"; tfio.RemoveFile(testFile);
删除文件夹
通过 RemoveDirectory 删除文件夹
TFIO tfio = new TFIO(); testFile = "/mnt/sdcard/tongfu.net/io.txt"; tfio.RemoveDirectory(testDir);