IO-Utils Library

TempFile, CustomMultipartFile, etc.

License

License

Categories

Categories

Ant Build Tools
GroupId

GroupId

com.antkorwin
ArtifactId

ArtifactId

ioutils
Last Version

Last Version

0.8
Release Date

Release Date

Type

Type

jar
Description

Description

IO-Utils Library
TempFile, CustomMultipartFile, etc.
Project URL

Project URL

https://github.com/antkorwin/ioutils
Source Code Management

Source Code Management

http://github.com/antkorwin/ioutils

Download ioutils

How to add to project

<!-- https://jarcasting.com/artifacts/com.antkorwin/ioutils/ -->
<dependency>
    <groupId>com.antkorwin</groupId>
    <artifactId>ioutils</artifactId>
    <version>0.8</version>
</dependency>
// https://jarcasting.com/artifacts/com.antkorwin/ioutils/
implementation 'com.antkorwin:ioutils:0.8'
// https://jarcasting.com/artifacts/com.antkorwin/ioutils/
implementation ("com.antkorwin:ioutils:0.8")
'com.antkorwin:ioutils:jar:0.8'
<dependency org="com.antkorwin" name="ioutils" rev="0.8">
  <artifact name="ioutils" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.antkorwin', module='ioutils', version='0.8')
)
libraryDependencies += "com.antkorwin" % "ioutils" % "0.8"
[com.antkorwin/ioutils "0.8"]

Dependencies

compile (5)

Group / Artifact Type Version
com.antkorwin : throwable-functions jar 1.2
commons-io : commons-io jar 2.4
com.antkorwin : better-strings jar 0.3
commons-fileupload : commons-fileupload jar 1.4
org.projectlombok : lombok jar 1.18.12

provided (2)

Group / Artifact Type Version
org.springframework : spring-web jar 5.2.5.RELEASE
org.apache.tomcat.embed : tomcat-embed-core jar 9.0.36

test (12)

Group / Artifact Type Version
org.assertj : assertj-core jar 3.9.1
org.mockito : mockito-core jar 2.15.0
org.hamcrest : hamcrest-core jar 1.3
org.hamcrest : hamcrest-library jar 1.3
org.springframework.boot : spring-boot-starter-test jar 2.2.6.RELEASE
org.springframework.boot : spring-boot-starter-web jar 2.2.6.RELEASE
org.springframework.boot : spring-boot-starter-webflux jar 2.2.6.RELEASE
org.springframework.cloud : spring-cloud-starter-openfeign jar 2.2.3.RELEASE
com.antkorwin : common-utils jar 1.0
com.jupiter-tools : stress-test jar 0.1
org.junit.jupiter : junit-jupiter-api jar 5.6.2
org.junit.jupiter : junit-jupiter-engine jar 5.6.2

Project Modules

There are no modules declared in this project.

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);

Versions

Version
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1