Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 9db3ea7

Browse filesBrowse files
mkargmichael-o
authored andcommitted
Using Files#write() to implement Files#append()
This closes #235
1 parent 6aa1b6f commit 9db3ea7
Copy full SHA for 9db3ea7

File tree

Expand file treeCollapse file tree

3 files changed

+15
-17
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+15
-17
lines changed

‎src/main/java/org/codehaus/plexus/util/BaseFileUtils.java

Copy file name to clipboardExpand all lines: src/main/java/org/codehaus/plexus/util/BaseFileUtils.java
+4-2Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import java.io.IOException;
44
import java.nio.file.Files;
5+
import java.nio.file.OpenOption;
56
import java.nio.file.Path;
7+
import java.nio.file.StandardOpenOption;
68

79
/**
810
* Implementation specific to Java SE 8 version.
@@ -15,9 +17,9 @@ static String fileRead( Path path, String encoding ) throws IOException
1517
return encoding != null ? new String( bytes, encoding ) : new String( bytes );
1618
}
1719

18-
static void fileWrite( Path path, String encoding, String data ) throws IOException
20+
static void fileWrite( Path path, String encoding, String data, OpenOption... openOptions ) throws IOException
1921
{
2022
byte[] bytes = encoding != null ? data.getBytes( encoding ) : data.getBytes();
21-
Files.write( path, bytes );
23+
Files.write( path, bytes, openOptions );
2224
}
2325
}

‎src/main/java/org/codehaus/plexus/util/FileUtils.java

Copy file name to clipboardExpand all lines: src/main/java/org/codehaus/plexus/util/FileUtils.java
+6-12Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -387,18 +387,12 @@ public static void fileAppend( String fileName, String data )
387387
public static void fileAppend( String fileName, String encoding, String data )
388388
throws IOException
389389
{
390-
try ( OutputStream out = Files.newOutputStream( Paths.get(fileName),
391-
StandardOpenOption.APPEND, StandardOpenOption.CREATE ) )
392-
{
393-
if ( encoding != null )
394-
{
395-
out.write( data.getBytes( encoding ) );
396-
}
397-
else
398-
{
399-
out.write( data.getBytes() );
400-
}
401-
}
390+
fileAppend( Paths.get( fileName), encoding, data );
391+
}
392+
393+
private static void fileAppend( Path path, String encoding, String data ) throws IOException
394+
{
395+
fileWrite( path, encoding, data, StandardOpenOption.APPEND, StandardOpenOption.CREATE );
402396
}
403397

404398
/**

‎src/main/java11/org/codehaus/plexus/util/BaseFileUtils.java

Copy file name to clipboardExpand all lines: src/main/java11/org/codehaus/plexus/util/BaseFileUtils.java
+5-3Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import java.io.IOException;
44
import java.nio.charset.Charset;
55
import java.nio.file.Files;
6+
import java.nio.file.OpenOption;
67
import java.nio.file.Path;
8+
import java.nio.file.StandardOpenOption;
79

810
/**
911
* Implementation specific to Java SE 11 version.
@@ -15,15 +17,15 @@ static String fileRead( Path path, String encoding ) throws IOException
1517
return encoding != null ? Files.readString( path, Charset.forName( encoding ) ) : Files.readString( path );
1618
}
1719

18-
static void fileWrite( Path path, String encoding, String data ) throws IOException
20+
static void fileWrite( Path path, String encoding, String data, OpenOption... openOptions ) throws IOException
1921
{
2022
if ( encoding != null )
2123
{
22-
Files.writeString( path, data, Charset.forName( encoding ) );
24+
Files.writeString( path, data, Charset.forName( encoding ), openOptions );
2325
}
2426
else
2527
{
26-
Files.writeString( path, data );
28+
Files.writeString( path, data, openOptions );
2729
}
2830
}
2931
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.