java io 性能问题,急用

qingkong16111 2004-06-20 11:26:27
程序实现文件读写,但是速度慢,请问怎么才能优化一下。
try{

if ( simpleFile.exists() && simpleFile.isFile() )
{
//FileInputStream fin = new FileInputStream(simpleFile.getAbsolutePath());
DataInputStream fin = new DataInputStream(new BufferedInputStream(
new FileInputStream(simpleFile.getAbsolutePath())));
System.out.println("enter0");
int index1 = simpleFile.getAbsolutePath().indexOf(".");
String name = simpleFile.getAbsolutePath().substring(0, index1);
SimpleFile temp = new SimpleFile(name);
temp.create();


DataOutputStream fout = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(name + "\\" +
simpleFile.getName() + index)));
System.out.println("original file name");
System.out.println(name + "\\" + simpleFile.getName() + index);
byte[] buf = new byte[1024*1024];
fin.skip(start);
while (size >= 1024*1024) {

int readbytes = fin.read(buf,0,1024*1024);
System.out.println("readbytes :" + readbytes);
size -= readbytes;
fout.write(buf);
}
System.out.println("size :" + size);
fin.read(buf, 0, (int) size);
fout.write(buf, 0, (int) size);
fout.flush();
fin.close();
fout.close();
}

}catch(Exception e){
e.printStackTrace();
System.out.println(e.getMessage());
}
...全文
60 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
CsdnRob 2004-07-02
  • 打赏
  • 举报
回复
该问题我也已经解决,有必要于我联系
maowu 2004-06-21
  • 打赏
  • 举报
回复
如果是单纯的大文件拷贝,这个方法最好:(jdk1.4以上)
import java.nio.channels.*

try {
// Create channel on the source
FileChannel srcChannel = new FileInputStream("srcFilename").getChannel();

// Create channel on the destination
FileChannel dstChannel = new FileOutputStream("dstFilename").getChannel();

// Copy file contents from source to destination
dstChannel.transferFrom(srcChannel, 0, srcChannel.size());

// Close the channels
srcChannel.close();
dstChannel.close();
} catch (IOException e) {
}
qingkong16111 2004-06-21
  • 打赏
  • 举报
回复
谢谢maowu(猫呜),我用来分割文件的,那是一部分代码。
dilidilidi 2004-06-21
  • 打赏
  • 举报
回复
同意maowu(猫呜),大文件用nio包中的api比较好,因为那是基与块的操作,而且channel是双向的。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧