好像我编的测试用例没有发现什么性能上的问题。我生成的输出文件经过几次的append之后,已经达到了10.02MB!可是性能还是没有因为文件的增大而降低。
因为你使用了BufferedWriter,所以所有的IO操作都是缓冲式的,因此有时候会用相对长的时间,不过整个程序的平均IO操作时间还是比较短。测试用例如下:
<-------------------------------import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.IOException;public class IOTimeTest
{
public static void main(String[] args)
throws IOException
{
BufferedWriter bw=null;
long[] timeCost=new long[100];
long totalTime=0; for(int i=0;i<100;i++)
{
long startTime=System.currentTimeMillis(); bw=new BufferedWriter(new FileWriter("IOTimerTest.txt",true)); for(int j=0;j<500;j++)
bw.write(j);
bw.close(); long endTime=System.currentTimeMillis(); timeCost[i]=endTime-startTime; totalTime+=timeCost[i];
}
for(int i=0;i<100;i++)
{
if(i%20==0)
System.out.println();
System.out.print(timeCost[i]+" ");
} System.out.println("\nTotal Time cost:"+totalTime+"(ms)");
System.out.println("\nAverage Time cost:"+(double)totalTime/100+"(ms)");
}
}-------------------------------------->我计算出了一次程序运行的每次IO操作时间,总时间,平均时间,下面是程序运行三次的结果:15 0 0 0 0 0 0 0 0 0 0 0 0 16 0 0 0 0 0 0
0 0 0 0 0 0 0 16 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 15 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 16 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Total Time cost:78(ms)Average Time cost:0.78(ms)-------------------------------------------------0 0 16 0 0 0 0 0 0 0 0 0 0 0 0 15 0 0 0 0
0 0 0 16 0 0 0 0 15 0 0 16 0 0 0 0 0 0 16 0
0 0 0 0 0 0 0 0 0 0 15 0 0 0 0 0 0 0 0 0
0 0 0 16 0 0 0 0 16 0 0 0 0 0 0 0 0 0 0 0
0 15 0 0 0 0 0 0 0 0 0 0 0 0 0 0 16 0 0 0
Total Time cost:172(ms)Average Time cost:1.72(ms)------------------------------------------------0 0 16 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 16
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 15 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 16 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 16 0 0 0 0 0
Total Time cost:79(ms)Average Time cost:0.79(ms)有什么问题在发短信给我^_^
因为你使用了BufferedWriter,所以所有的IO操作都是缓冲式的,因此有时候会用相对长的时间,不过整个程序的平均IO操作时间还是比较短。测试用例如下:
<-------------------------------import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.IOException;public class IOTimeTest
{
public static void main(String[] args)
throws IOException
{
BufferedWriter bw=null;
long[] timeCost=new long[100];
long totalTime=0; for(int i=0;i<100;i++)
{
long startTime=System.currentTimeMillis(); bw=new BufferedWriter(new FileWriter("IOTimerTest.txt",true)); for(int j=0;j<500;j++)
bw.write(j);
bw.close(); long endTime=System.currentTimeMillis(); timeCost[i]=endTime-startTime; totalTime+=timeCost[i];
}
for(int i=0;i<100;i++)
{
if(i%20==0)
System.out.println();
System.out.print(timeCost[i]+" ");
} System.out.println("\nTotal Time cost:"+totalTime+"(ms)");
System.out.println("\nAverage Time cost:"+(double)totalTime/100+"(ms)");
}
}-------------------------------------->我计算出了一次程序运行的每次IO操作时间,总时间,平均时间,下面是程序运行三次的结果:15 0 0 0 0 0 0 0 0 0 0 0 0 16 0 0 0 0 0 0
0 0 0 0 0 0 0 16 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 15 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 16 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Total Time cost:78(ms)Average Time cost:0.78(ms)-------------------------------------------------0 0 16 0 0 0 0 0 0 0 0 0 0 0 0 15 0 0 0 0
0 0 0 16 0 0 0 0 15 0 0 16 0 0 0 0 0 0 16 0
0 0 0 0 0 0 0 0 0 0 15 0 0 0 0 0 0 0 0 0
0 0 0 16 0 0 0 0 16 0 0 0 0 0 0 0 0 0 0 0
0 15 0 0 0 0 0 0 0 0 0 0 0 0 0 0 16 0 0 0
Total Time cost:172(ms)Average Time cost:1.72(ms)------------------------------------------------0 0 16 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 16
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 15 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 16 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 16 0 0 0 0 0
Total Time cost:79(ms)Average Time cost:0.79(ms)有什么问题在发短信给我^_^
String s = ...
RandomAccessFile file = new RandomAccessFile("a.txt", "rw");
file.seek(file.length() - 1);
file.writeBytes(s);
file.close();
import java.io.BufferedWriter;
import java.io.IOException;/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/public class Untitled1
{
public static void main(String[] args)throws IOException
{
BufferedWriter bw=null; long[] timeCost=new long[3000];
long totalTime=0; for(int i=0;i<3000;i++)
{
long startTime=System.currentTimeMillis(); bw=new BufferedWriter(new FileWriter("C:\\Test\\IOTimerTest"+i+".txt",true)); for(int j=0;j<500;j++)
bw.write(j); bw.close(); long endTime=System.currentTimeMillis(); timeCost[i]=endTime-startTime; totalTime+=timeCost[i];
} /* for(int i=0;i<3000;i++)
{
if(i%20==0)
System.out.println();
System.out.print(timeCost[i]+" ");
}*/ System.out.println("\nTotal Time cost:"+totalTime+"(ms)");
System.out.println("\nAverage Time cost:"+(double)totalTime/100+"(ms)");
}
}