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. servlet编码问题

    建议每个servlet都写上    request.setCharacterEncoding("UTF-8")

  2. U盘无法访问

    U盘无法访问 方法/步骤   首先,Win+R,打开“运行”窗口.   在打开的运行窗口中,输入cmd回车     这时会打开这样的一个窗口   这时输入chkdsk g: /f 需要说明的是,g这个 ...

  3. linux 文件系统之superblock

    为了实际测试这个pagecache和对裸盘操作的区别,我一不小心敲错命令,将一个磁盘的super_block给抹掉了,全是0, dd if =/dev/zero of=/dev/sda2 bs=409 ...

  4. 解决eclipse+adt出现的 loading data for android 问题

    因为公司最近做的项目中有用到一些第三方demo,蛋疼的是这些demo还比较旧...eclipse的... 于是给自己的eclipse装上了ADT插件,但是...因为我的eclipse比较新,Versi ...

  5. JQuery 基本知识

    一.简介 JQuery是继prototype之后又一个优秀的Javascript库.它是轻量级的js库 ,它兼容CSS3,还兼容各种浏览器(IE 6.0+, FF1.5+, Safari 2.0+,  ...

  6. 2018面向对象程序设计(Java)第7周学习指导及要求

    第7周学习指导及要求(2018.10.11-2018.10.14)   学习目标 深入理解OO程序设计的特征:继承.多态: 熟练掌握Java语言中基于类.继承技术构造程序的语法知识: 利用继承定义类设 ...

  7. K-means算法的实现

    K-MEANS算法是一种经典的聚类算法,在模式识别得到了广泛的应用.算法中有两个关键问题需要考虑:一是如何评价对象的相似性,通常用距离来度量,距离越近越相似:另外一个是如何评价聚类的效果,通常采用误差 ...

  8. 消息队列RabbitMQ与Spring

    1.RabbitMQ简介 RabbitMQ是流行的开源消息队列系统,用erlang语言开发.RabbitMQ是AMQP(高级消息队列协议)的标准实现. 官网:http://www.rabbitmq.c ...

  9. SpringJDBC数据库的基本使用

    SpringJDBC的基础使用部分内容 云笔记项目数据库部分采用的是Spring-MyBatis,前面学过了JDBC,SpringJDBC,Mybatis和Spring-MyBatis,有必要重新复习 ...

  10. 去掉Android新建项目的顶部标题

    [ 去掉Android新建项目的顶部标题] 使用NoActionBar的Theme即可. 参考:http://blog.csdn.net/u012246458/article/details/5299 ...