Skip to content

Commit 73c26ee

Browse files
author
nipa
committed
Demo UTF-8 property files
1 parent c2a83c6 commit 73c26ee

4 files changed

Lines changed: 52 additions & 0 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,4 @@ Check out [my blog](http://blog.codefx.org/tag/project-jigsaw/)
3636
## Internals
3737

3838
* [new version string schema](src/org/codefx/demo/java9/internal/version/VersionStrings.java) ([JEP 223](http://openjdk.java.net/jeps/223))
39+
* [UTF-8 property files](src/org/codefx/demo/java9/internal/resources/ResourceFileEncoding.java) ([JEP 226](http://openjdk.java.net/jeps/226))

‎compile.sh‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22

33
rm -rf out/*
44
$JAVA9_HOME/bin/javac -d out src/org/codefx/demo/java9/**/**/*.java
5+
cp resources/** out

‎resources/config.properties‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
money = € / \u20AC
2+
mood = 😎 / \uD83D\uDE0E
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package org.codefx.demo.java9.internal.resources;
2+
3+
import java.io.FileInputStream;
4+
import java.io.IOException;
5+
import java.io.InputStream;
6+
import java.util.Enumeration;
7+
import java.util.Properties;
8+
import java.util.PropertyResourceBundle;
9+
import java.util.stream.StreamSupport;
10+
11+
public class ResourceFileEncoding {
12+
13+
private static final String PATH_PREFIX = "out/";
14+
15+
public static void main(String[] args) {
16+
openFileAndPrint("config.properties", ResourceFileEncoding::printWithPropertyResourceBundle);
17+
openFileAndPrint("config.properties", ResourceFileEncoding::printWithProperties);
18+
}
19+
20+
private static void openFileAndPrint(String propertyFileName, PrintProperties print) {
21+
try (InputStream propertyFile = new FileInputStream(PATH_PREFIX + propertyFileName)) {
22+
print.print(propertyFile);
23+
} catch (IOException e) {
24+
e.printStackTrace();
25+
}
26+
}
27+
28+
private static void printWithPropertyResourceBundle(InputStream propertyFile) throws IOException {
29+
System.out.println("\nWith `PropertyResourceBundle`...");
30+
PropertyResourceBundle properties = new PropertyResourceBundle(propertyFile);
31+
properties.getKeys().asIterator().forEachRemaining(key -> {
32+
String value = properties.getString(key);
33+
System.out.println(key + " = " + value);
34+
});
35+
}
36+
37+
private static void printWithProperties(InputStream propertyFile) throws IOException {
38+
System.out.println("\nWith `Properties`...");
39+
Properties properties = new Properties();
40+
properties.load(propertyFile);
41+
properties.forEach((key, value) -> System.out.println(key + " = " + value));
42+
}
43+
44+
private interface PrintProperties {
45+
void print(InputStream propertyFile) throws IOException;
46+
}
47+
48+
}

0 commit comments

Comments
 (0)