天天看點

java 寫入txt越來越慢_Java批量寫檔案速度越來越慢

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左右;求大神幫忙~~~

展開