JAVA創(chuàng)建固定長度文件的方法

字號:


    JAVA創(chuàng)建固定長度文件的方法,具體代碼如下:
    //
    // usage : java CreateAFile 2048 twokbytes.dat
    //
    import java.io.FileOutputStream;
    import java.io.IOException;
    public class CreateAFile {
    public static void main(String[] args) throws IOException {
    byte[] buf = new byte[8192];
    long n = Long.parseLong(args[0]);
    FileOutputStream fos = new FileOutputStream(args[1]);
    long m = n / buf.length;
    for (long i = 0; i < m; i++) {
    fos.write(buf, 0, buf.length);
    }
    fos.write(buf, 0, (int)(n % buf.length));
    fos.close();
    }
    }