IOUtils
1. Dependencies
<dependency>
<groupId>com.antkorwin</groupId>
<artifactId>ioutils</artifactId>
<version>0.8</version>
</dependency>
2. Automatically deleted temp files in Java
There is a standard API to delete a temporary file in java:
File temp = File.createTempFile("filename", ".tmp");
temp.deleteOnExit();
But this file will be removed only after exit JVM, if your application actively working with temporary files then it can lead to the situation when a free space in the temp folder is over, or sometimes you should restart your application just to clean the temp folder.
To resolve this problem you can use the TempFile
from ioutils
:
File tmp = TempFile.createEmpty();
this file will be deleted after a GC collecting the tmp
object.
Warning
|
Please use the temp file only by this file reference (tmp variable in the example above) if you create a new reference to the temporary file using an absolute path then you can’t read this file after GC collects the original reference. |
Also, we can create non-empty files, creating a file from String:
File tmp = TempFile.createFromString("text file context");
an InputStream variant:
ByteArrayInputStream inputStream = new ByteArrayInputStream("file content".getBytes());
File file = TempFile.createFromInputStream(() -> inputStream);
And you can set an extension to files (.tmp
by default):
File file = TempFile.createFromString("data file content", "txt");
3. Read a file from resources in Java
To simplify working with files in the resource folder, you can use the ResourceFile
class. You can read about the main methods in this class below, or you can see in the unit test here: ResourceFileTest.java where describing all API of this class.
Read a file as String:
String content = new ResourceFile("filename_in_resource_folder.txt").readAsString();
read as a byte array:
byte[] content = new ResourceFile("/test.txt").readAsByteArray();
get a file from resource:
File file = new ResourceFile("folder/nestedfile").getFile();
read an InputStream from a file in resources:
try (InputStream inputStream = new ResourceFile("test.txt").getInputStream()) {
...
}
write a content of file from resources to the OutputStream:
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
new ResourceFile("test.txt").write(() -> outputStream);