Skip to content

Commit e481e6c

Browse files
committed
feat: create utility init a new File #90
1 parent 8c12039 commit e481e6c

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- FileIO.newFile() create utility init a new File <https://github.com/fugerit-org/fj-lib/issues/90>
13+
14+
1015
## [8.6.7] - 2025-05-09
1116

1217
### Added

fj-core/src/main/java/org/fugerit/java/core/io/FileIO.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,18 @@
2828
@Slf4j
2929
public class FileIO {
3030

31+
public static File newFile( String baseDir, String fileName ) {
32+
return baseDir == null ? new File( fileName ) :new File( baseDir, fileName );
33+
}
34+
35+
public static File newFile( String baseDir, String fileName, boolean mustAlreadyExists ) throws IOException {
36+
File file = newFile( baseDir, fileName );
37+
if ( mustAlreadyExists && !file.exists() ) {
38+
throw new IOException( String.format( "File [%s] does not exist", file.getCanonicalPath() ) );
39+
}
40+
return file;
41+
}
42+
3143
public static boolean isInTmpFolder( File tempFile ) throws IOException {
3244
return isInTmpFolder( tempFile.getCanonicalPath() );
3345
}

fj-core/src/test/java/test/org/fugerit/java/core/io/TestFileIO.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,16 @@ public void testIsInTmpFolderKo() throws IOException {
2020
Assert.assertFalse(FileIO.isInTmpFolder( new File( "/" ) ));
2121
}
2222

23+
@Test
24+
public void testNewFile() throws IOException {
25+
String baseDir = "target/";
26+
String fileName = "not-exists.txt";
27+
String fileNameExists = "classes";
28+
File file = FileIO.newFile( baseDir, fileName );
29+
Assert.assertTrue( file.getPath().endsWith( fileName ) );
30+
Assert.assertThrows( IOException.class, () -> FileIO.newFile( baseDir, fileName, Boolean.TRUE ) );
31+
Assert.assertTrue( FileIO.newFile( baseDir, fileName, Boolean.FALSE ).getPath().endsWith( fileName ) );
32+
Assert.assertTrue( FileIO.newFile( baseDir, fileNameExists, Boolean.TRUE ).getPath().endsWith( fileNameExists ) );
33+
}
34+
2335
}

0 commit comments

Comments
 (0)