Java批量写文件速度越来越慢
publicstaticvoidwriteFile(Filetarget,byte[]buffer){try{longstartTime=System.currentTi...
public static void writeFile(File target, byte[] buffer)
{
try
{
long startTime = System.currentTimeMillis();
OutputStream fos = new FileOutputStream(target);
int length = 0;
int writeLen = 512 * 1024;
int count = buffer.length/writeLen;
int lastLen = buffer.length%writeLen;
for (int i = 0; i < count; ++i)
{
fos.write(buffer, length, writeLen);
length += writeLen;
}
if (lastLen > 0)
{
fos.write(buffer, length, lastLen);
}
fos.close();
fos = null;
long endTime = System.currentTimeMillis();
System.out.println("(copy file use " + (endTime - startTime) + ") ms ");
}
catch (FileNotFoundException e)
{
// TODO 自动生成的 catch 块
e.printStackTrace();
}
catch (IOException e)
{
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
public static void main(String[] args)
{
try
{
byte[] filebytes = FileUtils.toByteArray("F:/JavaTest/yingzhiren/shenmo_ew3118.apk.unsign.apk");
String targetFile = "F:/JavaTest/yingzhiren/apk/shenmo_ew3118";
String[] packetids = new String[]
{"20001","20002","20003","20004","20005","20006","20007","20008","20009","20010","20011","20012","20013","20014","20015","20016","20017","20018","20019","20020","20021","20022","20023","20024","20025","20026","20027","20028","20029","20030"};
File file = null;
for (String id:packetids)
{
String name = targetFile + id + ".apk";
file = new File(name);
writeFile(file, filebytes);
file = null;
}
}
catch (IOException e)
{
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
我将一个文件读取出来保存在buffer中,然后写30份一样的文件,结果写的速度,前面10个很快,后面的就越来越慢了(前面10个差不多100多毫秒,后面的就要1500毫秒左右),求原因!文件大小为100M左右;求大神帮忙~~~ 展开
{
try
{
long startTime = System.currentTimeMillis();
OutputStream fos = new FileOutputStream(target);
int length = 0;
int writeLen = 512 * 1024;
int count = buffer.length/writeLen;
int lastLen = buffer.length%writeLen;
for (int i = 0; i < count; ++i)
{
fos.write(buffer, length, writeLen);
length += writeLen;
}
if (lastLen > 0)
{
fos.write(buffer, length, lastLen);
}
fos.close();
fos = null;
long endTime = System.currentTimeMillis();
System.out.println("(copy file use " + (endTime - startTime) + ") ms ");
}
catch (FileNotFoundException e)
{
// TODO 自动生成的 catch 块
e.printStackTrace();
}
catch (IOException e)
{
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
public static void main(String[] args)
{
try
{
byte[] filebytes = FileUtils.toByteArray("F:/JavaTest/yingzhiren/shenmo_ew3118.apk.unsign.apk");
String targetFile = "F:/JavaTest/yingzhiren/apk/shenmo_ew3118";
String[] packetids = new String[]
{"20001","20002","20003","20004","20005","20006","20007","20008","20009","20010","20011","20012","20013","20014","20015","20016","20017","20018","20019","20020","20021","20022","20023","20024","20025","20026","20027","20028","20029","20030"};
File file = null;
for (String id:packetids)
{
String name = targetFile + id + ".apk";
file = new File(name);
writeFile(file, filebytes);
file = null;
}
}
catch (IOException e)
{
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
我将一个文件读取出来保存在buffer中,然后写30份一样的文件,结果写的速度,前面10个很快,后面的就越来越慢了(前面10个差不多100多毫秒,后面的就要1500毫秒左右),求原因!文件大小为100M左右;求大神帮忙~~~ 展开
1个回答
展开全部
可能是你频繁创建并写文件导致磁盘跟不上了吧..,你在for循环里面加个Thread.sleep(1000),每个文件的复制时间应该不会出超过1秒的了.另外我用固态硬盘试了试,后面几个也会变慢,不过好一点,300多ms.
追问
是的,我目前采取的办法也是在里面睡一会....我这边睡个500ms速度就正常了~~~
不过,我整个程序是为了追求速度的,所以,我就想看看有什么办法可以解决
追答
如果是磁盘遇到瓶颈,那就不好解决了,而且现在的问题是还没搞清楚磁盘到底遇到了什么问题..,另外就算搞清楚了,你也没办法通过java来解决(如果是底层问题的话).其实你sleep 500ms本来就是一种解决方案.就好像机枪不能一直连射,要休息一下,而你就是写程序控制机枪的人(你能通过程序来改进机枪的耐热性能吗)
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询