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 ...
随机推荐
- Erlang Error Records
1.No match of right hand value ... Erlang变量名需要以大写开头.
- html 自定义属性的获取和应用
在html 中,我们可以给元素设置自定义属性,格式: data-属性="属性值",可以设置一个,也可以设置多个 1.获取元素属性的常规方法:直接获取元素,然后使用getAttri ...
- [PHP]对Json字符串解码返回NULL的一般解决方案
---------------------------------------------------------------------------------------------------- ...
- eclipse 关于*.properties 文件 中文显示为Unicode,无法显示中文的问题(Properties Editor)
一.以下为在线安装Properties Editor的过程1.在eclipse下 "帮助"(help)--- 2.Install New Software3.Add4.Name:P ...
- 全局异常 同时ajax或是web跳转
F8功能强大 在java代码debug的时候,F8键可直接跳到下一个类中.免去下一步 只用把之前两种方式合并即可,就是在exception包中不要ajax的异常,将其放入到web异常中,用if ...
- MVC 2nd
步骤 3 创建控制器. StudentController.java public class StudentController { private Student model; private S ...
- 关于三层(dao,serviece,servlet)
在登陆校验中, dao:返回的是resultset 对象,就是 ps.executeQuery(需要强化的是jdbc的具体的流程) 其中的数据库连接时可以自己写 可以通过工厂类 可以通过数据库的连接 ...
- SQL调用C# dll(第二中DLL,强名称密匙)
参考:微软官网 https://msdn.microsoft.com/zh-cn/library/ms345106(es-es).aspx 1.新建项目 SQLDllTestUsingNew Clas ...
- Github好桑心,慢慢来吧,等待中
等了大半天还是没办法注册,在线求助...
- MATLAB 移动复制文件
copyfile('source','destination'):%复制文件 delete('fileName');%删除文件 movefile('source','destination');%移动 ...