Skip to content

Commit

Permalink
Using Files#write() to implement Files#append()
Browse files Browse the repository at this point in the history
This closes #235
  • Loading branch information
mkarg authored and michael-o committed Jan 21, 2023
1 parent 6aa1b6f commit 9db3ea7
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 17 deletions.
6 changes: 4 additions & 2 deletions src/main/java/org/codehaus/plexus/util/BaseFileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.OpenOption;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;

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

static void fileWrite( Path path, String encoding, String data ) throws IOException
static void fileWrite( Path path, String encoding, String data, OpenOption... openOptions ) throws IOException
{
byte[] bytes = encoding != null ? data.getBytes( encoding ) : data.getBytes();
Files.write( path, bytes );
Files.write( path, bytes, openOptions );
}
}
18 changes: 6 additions & 12 deletions src/main/java/org/codehaus/plexus/util/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -387,18 +387,12 @@ public static void fileAppend( String fileName, String data )
public static void fileAppend( String fileName, String encoding, String data )
throws IOException
{
try ( OutputStream out = Files.newOutputStream( Paths.get(fileName),
StandardOpenOption.APPEND, StandardOpenOption.CREATE ) )
{
if ( encoding != null )
{
out.write( data.getBytes( encoding ) );
}
else
{
out.write( data.getBytes() );
}
}
fileAppend( Paths.get( fileName), encoding, data );
}

private static void fileAppend( Path path, String encoding, String data ) throws IOException
{
fileWrite( path, encoding, data, StandardOpenOption.APPEND, StandardOpenOption.CREATE );
}

/**
Expand Down
8 changes: 5 additions & 3 deletions src/main/java11/org/codehaus/plexus/util/BaseFileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.OpenOption;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;

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

static void fileWrite( Path path, String encoding, String data ) throws IOException
static void fileWrite( Path path, String encoding, String data, OpenOption... openOptions ) throws IOException
{
if ( encoding != null )
{
Files.writeString( path, data, Charset.forName( encoding ) );
Files.writeString( path, data, Charset.forName( encoding ), openOptions );
}
else
{
Files.writeString( path, data );
Files.writeString( path, data, openOptions );
}
}
}

0 comments on commit 9db3ea7

Please sign in to comment.