首页 > 学院 > 开发设计 > 正文

安卓 文件相关

2019-11-06 09:41:22
字体:
来源:转载
供稿:网友
	//文件不存在就创建	public static File makeRootDirectory(String filePath) {		File file = null;		try {			file = new File(filePath);			if (!file.exists()) {				file.mkdir();			}		} catch (Exception e) {			LogUtils.i(Tag, e+"");		}		return file;	}	/**	 * 生成文件	 * */ 	public static File makeFilePath(String filePath, String fileName) {		File file = null;		makeRootDirectory(filePath);		try {			file = new File(filePath + fileName);			if (!file.exists()) {				file.createNewFile();			}		} catch (Exception e) {			e.PRintStackTrace();		}		return file;	}	/**	 * 写入文件	 * */	public static void writeFile(String fileName, String message){		message = message+" ";		LogUtils.i(Tag, "message:"+message);		if (fileIsExists(fileName)) {			try {				FileOutputStream fileOutputStream = new FileOutputStream(fileName);				byte [] bytes = message.getBytes();				fileOutputStream.write(bytes);				fileOutputStream.flush();				LogUtils.i(Tag, "message:"+message);				fileOutputStream.close();			} catch (FileNotFoundException e) {				// TODO Auto-generated catch block				e.printStackTrace();			} catch (IOException e) {				// TODO Auto-generated catch block				e.printStackTrace();			}		}	}		/**	 * 读文件	 * */	private String readFile(String fileName) throws IOException {        String res = "";        try {                        FileInputStream fin = new FileInputStream(fileName);            int length = fin.available();                        byte[] buffer = new byte[length];            fin.read(buffer);                        res = EncodingUtils.getString(buffer, "UTF-8");                        fin.close();                    } catch (Exception e) {            e.printStackTrace();        }        return res;    }	/**	 * 文件是否存在	 * */	public static boolean fileIsExists(String filePath){		try{			File f=new File(filePath);			if(!f.exists()){				return false;			}		}catch (Exception e) {			// TODO: handle exception			return false;		}		return true;	}	/**	 * 删除文件夹及子目录	 * */	public static Boolean deleteFile(File file){		if(file.isDirectory()){			File[] files = file.listFiles();			for(int i=0; i<files.length; i++){				deleteFile(files[i]);			}		}		return file.delete();	}	/**	 * 判断文件是否为空	 * */	public static  boolean deleteEmptyDirectory(File path) {   		if( path.exists() ) {  //判断是否为空 			File[] files = path.listFiles();   			for(int i=0; i<files.length; i++) {//如果不为空不删除   				if(files[i].isDirectory()) {   					deleteEmptyDirectory(files[i]);   				}   				else {//执行删除   					files[i].delete();   				}   			}   		}   		return( path.delete() );   	}   	//获取文件里面的图片列表	public static List<Photos> getListOfPictures(File fileName){		LogUtils.i(Tag, "fileName:   "+fileName);		File[] files = fileName.listFiles();//		LogUtils.i(Tag, "files fileName.listFiles() "+fileName.listFiles());		List<Photos> listPhoto = new ArrayList<Photos>();		if (files != null) {			for (int i = 0; i < files.length; i++) {				File file = files[i];  				if (file.getName().substring(file.getName().lastIndexOf(".")+1).equals("png")) {					LogUtils.i(Tag, "file.getName() "+file.getName());					Photos photos = new Photos();					photos.setPhotoName(file.getName());					listPhoto.add(photos);				}				}		}		return listPhoto;	}	/**	 * 获取文件夹下所有指定文件	 * */	public static void getAllFile(File root){		File files[] = root.listFiles();		if (files != null) {			for (File f: files) {				if (f.isDirectory()) {					getAllFile(f);				}else {					LogUtils.i(Tag, "f: "+f);					LogUtils.i(Tag, "lastModified: "+f.lastModified()/1000);				}			}		}	}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表