1+ package com .baeldung .replacewordinfile ;
2+
3+ import static org .junit .jupiter .api .Assertions .assertLinesMatch ;
4+ import static org .junit .jupiter .api .Assertions .assertTrue ;
5+
6+ import java .io .BufferedReader ;
7+ import java .io .BufferedWriter ;
8+ import java .io .File ;
9+ import java .io .IOException ;
10+ import java .nio .file .Files ;
11+ import java .nio .file .Path ;
12+ import java .nio .file .StandardCopyOption ;
13+ import java .util .List ;
14+ import java .util .regex .Pattern ;
15+
16+ import org .junit .jupiter .api .BeforeEach ;
17+ import org .junit .jupiter .api .Test ;
18+ import org .junit .jupiter .api .io .TempDir ;
19+
20+ public class ModifyFileByPatternUnitTest {
21+
22+ @ TempDir
23+ private File fileDir ;
24+
25+ private File myFile ;
26+
27+ private static final List <String > ORIGINAL_LINES = List .of (
28+ //@formatter:off
29+ "Both JAVA and KOTLIN applications can run on the JVM." ,
30+ "But python is a simpler language" ,
31+ "PYTHON application is also platform independent." ,
32+ "java and kotlin are statically typed languages." ,
33+ "On the other hand, python is a dynamically typed language." );
34+ //@formatter:on
35+
36+ private static final List <String > EXPECTED_LINES = List .of (
37+ //@formatter:off
38+ "Both Java and Kotlin applications can run on the JVM (Java Virtual Machine)." ,
39+ "Python application is also platform independent." ,
40+ "Java and Kotlin are statically typed languages." ,
41+ "On the other hand, Python is a dynamically typed language." );
42+ //@formatter:on
43+
44+ @ BeforeEach
45+ void initFile () throws IOException {
46+ myFile = new File (fileDir , "myFile.txt" );
47+ Files .write (myFile .toPath (), ORIGINAL_LINES );
48+ }
49+
50+ @ Test
51+ void whenLoadTheFileContentToMemModifyThenWriteBack_thenCorrect () throws IOException {
52+ assertTrue (Files .exists (myFile .toPath ()));
53+ List <String > lines = Files .readAllLines (myFile .toPath ());
54+ lines .remove (1 ); // remove the 2nd line
55+ List <String > newLines = lines .stream ()
56+ .map (line -> line .replaceAll ("(?i)java" , "Java" )
57+ .replaceAll ("(?i)kotlin" , "Kotlin" )
58+ .replaceAll ("(?i)python" , "Python" )
59+ .replaceAll ("JVM" , "$0 (Java Virtual Machine)" ))
60+ .toList ();
61+
62+ Files .write (myFile .toPath (), newLines );
63+ assertLinesMatch (EXPECTED_LINES , Files .readAllLines (myFile .toPath ()));
64+ }
65+
66+ @ Test
67+ void whenUsingBufferedReaderAndModifyViaTempFile_thenCorrect (@ TempDir Path tempDir ) throws IOException {
68+
69+ Pattern javaPat = Pattern .compile ("(?i)java" );
70+ Pattern kotlinPat = Pattern .compile ("(?i)kotlin" );
71+ Pattern pythonPat = Pattern .compile ("(?i)python" );
72+ Pattern jvmPat = Pattern .compile ("JVM" );
73+
74+ Path modifiedFile = tempDir .resolve ("modified.txt" );
75+ //@formatter:off
76+ try ( BufferedReader reader = Files .newBufferedReader (myFile .toPath ());
77+ BufferedWriter writer = Files .newBufferedWriter (modifiedFile )) {
78+ //@formatter:on
79+
80+ int lineNumber = 0 ;
81+ String line ;
82+ while ((line = reader .readLine ()) != null ) {
83+ lineNumber ++;
84+ if (lineNumber == 2 ) {
85+ continue ; // skip the 2nd line
86+ }
87+
88+ String replaced = line ;
89+ replaced = javaPat .matcher (replaced )
90+ .replaceAll ("Java" );
91+ replaced = kotlinPat .matcher (replaced )
92+ .replaceAll ("Kotlin" );
93+ replaced = pythonPat .matcher (replaced )
94+ .replaceAll ("Python" );
95+ replaced = jvmPat .matcher (replaced )
96+ .replaceAll ("JVM (Java Virtual Machine)" );
97+
98+ writer .write (replaced );
99+ writer .newLine ();
100+ }
101+ }
102+
103+ Files .move (modifiedFile , myFile .toPath (), StandardCopyOption .REPLACE_EXISTING );
104+ assertTrue (myFile .exists ());
105+ assertLinesMatch (EXPECTED_LINES , Files .readAllLines (myFile .toPath ()));
106+
107+ }
108+ }
0 commit comments