Java快速创建指定大小的文件,最多的解决办法就是循环向文件里面入固定大小的空字节,但是这种方式构建大文件性能比较低下,因此有这样两种方式可供参考:

  Java有一个类:FileChannel,查阅API发现通过这个类来实现复制文件比简单的循环读取写入可能会高效得多,很多操作系统可将字节直接从文件系统缓存传输到目标通道,而无需实际复制各字节。构建大的文件10GB,20GB,150GB,所用时间都是100毫秒左右。

/**
* 创建固定大小的文件
* @param file
* @param length
* @throws IOException
*/
public static void createFixLengthFile(File file, long length) throws IOException{
long start = System.currentTimeMillis();
FileOutputStream fos = null;
FileChannel output = null;
try {
fos = new FileOutputStream(file);
output = fos.getChannel();
output.write(ByteBuffer.allocate(1), length-1);
} finally {
try {
if (output != null) {
output.close();
}
if (fos != null) {
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
long end = System.currentTimeMillis();
System.out.println("total times "+(end-start));
}

  另外一种方式就是RandomAccessFile类, 能够更方便,更直观的实现,两者效率相差无几,大文件RandomAccessFile大约是FileChannel的一倍,但是小文件RandomAccessFile效率就要高的多了,但这应该是更推荐的一种方式。

public static void create(File file, long length) throws IOException{
long start = System.currentTimeMillis();
RandomAccessFile r = null;
try {
r = new RandomAccessFile(file, "rw");
r.setLength(length);
} finally{
if (r != null) {
try {
r.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
long end = System.currentTimeMillis();
System.out.println(end-start);
}

  完整代码示例:

public class CreateFile
{
public static void main(String[] args) throws IOException
{
String filePath = "D:\\temp\\api-auto-test\\10000-files";
File file = new File(filePath);
if(!file.exists()){
file.mkdirs();
}
long start = System.currentTimeMillis();
for(int i=0;i<10000;i++){
String fileName = "auto-test1"+i;
File f = new File(filePath,fileName);
if(!f.exists()){
createFile(f,1l);
}
}
long end = System.currentTimeMillis();
System.out.println("total times "+(end-start));
start = System.currentTimeMillis();
for(int i=0;i<10000;i++){
String fileName = "auto-test2"+i;
File f = new File(filePath,fileName);
if(!f.exists()){
createFixLengthFile(f,1l);
}
}
end = System.currentTimeMillis();
System.out.println("total times "+(end-start));
} public static void createFixLengthFile(File file, long length) throws IOException{
FileOutputStream fos = null;
FileChannel output = null;
try {
fos = new FileOutputStream(file);
output = fos.getChannel();
output.write(ByteBuffer.allocate(1), length-1);
} finally {
try {
if (output != null) {
output.close();
}
if (fos != null) {
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
} private static void createFile(File file, long length) throws IOException{
RandomAccessFile ff = null;
try{
ff = new RandomAccessFile(file,"rw");
ff.setLength(length);
}finally{
if (ff != null){
try{
ff.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
}
}

Java构建指定大小文件的更多相关文章

  1. dd 生成指定大小文件

    d命令可以轻易实现创建指定大小的文件,如 dd if=/dev/zero of=test bs=1M count=1000 会生成一个1000M的test文件,文件内容为全0(因从/dev/zero中 ...

  2. Linux下自动清理超过指定大小文件

    作者:邓聪聪 扫描某个目录下的文件,发现超过指定大小即清空 1)扫描目录下的文件 2)判断文件大小 3)清空大于指定文件的内容 以byte为单位显示文件大小,然后和20M大小做对比. 20M换算成字节 ...

  3. 使用Java创建指定大小的空文件夹

    /** 方法一 * 创建固定大小的文件 * @param file * @param length * @throws IOException */ public static void create ...

  4. Linux下自动清理超过指定大小文件的方法

    由于线上业务用的squid,根据经验值如果长时间运行则缓存目录下的swap.state会慢慢变大,一旦超过60M,squid的性能就会急剧下降,因此需要定时去清理大于60M的swap.state文件. ...

  5. Java 清除指定目录文件夹下文件

    public static void clearFiles(String filePath){ File scFileDir = new File(filePath); File TrxFiles[] ...

  6. java判断指定路径文件夹是否存在,若不存在则创建新的文件夹

    File file = new File(dirPath); if (!file.exists()) { file.mkdirs(); }

  7. Java、Linux、Win 快速生成指定大小的空文件

    Linux dd 命令: dd if=/dev/zero of=<fileName> bs=<一次复制的大小> count=<复制的次数> 生成 50 MB 的空文 ...

  8. Linux下删除空文件,删除指定大小的文件

    Linux下批量删除空文件(大小等于0的文件)的方法: find . -name "*" -type f -size 0c | xargs -n 1 rm -f 用这个还可以删除指 ...

  9. 解决java.io.IOException: Cannot run program "cygpath": CreateProcess error=2, 系统找不到指定的文件 的错误

    一.外部环境: 系统环境:Windows 8 磁盘分区:只有C盘 开发环境:IntelliJ IDEA Community Edition 2016.1.3(64) 执行代码:rdd.saveAsTe ...

随机推荐

  1. pgsql restart

    /etc/init.d/postgresql restart

  2. Delphi中TApplication详解(转仅供自己参考)

    转自:http://blog.sina.com.cn/s/blog_4d6f55d90100bmv9.html TApplication是用于Delphi应用程序的类型,该类在单元forms中声明.T ...

  3. C语言复习:文件操作

    文件操作专题 C语言文件读写概念 文件分类 按文件的逻辑结构: 记录文件:由具有一定结构的记录组成(定长和不定长) 流式文件:由一个个字符(字节)数据顺序组成 按存储介质: 普通文件:存储介质文件(磁 ...

  4. 趣味编程:静夜思(Python版)

    from itertools import groupby def verticalWriting(txt, offset): l = lambda x: x[0] % offset for (_, ...

  5. Delphi在Listview中加入Edit控件

    原帖 : http://www.cnblogs.com/hssbsw/archive/2012/06/03/2533092.html Listview是一个非常有用的控件,我们常常将大量的数据(如数据 ...

  6. python 拷贝 深拷贝 浅拷贝 赋值

    t = [1,["a","b"]] t_bak = t t_cop = copy.copy(t) t_deep = copy.deepcopy(t) print ...

  7. Hibernate学习笔记3.3(Hibernate组建映射2)

    多对多 相当于一个老师可以教多个学生,一个学生也可以有多个老师 数据表中都是再设计一个表寸相关的id 1.多对多单向 1annotation Student.java package com.bjsx ...

  8. EF AutoMaper

    Mapper.CreateMap<Source,Dest>(); 该方法已弃用,使用下面这个 Mapper.Initialize(x=>x.CreateMap<Source,D ...

  9. MongoDB分布式集群搭建

    最近在做一个关于车险的项目,由于数据量较大,实验室的Boss决定采用HBase+ES/MongoDB这两种方案,并做性能对比,本人负责MongoDB方案.为了满足海量数据的存储要求,需要搭建一个分布式 ...

  10. python3进行汉字和unicode码的转换

    输出某个unicode码对应的汉字和某个汉字对应的unicode编码. # -*- coding=UTF-8 -*- str1 = "\u6000"#某个汉字的unicode码 s ...