Skip to content

Commit 0eeccc1

Browse files
committed
Merge remote-tracking branch 'origin/main'
2 parents 3c22921 + 7fd12dc commit 0eeccc1

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package de.varilx.utils;
2+
3+
import lombok.experimental.UtilityClass;
4+
5+
import java.io.*;
6+
7+
@UtilityClass
8+
public class ResourceUtils {
9+
10+
public void copyFileFromResources(Class<?> applicationClass, String destinationPath, String fileName) {
11+
try (InputStream inputStream = applicationClass.getClassLoader().getResourceAsStream(fileName)) {
12+
if (inputStream != null) {
13+
File destinationDir = new File(destinationPath);
14+
if (!destinationDir.exists()) {
15+
boolean created = destinationDir.mkdirs();
16+
if (!created) {
17+
throw new IOException("Failed to create directory: " + destinationPath);
18+
}
19+
}
20+
File destinationFile = new File(destinationPath, fileName);
21+
try (OutputStream outputStream = new FileOutputStream(destinationFile)) {
22+
byte[] buffer = new byte[1024];
23+
int length;
24+
while ((length = inputStream.read(buffer)) > 0) {
25+
outputStream.write(buffer, 0, length);
26+
}
27+
}
28+
} else {
29+
System.out.println("File is null!");
30+
}
31+
} catch (IOException e) {
32+
throw new RuntimeException("Error copying file from resources: " + e.getMessage(), e);
33+
}
34+
}
35+
36+
}

0 commit comments

Comments
 (0)