-2

如何使用 Java 创建一个大小为 250KB 的二进制文件和另一个 1MB 的二进制文件?可能吗?

4

3 回答 3

6
 new RandomAccessFile("file.file", "rw").setLength(1048576);
于 2012-06-11T18:35:39.097 回答
1
FileOutputStream s = new FileOutputStream("a.bin");
byte[] buf = new byte[250*1024];
s.write(buf);
s.flush();
s.close();
于 2012-06-11T18:35:52.580 回答
0
     public voidwriteFile(String path, int bytes) throws Exception {
    OutputStream outputStream = null;
    File f=null;
    try {
        f = new File(path + "/tmp.txt");
        f.createNewFile();
        byte[] buf = new byte[bytes];
        outputStream = new BufferedOutputStream(new FileOutputStream(f));
        outputStream.write(buf);
        outputStream.flush();
    } catch (Exception e) {
        throw new Exception();
    } finally {
        try {
            if(f!=null){
                f.delete();
            }
            outputStream.close();
        } catch (IOException e) {
        }
    }
}
于 2015-03-25T06:19:44.927 回答