Linux下生成指定大小文件(命令+Java程序)
生活随笔
收集整理的這篇文章主要介紹了
Linux下生成指定大小文件(命令+Java程序)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1、dd命令
of:fileName為文件名稱,例如test.txt ;
bs:設(shè)置一次復(fù)制的大小,例如1M;
count:設(shè)置復(fù)制次數(shù),例如100;
dd if=/dev/zero of=<fileName> bs=<一次復(fù)制的大小> count=<復(fù)制的次數(shù)>例如:
dd if=/dev/zero of=1.txt bs=1M count=1002、Java快速生成
兩種方式都可以生成
package com.jxnu.test;import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.RandomAccessFile; import java.nio.ByteBuffer; import java.nio.channels.FileChannel;/*** @program: CreateFile* @author: xiaojing* @create: 2020-03-01-09-46*/ public class Test {public static void main(String[] args) throws IOException {File file = new File("test.txt");long length = 1024*1024*15;//createFixLengthFile(file, length);createFile(file, length);}/*** 第一種方法* @param file* @param length* @throws IOException*/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();}}}/*** * 第二種方法* @param file* @param length* @throws IOException*/private static void createFile(File file, long length) throws IOException {RandomAccessFile r = null;try {r = new RandomAccessFile(file, "rw");r.setLength(length);} finally {if (r != null) {r.close();}}} }總結(jié)
以上是生活随笔為你收集整理的Linux下生成指定大小文件(命令+Java程序)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: linux标准I/O——标准I/O介绍
- 下一篇: linux的系统移植——交叉编译工具集