閱讀233 返回首頁    go 技術社區[雲棲]


Java中IOUtils

public class IOUtils
{
 public static byte[] getByte(InputStream is) throws Exception
 {
  ByteArrayOutputStream bos = new ByteArrayOutputStream();
  byte[] buffer = new byte[1024];
  int len = 0;
  while ((len = is.read(buffer)) != -1)
  {
   bos.write(buffer);
  }
  return bos.toByteArray();
 }


 public static String getString(InputStream is) throws Exception
 {
  ByteArrayOutputStream bos = new ByteArrayOutputStream();
  byte[] buffer = new byte[1024];
  int len = 0;
  while ((len = is.read(buffer)) != -1)
  {
   bos.write(buffer);
  }
  return bos.toString().trim();
 }


 public static String read(String filename) throws Exception
 {
  FileInputStream fis = new FileInputStream(filename);
  ByteArrayOutputStream outStream = new ByteArrayOutputStream();
  byte[] buffer = new byte[1024];
  int len = 0;
  // 將內容讀到buffer中,讀到末尾為-1
  while ((len = fis.read(buffer)) != -1)
  {
   // 將每次讀到字節數組(buffer變量)內容寫到內存緩衝區中,起到保存每次內容的作用
   outStream.write(buffer, 0, len);
  }
  byte[] data = outStream.toByteArray(); // 取內存中保存的數據
  fis.close();
  String result = new String(data, "UTF-8");
  return result;
 }
}

最後更新:2017-04-03 16:49:08

  上一篇:go maven私服nexus-bundle的安裝和啟動
  下一篇:go 安裝ttf格式字體