Skip to content

Commit a995b35

Browse files
committed
Java 9 - Effectively Final Variables In try-with-resources
1 parent 48e2960 commit a995b35

File tree

6 files changed

+66
-1
lines changed

6 files changed

+66
-1
lines changed

java9-language-features-examples/src/test/java/com/howtoprogram/java9ex/Java9HTTP2ClientTest.java renamed to java9-language-features-examples/src/test/java/com/howtoprogram/http2client/Java9HTTP2ClientTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.howtoprogram.java9ex;
1+
package com.howtoprogram.http2client;
22

33

44
import jdk.incubator.http.HttpClient;
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.howtoprogram.java9ex;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.io.*;
6+
import java.nio.Buffer;
7+
import java.nio.file.Files;
8+
import java.nio.file.Path;
9+
10+
public class TryWithResourceTest {
11+
12+
@Test
13+
public void tryWithResourceTestPriorJava9() throws IOException {
14+
15+
File appConfig = new File("src/test/resources/application-backup.properties");
16+
File appConfigProd = new File("src/test/resources/application-prod.properties");
17+
18+
try (BufferedReader prodBr = new BufferedReader(new FileReader(appConfigProd));
19+
BufferedWriter backupBw = new BufferedWriter(new FileWriter(appConfig))) {
20+
String line;
21+
while (null != (line = prodBr.readLine())) {
22+
String prodLine = "prod." + line;
23+
backupBw.write(prodLine);
24+
25+
}
26+
}
27+
}
28+
29+
@Test
30+
public void tryWithResourceTest() throws IOException {
31+
32+
File appConfig = new File("src/test/resources/application-backup.properties");
33+
BufferedWriter backupBw = new BufferedWriter(new FileWriter(appConfig));
34+
35+
File appConfigProd = new File("src/test/resources/application-prod.properties");
36+
try (BufferedReader prodBr = new BufferedReader(new FileReader(appConfigProd))) {
37+
String line;
38+
while (null != (line = prodBr.readLine())) {
39+
String prodLine = "prod." + line;
40+
backupBw.write(prodLine);
41+
42+
}
43+
}
44+
45+
File appConfigQa = new File("src/test/resources/application-qa.properties");
46+
try (backupBw;
47+
BufferedReader brQA = new BufferedReader(new FileReader(appConfigQa))) {
48+
49+
String line;
50+
while (null != (line = brQA.readLine())) {
51+
String qaLine = "qa." + line;
52+
backupBw.write(qaLine);
53+
backupBw.newLine();
54+
}
55+
56+
}
57+
58+
}
59+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
prod.email=prod@gmail.com
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
email=prod@gmail.com
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
email=qa@gmail.com
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
app.name=Java 9 New Features
2+
qa.email=qa@gmail.com
3+
prod.email=prod@gmail.com

0 commit comments

Comments
 (0)