Java构建指定大小文件
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构建指定大小文件的更多相关文章
- dd 生成指定大小文件
d命令可以轻易实现创建指定大小的文件,如 dd if=/dev/zero of=test bs=1M count=1000 会生成一个1000M的test文件,文件内容为全0(因从/dev/zero中 ...
- Linux下自动清理超过指定大小文件
作者:邓聪聪 扫描某个目录下的文件,发现超过指定大小即清空 1)扫描目录下的文件 2)判断文件大小 3)清空大于指定文件的内容 以byte为单位显示文件大小,然后和20M大小做对比. 20M换算成字节 ...
- 使用Java创建指定大小的空文件夹
/** 方法一 * 创建固定大小的文件 * @param file * @param length * @throws IOException */ public static void create ...
- Linux下自动清理超过指定大小文件的方法
由于线上业务用的squid,根据经验值如果长时间运行则缓存目录下的swap.state会慢慢变大,一旦超过60M,squid的性能就会急剧下降,因此需要定时去清理大于60M的swap.state文件. ...
- Java 清除指定目录文件夹下文件
public static void clearFiles(String filePath){ File scFileDir = new File(filePath); File TrxFiles[] ...
- java判断指定路径文件夹是否存在,若不存在则创建新的文件夹
File file = new File(dirPath); if (!file.exists()) { file.mkdirs(); }
- Java、Linux、Win 快速生成指定大小的空文件
Linux dd 命令: dd if=/dev/zero of=<fileName> bs=<一次复制的大小> count=<复制的次数> 生成 50 MB 的空文 ...
- Linux下删除空文件,删除指定大小的文件
Linux下批量删除空文件(大小等于0的文件)的方法: find . -name "*" -type f -size 0c | xargs -n 1 rm -f 用这个还可以删除指 ...
- 解决java.io.IOException: Cannot run program "cygpath": CreateProcess error=2, 系统找不到指定的文件 的错误
一.外部环境: 系统环境:Windows 8 磁盘分区:只有C盘 开发环境:IntelliJ IDEA Community Edition 2016.1.3(64) 执行代码:rdd.saveAsTe ...
随机推荐
- C++复习:异常
异常处理机制专题 前言 1)异常是一种程序控制机制,与函数机制独立和互补 函数是一种以栈结构展开的上下函数衔接的程序控制系统,异常是另一种控制结构,它依附于栈结构,却可以同时设置多个异常类型作 ...
- Oracle相关文章
1.oracle 11g常用命令 2.Oracle的在windows下的安装及使用 3.Oracle scott账户被锁定,scott默认密码,sys,system默认密码 4.NaviCat Pri ...
- 跨域(五)——postMessage
HTML5的postMessage机制是客户端最直接的中档传输方法,一般用在iframe中父页与子页之间的客户端跨域通信. 浏览器支持情况:Chrome 2.0+.Internet Explorer ...
- linux shell实现 URL 编码/解码方法
(1)编码的两种方法 # echo '手机' | tr -d '\n' | xxd -plain | sed 's/\(..\)/%\1/g' # echo '手机' |tr -d '\n' |od ...
- 10. Lambda表达式.md
为了简化匿名内部类的代码,具体定义: 例如将9.内部类中的匿名内部类例子: 原来代码: //Main.java public class Main { public static void main( ...
- C# Liseview的使用方法之一:滚动到选中的行
listview.items[i].EnsureVisible();//滚动到你想要显示出来的行上. 其中,listview.items[i]就是你想要显示的行.
- struts2与spring整合时需要注意的点
首先我们需要明白spring整合struts2中的什么东西,spring中的核心就是IOC和AOP,IOC是对象的容器,AOP是处理动态代理的;比如spring与hibernate整合时就要用到aop ...
- vue如果是首页了 不让其后退
history.pushState(null, null, document.URL); //首页加载时候先置空 window.addEventListener('popstate', functio ...
- 立个FLAG
今天再次初步浏览了寒假生活: 三篇阅读笔记(人月神话,梦断代码,程序员修炼之道:从小工到专家),2月24日之前发表开发记账本软件,连续七天以上发表开发进度 学会使用GitHub,录制抖音(父母评价作品 ...
- CSS实现禁止文字选中
E10平台预览第四版中包含了对 CSS3 新属性 -ms-user-select 的支持,Web 开发人员可以利用这一新属性轻松精确的控制用户可以在网站上选择哪些文本. user-select:non ...