publicstaticvoidwriteFile(Filetarget,byte[]buffer){try{longstartTime=System.currentTimeMillis();OutputStreamfos=newFileOutputStream(target);intlength=0;intwriteLen=512*10...
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左右;求大神帮忙~~~
展开