方式一:public byte [] readBytes(String filePath) {
byte [] bt = null;
FileInputStream fis = null;
try {
fis = new FileInputStream(filePath);
bt = new byte[fis.available()];
fis.read(bt);
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
return bt;
}
方式二:public byte [] readBytes(String filePath) {
byte [] bt = null;
FileInputStream fis = null;
try {
fis = new FileInputStream(filePath);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bt = new byte [1024];
while (fis.read(bt) != -1) {
baos.write(bt);
}
baos.close();
fis.close();
bt = baos.toByteArray();
} catch (IOException e) {
e.printStackTrace();
}
return bt;
}请问这两种方式那种更好,是否与文件大小有关?
byte [] bt = null;
FileInputStream fis = null;
try {
fis = new FileInputStream(filePath);
bt = new byte[fis.available()];
fis.read(bt);
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
return bt;
}
方式二:public byte [] readBytes(String filePath) {
byte [] bt = null;
FileInputStream fis = null;
try {
fis = new FileInputStream(filePath);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bt = new byte [1024];
while (fis.read(bt) != -1) {
baos.write(bt);
}
baos.close();
fis.close();
bt = baos.toByteArray();
} catch (IOException e) {
e.printStackTrace();
}
return bt;
}请问这两种方式那种更好,是否与文件大小有关?
解决方案 »
- 关于log4j.xml很奇怪的一个问题:输出日志文件绝对路径改为相对路径tomcat就卡住了
- hibernate 3.6.10 怎么没有annotation文档
- sso -cas jndi验证页面跳转不到你想要登陆的页面
- java如何解析指向xml的url
- 请问高人:如何配置方可解决:JBUILDER2005工程重新编译后,必须重新启动TOMCAT5.0.30的麻烦?
- 求助:hibernate one-to-many 怎样查询
- 我被网上的文章搞糊了,IIS和Resin到底如何集成
- 为什么精华区里的东西这么少?只有一条,远远不如java-cn、javaworld、javaresearch等网,真是爱之深恨之切啊!
- 关于soap的小问题,送分哦。
- J2EE启动的问题
- 咨询下,这些字段数据库是怎么建的?
- 纠结,蛋疼!!!
异常后 你的流怎么关掉
只取流中可用的数据,并不是全部的数据,具体你可以去看api。所以第一种会留下很大的隐患。
第二种常用,读一段写一段。但是文件很大会把jvm内存爆掉,到时候就outofmemory了。另外你的close最好都写在finally里面,