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();
}ide
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();
}spa
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;
}
}內存