-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Using Files#readString()/#writeString() on Java 11+
This closes #233
- Loading branch information
Showing
5 changed files
with
89 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package org.codehaus.plexus.util; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
|
||
/** | ||
* Implementation specific to Java SE 8 version. | ||
*/ | ||
abstract class BaseFileUtils | ||
{ | ||
static String fileRead( Path path, String encoding ) throws IOException | ||
{ | ||
byte[] bytes = Files.readAllBytes( path ); | ||
return encoding != null ? new String( bytes, encoding ) : new String( bytes ); | ||
} | ||
|
||
static void fileWrite( Path path, String encoding, String data ) throws IOException | ||
{ | ||
byte[] bytes = encoding != null ? data.getBytes( encoding ) : data.getBytes(); | ||
Files.write( path, bytes ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -114,7 +114,7 @@ | |
* @author <a href="mailto:[email protected]">Jeff Turner</a> | ||
* | ||
*/ | ||
public class FileUtils | ||
public class FileUtils extends BaseFileUtils | ||
{ | ||
/** | ||
* The number of bytes in a kilobyte. | ||
|
@@ -330,7 +330,7 @@ public static String fileRead( String file ) | |
public static String fileRead( String file, String encoding ) | ||
throws IOException | ||
{ | ||
return fileRead( new File( file ), encoding ); | ||
return fileRead( Paths.get( file ), encoding ); | ||
} | ||
|
||
/** | ||
|
@@ -358,13 +358,6 @@ public static String fileRead( File file, String encoding ) | |
return fileRead( file.toPath(), encoding ); | ||
} | ||
|
||
private static String fileRead( Path path, String encoding ) | ||
throws IOException | ||
{ | ||
byte[] bytes = Files.readAllBytes( path ); | ||
return encoding != null ? new String( bytes, encoding ) : new String( bytes ); | ||
} | ||
|
||
/** | ||
* Appends data to a file. The file will be created if it does not exist. Note: the data is written with platform | ||
* encoding | ||
|
@@ -433,7 +426,7 @@ public static void fileWrite( String fileName, String data ) | |
public static void fileWrite( String fileName, String encoding, String data ) | ||
throws IOException | ||
{ | ||
File file = ( fileName == null ) ? null : new File( fileName ); | ||
Path file = ( fileName == null ) ? null : Paths.get( fileName ); | ||
fileWrite( file, encoding, data ); | ||
} | ||
|
||
|
@@ -467,13 +460,6 @@ public static void fileWrite( File file, String encoding, String data ) | |
fileWrite( file.toPath(), encoding, data ); | ||
} | ||
|
||
private static void fileWrite( Path path, String encoding, String data ) | ||
throws IOException | ||
{ | ||
byte[] bytes = encoding != null ? data.getBytes( encoding ) : data.getBytes(); | ||
Files.write( path, bytes ); | ||
} | ||
|
||
/** | ||
* Deletes a file. | ||
* | ||
|
29 changes: 29 additions & 0 deletions
29
src/main/java11/org/codehaus/plexus/util/BaseFileUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package org.codehaus.plexus.util; | ||
|
||
import java.io.IOException; | ||
import java.nio.charset.Charset; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
|
||
/** | ||
* Implementation specific to Java SE 11 version. | ||
*/ | ||
abstract class BaseFileUtils | ||
{ | ||
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 | ||
{ | ||
if ( encoding != null ) | ||
{ | ||
Files.writeString( path, data, Charset.forName( encoding ) ); | ||
} | ||
else | ||
{ | ||
Files.writeString( path, data ); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters