首先建立一个文件,之后再写入一定量的垃圾信息,这种方法我也会,但就是太慢了,怎样能够快速地生成一个文件,就像网络蚂蚁刚开始下载是那样?
谢谢大家!

解决方案 »

  1.   

    那样要涉及到底层磁盘I/O操作的,Java恐怕是做不来的吧
      

  2.   

    RandomAccessFile r = new RandomAccessFile(file, "rw");
    r.setLength(size);
    r.close();
      

  3.   

    用gtlang78()的方法生成一个500M的文件需要17秒左右,我刚才试了一下网络蚂蚁生成一个600M的文件也就1、2秒,人家到底是怎么弄的呢?
      

  4.   

    import java.io.*;public class test
    {
        public test()
        {
        }    public static void main(String[] args)
        {
            try {
                long lo = System.currentTimeMillis();
                java.io.RandomAccessFile raf = new java.io.RandomAccessFile("d:/abc.txt", "rw");
                raf.setLength(1024 * 1024 * 1024);
                raf.close();
                System.out.print(System.currentTimeMillis() - lo);
            } catch (FileNotFoundException ex) {
                ex.printStackTrace();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
    cmd:D:\test\classes>java test
    0
    楼主我测试: 需要时间为0
      

  5.   

    楼上的我运行你那段程序怎么要用34秒?难道是SDK版本不同吗?
    另外我看API手册上讲:如果setLength的参数比文件实际长度大,“the contents of the extended portion of the file are not defined”,但是我生成的文件为什么所有的字节都是“0”?我估计时间都浪费在写文件内容上了,但问题是怎样让setLength仅生成文件呢?还是有别的方法?
    谢谢各位F:\>java -version
    java version "1.5.0_03"
    Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_03-b07)
    Java HotSpot(TM) Client VM (build 1.5.0_03-b07, mixed mode, sharing)F:\>java test
    34203
    F:\>
      

  6.   

    谁有详细点的 JDK 啊谢谢了
      

  7.   

    import java.io.*;public class test
    {
        public test()
        {
        }    public static void main(String[] args)
        {
            try {
                long lo = System.currentTimeMillis();
                java.io.RandomAccessFile raf = new java.io.RandomAccessFile("d:/abc.txt", "rw");
                raf.setLength(1024 * 1024 * 1024);
                System.out.print(System.currentTimeMillis() - lo);
            } catch (FileNotFoundException ex) {
                ex.printStackTrace();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }如果楼主要使用这个方法,建议搂主创建空文件后不要关闭.传给下载的程序直接使用RandomAccessFile  raf填冲文件类容