Skip to content

Commit ba7c401

Browse files
authored
Merge pull request #91 from fugerit-org/90-feat-create-utility-init-a-new-file
feat: create utility init a new File #90
2 parents 8c12039 + a4bbc2d commit ba7c401

File tree

3 files changed

+75
-5
lines changed

3 files changed

+75
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ 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+
1014
## [8.6.7] - 2025-05-09
1115

1216
### Added

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

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

31+
/**
32+
* Creates a new File from a base dir and file name.
33+
*
34+
* If the baseDir is null, only file name is used.
35+
*
36+
* @param baseDir the parent folder
37+
* @param fileName the file name
38+
* @return the File
39+
*/
40+
public static File newFile( String baseDir, String fileName ) {
41+
return baseDir == null ? new File( fileName ) :new File( baseDir, fileName );
42+
}
43+
44+
/**
45+
* Creates a new File from a base dir and file name.
46+
*
47+
* If the baseDir is null, only file name is used.
48+
*
49+
* Optionally mustAlreadyExists flag can be set.
50+
*
51+
* @param baseDir the parent folder
52+
* @param fileName the file name
53+
* @param mustAlreadyExists true if final file existence should be checked (if the path does not exist a IOException is thrown)
54+
* @return the File
55+
* @throws IOException in case of issues
56+
*/
57+
public static File newFile( String baseDir, String fileName, boolean mustAlreadyExists ) throws IOException {
58+
File file = newFile( baseDir, fileName );
59+
if ( mustAlreadyExists && !file.exists() ) {
60+
throw new IOException( String.format( "File [%s] does not exist", file.getCanonicalPath() ) );
61+
}
62+
return file;
63+
}
64+
3165
public static boolean isInTmpFolder( File tempFile ) throws IOException {
3266
return isInTmpFolder( tempFile.getCanonicalPath() );
3367
}
@@ -38,18 +72,16 @@ public static boolean isInTmpFolder( String tempFilePath ) throws IOException {
3872
}
3973

4074
public static boolean createFullFile( File file ) throws IOException {
41-
boolean created = true;
4275
if ( file.exists() ) {
43-
created = false;
76+
return Boolean.FALSE;
4477
} else {
4578
if ( file.isDirectory() ) {
46-
created = file.mkdirs();
79+
return file.mkdirs();
4780
} else {
4881
file.getParentFile().mkdirs();
49-
created = file.createNewFile();
82+
return file.createNewFile();
5083
}
5184
}
52-
return created;
5385
}
5486

5587
/*

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,38 @@ 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.assertTrue( FileIO.newFile( null, fileName ).getPath().endsWith( fileName ) );
31+
Assert.assertThrows( IOException.class, () -> FileIO.newFile( baseDir, fileName, Boolean.TRUE ) );
32+
Assert.assertTrue( FileIO.newFile( baseDir, fileName, Boolean.FALSE ).getPath().endsWith( fileName ) );
33+
Assert.assertTrue( FileIO.newFile( baseDir, fileNameExists, Boolean.TRUE ).getPath().endsWith( fileNameExists ) );
34+
}
35+
36+
37+
@Test
38+
public void testCreateFullFile() throws IOException {
39+
String baseDir0 = "target/";
40+
String baseDir1 = "target/path/";
41+
String fileName = "not-exists-alt.txt";
42+
String fileNameExists = "classes";
43+
File file0 = FileIO.newFile( baseDir0, fileName );
44+
File file1 = FileIO.newFile( baseDir1, fileName );
45+
File file2 = FileIO.newFile( baseDir0, fileNameExists );
46+
file0.delete();
47+
file1.delete();
48+
file1.getParentFile().delete();
49+
file2.delete();
50+
Assert.assertTrue( FileIO.createFullFile( file0 ) );
51+
Assert.assertTrue( FileIO.createFullFile( file1 ) );
52+
Assert.assertFalse( FileIO.createFullFile( file0 ) );
53+
Assert.assertFalse( FileIO.createFullFile( file2 ) );
54+
Assert.assertFalse( FileIO.createFullFile( new File( baseDir1 ) ) );
55+
}
56+
2357
}

0 commit comments

Comments
 (0)