IO流 *** 作.MyIOHelper.可以当做Utils直接使用

IO流 *** 作.MyIOHelper.可以当做Utils直接使用,第1张

概述public class MyIOHelper implements InterfaceIO{ // 缓冲数组的大小 private static final int SIZE = 1024 * 8; /* * 读取文本文件 */ @Override public String readTextFile(String filePath) { StringBuilder sb = n
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,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,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,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,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; }}
总结

以上是内存溢出为你收集整理的IO流 *** 作.MyIOHelper.可以当做Utils直接使用全部内容,希望文章能够帮你解决IO流 *** 作.MyIOHelper.可以当做Utils直接使用所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1089416.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存