通过网络传输? 还是用ftp吧.
解决方案 »
- 利用java applet如何实现java web中文件的下载,并显示一个下载进度条?
- 多个开源系统的单点登录和权限管理问题,需企业级可用的
- 如何通过hibernate3来取得db2 sequence的下一个值?
- ibatis 如果在配置文件配置了连接池 在代码中还需要写代码生成数据源吗?????
- 一个java.sql.Connection可以被共享吗
- 用java作网络应用服务器
- XML转化为bean出现错误,急,在线等
- 求助:如何建立一个jdbc连接工厂?
- 下面有段源程序,有几个概念搞混淆,请指点谢谢!
- 求Java电商秒杀系统深度优化 从容应对亿级流量挑战下载地址
- 在线求助,jbuilder下axis的web service文件传送(只能够上传,返回文件时候报错)
- jsp+xml怎么在B/S的模式下实现树型目录(如windows的资源管理器)
if (oldF.exists()&& oldF.isFile()) { File nf = null;
File of = null;
byte[] bytes = null;
try { FileInputStream fis = new FileInputStream(oldF);
String path = newF.getAbsolutePath(); nf = new File(path);
if (!nf.exists()) {
File dir = new File(path.substring(0, path.lastIndexOf('\\')));
if (!dir.exists())
dir.mkdirs();
} nf.createNewFile(); FileOutputStream fos = new FileOutputStream(nf); bytes = new byte[fis.available()];
fis.read(bytes);
fos.write(bytes);
fis.close();
fos.close();
}
catch (Exception e) {
System.out.print("error:"+e);
}
} }执行到 bytes = new byte[fis.available()]; 时报
java.lang.OutOfMemoryErrorException in thread "main"
可以定个死数,不要太大。
读后判断读到多少就写多少。
bytes = new byte[1024];
int length = (int)oldF.length();
int count = length/1024;
System.out.println(count+"****************");
for(int i=0;i<count;i++){
fis.read(bytes);
fos.write(bytes);
}
int s = length%1024;
if(s!=0){
bytes = new byte[s];
fis.read(bytes);
fos.write(bytes); }
但是这样写要循环执行几十万次,有没有好一点的方法呢??
MappedByteBuffer out =
new RandomAccessFile("C:/aa/test.txt", "rw").getChannel().map(FileChannel.MapMode.READ_WRITE, 0, length);MappedByteBuffer 与 FileInputStream/FileOutputStream 相比 那个效率更高些??
我给你写一个....
....
InputStream in=new FileInputStream("fromfilename");
OutputStream out=new FileOutputStream("tofilename");
byte[] buff=new byte[2048]//经过我测试,2048性能相对不错
int len;
while((len=in.read(buff))>-1){
out.write(buff,0,len);
}
in.flush();
in.close();
out.close();
fis.read(bytes);
fos.write(bytes);
因为不一定会把数组读满,特别是在对网络读取的时候更是如此,应该是读多少,写多少