public String readfile(String filename) throws Exception
{
FileInputStream in = new.FileInputStream(filename);
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] b = new byte[1024];
int len = 0;
// 将内容读到b中,读到末尾为-1
while ((len = in.read(b)) != -1)
{
// 本例子将每次读到字节数组(buffer变量)内容写到内存缓冲区中,起到保存每次内容的作用
outStream.write(b, 0, len);
}
byte[] bt = outStream.toByteArray(); // 取内存中保存的数据
fis.close();
String result = new String(bt, "UTF-8");
return result;
}
2------------------- 对于字节数组我们还可以转为字节输入流,如下面的代码
ByteArrayInputStream in = new ByteArrayInputStream(b);
3———————–拓展小知识
如上,我们还用到了字符(字符串)和字节数组之间的转换
//字节数组转换成字符串
String result = new String(bt, "UTF-8");
//字符串转换成字节数组
byte[] bt = str.getBytes("GBK");