Skip to content

Commit 6ca093b

Browse files
committed
Java 8: Files.newBufferedWriter instead of new FileWriter
Provides potentially better performance. Signed-off-by: Markus KARG <[email protected]>
1 parent 4bc0050 commit 6ca093b

File tree

4 files changed

+11
-14
lines changed

4 files changed

+11
-14
lines changed

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@
6060

6161
import java.io.BufferedReader;
6262
import java.io.File;
63-
import java.io.FileWriter;
6463
import java.io.IOException;
6564
import java.io.InputStream;
6665
import java.io.InputStreamReader;
@@ -2253,7 +2252,7 @@ public static void copyFile( File from, File to, String encoding, FilterWrapper[
22532252
if ( encoding == null || encoding.length() < 1 )
22542253
{
22552254
fileReader = Files.newBufferedReader( from.toPath() );
2256-
fileWriter = new FileWriter( to );
2255+
fileWriter = Files.newBufferedWriter( to.toPath() );
22572256
}
22582257
else
22592258
{

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

+1-3
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
*/
1818

1919
import java.io.File;
20-
import java.io.FileNotFoundException;
21-
import java.io.FileWriter;
2220
import java.io.IOException;
2321
import java.io.OutputStream;
2422
import java.io.OutputStreamWriter;
@@ -145,7 +143,7 @@ public static Writer newPlatformWriter( OutputStream out )
145143
public static Writer newPlatformWriter( File file )
146144
throws IOException
147145
{
148-
return new FileWriter( file );
146+
return Files.newBufferedWriter( file.toPath() );
149147
}
150148

151149
/**

src/test/java/org/codehaus/plexus/util/IOUtilTest.java

+4-5
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
import java.io.BufferedOutputStream;
2424
import java.io.File;
25-
import java.io.FileWriter;
2625
import java.io.IOException;
2726
import java.io.InputStream;
2827
import java.io.OutputStream;
@@ -178,7 +177,7 @@ public void testInputStreamToWriter()
178177
{
179178
File destination = newFile( "copy2.txt" );
180179
InputStream fin = Files.newInputStream( testFile.toPath() );
181-
FileWriter fout = new FileWriter( destination );
180+
Writer fout = Files.newBufferedWriter( destination.toPath() );
182181

183182
IOUtil.copy( fin, fout );
184183

@@ -232,7 +231,7 @@ public void testReaderToWriter()
232231
{
233232
File destination = newFile( "copy4.txt" );
234233
Reader fin = Files.newBufferedReader( testFile.toPath() );
235-
FileWriter fout = new FileWriter( destination );
234+
Writer fout = Files.newBufferedWriter( destination.toPath() );
236235
IOUtil.copy( fin, fout );
237236

238237
fout.flush();
@@ -286,7 +285,7 @@ public void testStringToWriter()
286285
Reader fin = Files.newBufferedReader( testFile.toPath() );
287286
// Create our String. Rely on testReaderToString() to make sure this is valid.
288287
String str = IOUtil.toString( fin );
289-
FileWriter fout = new FileWriter( destination );
288+
Writer fout = Files.newBufferedWriter( destination.toPath() );
290289
IOUtil.copy( str, fout );
291290
fout.flush();
292291

@@ -330,7 +329,7 @@ public void testByteArrayToWriter()
330329
throws Exception
331330
{
332331
File destination = newFile( "copy7.txt" );
333-
FileWriter fout = new FileWriter( destination );
332+
Writer fout = Files.newBufferedWriter( destination.toPath() );
334333
InputStream fin = Files.newInputStream( testFile.toPath() );
335334

336335
// Create our byte[]. Rely on testInputStreamToByteArray() to make sure this is valid.

src/test/java/org/codehaus/plexus/util/cli/CommandlineTest.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@
2121
import static org.junit.Assert.fail;
2222

2323
import java.io.File;
24-
import java.io.FileWriter;
2524
import java.io.IOException;
2625
import java.io.Writer;
26+
import java.nio.file.Files;
27+
import java.nio.file.Paths;
2728

2829
import org.codehaus.plexus.util.IOUtil;
2930
import org.codehaus.plexus.util.Os;
@@ -443,10 +444,10 @@ public void testDollarSignInArgumentPath()
443444
assertTrue( "Can't create dir:" + dir.getAbsolutePath(), dir.mkdirs() );
444445
}
445446

446-
FileWriter writer = null;
447+
Writer writer = null;
447448
try
448449
{
449-
writer = new FileWriter( new File( dir, "test$1.txt" ) );
450+
writer = Files.newBufferedWriter( dir.toPath().resolve( "test$1.txt" ) );
450451
IOUtil.copy( "Success", writer );
451452
}
452453
finally
@@ -568,7 +569,7 @@ private static void createAndCallScript( File dir, String content )
568569
bat = new File( dir, "echo" );
569570
}
570571

571-
Writer w = new FileWriter( bat );
572+
Writer w = Files.newBufferedWriter( bat.toPath() );
572573
try
573574
{
574575
IOUtil.copy( content, w );

0 commit comments

Comments
 (0)