首页 > 代码库 > 文件操作常用工具方法
文件操作常用工具方法
写字节到文件:
/** * 工具方法,写bytes到文件中 如果写入过程出现异常就删除文件 * * @param bytes * @param file */ public static void writeBytesToFile(byte[] bytes, File file) { RandomAccessFile access = null; try { access = new RandomAccessFile(file, "rw"); access.write(bytes); } catch (IOException e) { if (e.getMessage() != null) { Log.e("JsonUtil2", e.getMessage()); } file.delete(); } finally { JsonUtil.release(access); } }
随机读取文件的字节:
/** * 工具方法,随机位置读取文件的字节 * * @param file * @param position * @param length * @return */ public static byte[] readFile(File file, int position, int length) { if (file == null || !file.exists() || !file.isFile()) { return null; } RandomAccessFile access = null; byte[] bytes = new byte[length]; try { access = new RandomAccessFile(file, "r"); access.seek(position); access.readFully(bytes, 0, length); return bytes; } catch (Exception e) { if (e.getMessage() != null) { Log.e("JsonUtil2", e.getMessage()); } return null; } finally { JsonUtil.release(access); } }
读取文件的全部字节:
/** * 工具方法,读文件的字节 * * @param file * @return */ private static byte[] readFile(File file) { if (file == null || !file.isFile() || !file.canRead()) { return null; } FileInputStream fis = null; BufferedInputStream bis = null; long fileSize = file.length(); if (fileSize > Integer.MAX_VALUE) { System.out.println("file too big..."); return null; } ByteArrayOutputStream bos = new ByteArrayOutputStream((int) fileSize); try { bis = new BufferedInputStream(new FileInputStream(file)); int buf_size = 1024; byte[] buffer = new byte[buf_size]; int len = 0; while (-1 != (len = bis.read(buffer, 0, buf_size))) { bos.write(buffer, 0, len); } return bos.toByteArray(); } catch (IOException e) { Log.e("JsonUtil2", "read bytes form file error"); return null; } finally { JsonUtil.release(fis); JsonUtil.release(bis); } }
从文件中读取出字符串:
/** * 从给定文件中读出字符串 * * @param file * @return string, null 如果文件不存在 */ public static String readFromFile(File file) { if (file == null || !file.exists() || !file.isFile()) { Log.e("JsonUtil", "cannot read file"); return null; } StringBuilder sBuilder = new StringBuilder(""); FileReader fr = null; BufferedReader br = null; char[] buffer = new char[1024]; int length = -1; try { fr = new FileReader(file); br = new BufferedReader(fr); while ((length = br.read(buffer)) != -1) { sBuilder.append(buffer, 0, length); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { release(fr); release(br); } if (sBuilder.length() == 0) {// 如果读取后长度仍为零 return null; } // Log.i("bbbbb", ""+sBuilder.toString());//test /** * 要判断第一个char是不是utf8 bom文件头 如果是则去掉第一个char */ if (getTHEChar().equals(((Character) (sBuilder.charAt(0))))) { sBuilder.deleteCharAt(0); } return sBuilder.toString(); }
释放closable资源:
/** * 释放closable的资源 * * @param closeobj */ public static void release(Closeable closeobj) { if (closeobj != null) { try { closeobj.close(); } catch (IOException e) { e.printStackTrace(); } closeobj = null; } }
写字符串到文件:
/** * 将指定字符串写入指定文件中,覆盖写 * * @param file * @param str * @param cover * 是否覆盖写,true 覆盖,false 追加 */ private static void writeToFile(File file, String str, Boolean cover) { cover = !cover; if (file == null || !file.exists() || !file.isFile()) { Log.e("JsonUtil", "cannot write file"); return; } if (str == null) { return; } FileWriter fw = null; BufferedWriter bw = null; try { fw = new FileWriter(file, cover); bw = new BufferedWriter(fw); // Log.e("JsonUtil",file.getAbsolutePath()); bw.write(str); bw.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { release(fw); release(bw); } }
有BOM的utf-8文件头:
/** * 得到utf8文件头的char * * @return */ private static Character getTHEChar() { byte a = (byte) 239;// 0xef(byte)0xef;eb byte b = (byte) 187;// (byte)0xbb;bc byte c = (byte) 191;// 0xbf;80 // char ret = // (char)(((a&0x000000ff)<<24)|(b&0x000000ff)<<16|(c&0x000000ff)<<8|0x00000000); // return (Character)ret; char[] chars = getChars(new byte[] { a, b, c }); return (Character) chars[0]; } /** * bytes转chars * * @param bytes * @return */ private static char[] getChars(byte[] bytes) { Charset cs = Charset.forName("UTF-8"); ByteBuffer bb = ByteBuffer.allocate(bytes.length); bb.put(bytes); bb.flip(); CharBuffer cb = cs.decode(bb); return cb.array(); }
文件操作常用工具方法
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。