Skip to content

Commit 7983d57

Browse files
committed
Using Files.write on JRE 8 to append files
1 parent 6aa1b6f commit 7983d57

File tree

2 files changed

+9
-13
lines changed

2 files changed

+9
-13
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.io.IOException;
44
import java.nio.file.Files;
55
import java.nio.file.Path;
6+
import java.nio.file.StandardOpenOption;
67

78
/**
89
* Implementation specific to Java SE 8 version.
@@ -20,4 +21,11 @@ static void fileWrite( Path path, String encoding, String data ) throws IOExcept
2021
byte[] bytes = encoding != null ? data.getBytes( encoding ) : data.getBytes();
2122
Files.write( path, bytes );
2223
}
24+
25+
static void fileAppend( Path path, String encoding, String data ) throws IOException
26+
{
27+
byte[] bytes = encoding != null ? data.getBytes( encoding ) : data.getBytes();
28+
Files.write( path, bytes, StandardOpenOption.APPEND, StandardOpenOption.CREATE );
29+
}
30+
2331
}

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

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@
7171
import java.nio.file.Files;
7272
import java.nio.file.Path;
7373
import java.nio.file.Paths;
74-
import java.nio.file.StandardOpenOption;
7574
import java.security.SecureRandom;
7675
import java.text.DecimalFormat;
7776
import java.util.ArrayList;
@@ -387,18 +386,7 @@ public static void fileAppend( String fileName, String data )
387386
public static void fileAppend( String fileName, String encoding, String data )
388387
throws IOException
389388
{
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-
}
389+
fileAppend( Paths.get( fileName), encoding, data );
402390
}
403391

404392
/**

0 commit comments

Comments
 (0)