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; }}
原创粉丝点击