android中文件IO
?
Android中文件的读写/** * 写入数据 * @param fs * @param content */ public void fileWrite(FileOutputStream fos,String content){ byte[] contentByteArray = content.getBytes(); try { fos.write(contentByteArray); } catch (IOException e1) { e1.printStackTrace(); } try {//关闭流 fos.close(); } catch (IOException e) { e.printStackTrace(); } }?
?
* 读数据 * @param fis * @return */ public String fileRead(FileInputStream fis){ ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = -1; try { while((len=(fis.read(buffer))) != -1){ baos.write(buffer,0,len); } } catch (IOException e) { e.printStackTrace(); } String result = new String(baos.toByteArray());//System.out.println(result); try { baos.close(); fis.close(); } catch (IOException e) { e.printStackTrace(); } return result; }?
ByteArrayOutputStream:?????? 此类实现了一个输出流,其中的数据被写入一个 byte 数组。
?????? publicvoid write(byte[] b,int off,int len)???? 将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此 byte 数组输出流。
可参考代码:FileIO
?
?
?
?
