Reading and writing files using Java NIO by mkarg · Pull Request #232 · codehaus-plexus/plexus-utils
Expand Up
@@ -62,14 +62,14 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.security.SecureRandom;
Expand Down
Expand Up
@@ -355,33 +355,16 @@ public static String fileRead( File file )
public static String fileRead( File file, String encoding )
throws IOException
{
StringBuilder buf = new StringBuilder();
try ( Reader reader = getInputStreamReader( file, encoding ) ) { int count; char[] b = new char[512]; while ( ( count = reader.read( b ) ) >= 0 ) // blocking read { buf.append( b, 0, count ); } }
return buf.toString(); return fileRead( file.toPath(), encoding ); }
private static InputStreamReader getInputStreamReader( File file, String encoding ) throws IOException private static String fileRead( Path path, String encoding ) throws IOException { if ( encoding != null ) { return new InputStreamReader( Files.newInputStream( file.toPath() ), encoding ); } else { return new InputStreamReader( Files.newInputStream( file.toPath() ) ); } 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 Expand Down Expand Up @@ -481,23 +464,14 @@ public static void fileWrite( File file, String data ) public static void fileWrite( File file, String encoding, String data ) throws IOException { try ( Writer writer = getOutputStreamWriter( file, encoding ) ) { writer.write( data ); } fileWrite( file.toPath(), encoding, data ); }
private static OutputStreamWriter getOutputStreamWriter( File file, String encoding ) throws IOException
private static void fileWrite( Path path, String encoding, String data ) throws IOException { OutputStream out = Files.newOutputStream( file.toPath() ); if ( encoding != null ) { return new OutputStreamWriter( out, encoding ); } else { return new OutputStreamWriter( out ); } byte[] bytes = encoding != null ? data.getBytes( encoding ) : data.getBytes(); Files.write( path, bytes ); }
/** Expand Down
try ( Reader reader = getInputStreamReader( file, encoding ) ) { int count; char[] b = new char[512]; while ( ( count = reader.read( b ) ) >= 0 ) // blocking read { buf.append( b, 0, count ); } }
return buf.toString(); return fileRead( file.toPath(), encoding ); }
private static InputStreamReader getInputStreamReader( File file, String encoding ) throws IOException private static String fileRead( Path path, String encoding ) throws IOException { if ( encoding != null ) { return new InputStreamReader( Files.newInputStream( file.toPath() ), encoding ); } else { return new InputStreamReader( Files.newInputStream( file.toPath() ) ); } 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 Expand Down Expand Up @@ -481,23 +464,14 @@ public static void fileWrite( File file, String data ) public static void fileWrite( File file, String encoding, String data ) throws IOException { try ( Writer writer = getOutputStreamWriter( file, encoding ) ) { writer.write( data ); } fileWrite( file.toPath(), encoding, data ); }
private static OutputStreamWriter getOutputStreamWriter( File file, String encoding ) throws IOException
private static void fileWrite( Path path, String encoding, String data ) throws IOException { OutputStream out = Files.newOutputStream( file.toPath() ); if ( encoding != null ) { return new OutputStreamWriter( out, encoding ); } else { return new OutputStreamWriter( out ); } byte[] bytes = encoding != null ? data.getBytes( encoding ) : data.getBytes(); Files.write( path, bytes ); }
/** Expand Down