IO流操作.MyIOHelper.可以当做Utils直接使用
来源:互联网 发布:ipad软件开发 编辑:程序博客网 时间:2024/05/22 05:25
public class MyIOHelper implements InterfaceIO{ // 缓冲数组的大小 private static final int SIZE = 1024 * 8; /* * 读取文本文件 */ @Override public String readTextFile(String filePath) { StringBuilder sb = new StringBuilder(); char[] cbuf = new char[1024]; FileReader fr = null; try { fr = new FileReader(filePath); int len; while((len = fr.read(cbuf)) != -1) { sb.append(new String(cbuf, 0, len)); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if(fr != null) { try { fr.close(); } catch (IOException e) { e.printStackTrace(); } } } return sb.toString(); } /* * 读取非文本文件 */ @Override public byte[] readBinaryFile(String filePath) { BufferedInputStream bis = null; ByteArrayOutputStream baos = null; try { bis = new BufferedInputStream(new FileInputStream(filePath)); baos = new ByteArrayOutputStream(); int len; byte[] buf = new byte[SIZE]; while((len = bis.read(buf)) != -1) { baos.write(buf, 0, len); baos.flush(); } return baos.toByteArray(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if(baos != null) { try { baos.close(); } catch (IOException e) { e.printStackTrace(); } } if(bis != null) { try { bis.close(); } catch (IOException e) { e.printStackTrace(); } } } return null; } /* * 将文本文件写入到fileName所指定的路径中 */ @Override public boolean writeTextFile(String content, String fileName) { BufferedWriter bw = null; try { bw = new BufferedWriter(new FileWriter(fileName)); bw.write(content); bw.flush(); return true; } catch (IOException e) { e.printStackTrace(); } finally { if(bw != null) { try { bw.close(); } catch (IOException e) { e.printStackTrace(); } } } return false; } /* * 将非文本文件写入到filePath路径中 */ @Override public boolean writeBinaryFile(byte[] data, String filePath) { BufferedOutputStream bos = null; try { bos = new BufferedOutputStream(new FileOutputStream(filePath)); bos.write(data); bos.flush(); return true; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if(bos != null) { try { bos.close(); } catch (IOException e) { e.printStackTrace(); } } } return false; } /* * 复制文本文件 */ @Override public boolean copyTextFile(String filePath, String destFilePath) { BufferedReader br = null; BufferedWriter bw = null; try { br = new BufferedReader(new FileReader(filePath)); bw = new BufferedWriter(new FileWriter(destFilePath)); String str = ""; while((str = br.readLine()) != null) { bw.write(str); bw.newLine(); bw.flush(); } return true; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { // 关流 if(bw != null) { try { bw.close(); } catch (IOException e) { e.printStackTrace(); } } if(br != null) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } return false; } /* * 复制二进制文件 */ @Override public boolean copyBinaryFile(String filePath, String destFilePath) { BufferedInputStream bis = null; BufferedOutputStream bos = null; try { bis = new BufferedInputStream(new FileInputStream(filePath)); bos = new BufferedOutputStream(new FileOutputStream(destFilePath)); byte[] buf = new byte[SIZE]; int len = 0; while((len = bis.read(buf)) != -1) { bos.write(buf, 0, len); bos.flush(); } return true; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if(bos != null) { try { bos.close(); } catch (IOException e) { e.printStackTrace(); } } if(bis != null) { try { bis.close(); } catch (IOException e) { e.printStackTrace(); } } } return false; } /* * 删除文件 */ @Override public boolean deleteFile(String filePath) { File file = new File(filePath); boolean flag = file.delete(); return flag; } @Override public boolean isExistFile(String filePath) { File file = new File(filePath); boolean flag = file.exists(); return flag; } /* * 获取文件的扩展名 */ @Override public String getFileExtension(String filePath) { File file = new File(filePath); StringBuilder sb = new StringBuilder(); String name = file.getName(); String[] strs = name.split("\\."); sb.append(strs[strs.length - 1]); return sb.toString(); } @Override public byte[] streamToByte(InputStream is) { ByteArrayOutputStream baos = null; try { baos = new ByteArrayOutputStream(); byte[] buf = new byte[SIZE]; int len = 0; while((len = is.read(buf)) != -1) { baos.write(buf, 0, len); baos.flush(); } return baos.toByteArray(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } /* * 将输入流中的数据按照给定的字符集编码方式转换成字符串返回 */ @Override public String streamToString(InputStream is, String charsetName) { ByteArrayOutputStream baos = null; baos = new ByteArrayOutputStream(); int len = 0; byte[] buf = new byte[SIZE]; try { while((len = is.read(buf)) != -1) { baos.write(buf, 0, len); baos.flush(); } return baos.toString(charsetName); } catch (IOException e) { e.printStackTrace(); } finally { if(baos != null) { try { baos.close(); } catch (IOException e) { e.printStackTrace(); } } } return null; } /* * 将给定的字符串,转换字节输出流返回 */ @Override public InputStream stringToInputStream(String str) { ByteArrayInputStream bais = null; bais = new ByteArrayInputStream(str.getBytes()); return bais; }}
阅读全文
0 0
- IO流操作.MyIOHelper.可以当做Utils直接使用
- debugfs范例 可以当做模板使用
- stm32f103系列芯片JTAG口当做普通IO使用
- stm32f103系列芯片P15端口当做普通IO使用
- 动态开辟的链表 可以当做数组使用
- 一个简单的加载类,可以当做autoload使用
- 使用WinIo直接访问IO
- 使用WinIo直接访问IO
- 使用DMA的直接IO
- 使用iptables当做网关
- 闪光灯当做手电筒使用
- 关于使用FileMapping和直接操作File+缓存的IO性能对比
- css 制作的三级菜单也可以当做二级菜单使用
- 使用Zip Utils 解压文件的操作示例
- 当我把一个"结构体"在类中当做属性后, 在实用中可以直接读取结构体成员, 但不能直接写入.
- IO:*使用缓冲区数组。 *使用就是可以操作数组的读写方法。
- Java文件操作Utils
- 没有表单的网页,所有元素都可以当做表单元素,通过AJAX提交,JQ拓展,支持连续操作
- Java面经_Servlet
- Qt char*与QString互转
- 编辑距离
- curl
- java判断字符串是否为空的方法总结
- IO流操作.MyIOHelper.可以当做Utils直接使用
- 蓝桥杯 算法训练 动态数组使用
- python 小顶堆
- 显著性水平 P值 概念解释
- 深度学习11
- 关于scrapy采集网易云音乐歌手遇到的问题
- Capstone日志2017-12-11
- spoon各个控件的说明
- Win8.1下安装ubuntu16.04双系统