- Amir Boroumand | Software engineer based in Pittsburgh, PA/
- blog/
- Create a file with a specific size in Java/
Create a file with a specific size in Java
··1 min
Note
This article was written over 5 years ago. Some information may be outdated or irrelevant.
Overview #
The java.io
package offers classes that help us create and manage files. In this article, I’ll demonstrate how to create an empty file based on a filename and how to create a file with a certain size.
Code Examples #
Create an empty file #
|
|
Note that this does not create missing parent folders. If you need to do this, call file.getParentFile().mkdirs()
on line 3.
Create a file with a specific size #
We can use the RandomAccessFile
class to create a file with a specific size.
|
|
Note that files created this way are populated with random data and may be treated as “sparse files” by the JVM implementation and the underlying operating system.