转换UTF-8函数

来源:互联网 发布:淘宝主图如何制作 编辑:程序博客网 时间:2024/05/01 19:11
public String getTextByUTF(String name)
 {
  String strReturn = "";
  int ic;
  InputStream in = null;
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  DataOutputStream dos = new DataOutputStream(baos);
  byte[] myData;
  byte[] buffer = new byte[1024];
  try {
   in = getClass().getResourceAsStream(name);
   if (in != null) {
    while ((ic = in.read(buffer)) > 0) {
     dos.write(buffer, 0, ic);
    }
    myData = baos.toByteArray();
    strReturn = new String(myData, "UTF-8");
    in.close();
   }
   dos.close();
   baos.close();
  } catch (Exception e) {
   System.out.println("getTextByUTF   Error:" + e.toString());
  } finally {
   in = null;
   dos = null;
   baos = null;
  }
  return strReturn;
 }
原创粉丝点击