Hi,
Here, a simple java example using the Apache Commons Configuration library. The Apache Commons Configuration software library provides a generic configuration interface which enables a Java application to read configuration data from a variety of sources.
Some resources:
https://commons.apache.org/proper/commons-configuration/
https://commons.apache.org/proper/commons-configuration/userguide/quick_start.html
A simple file properties myfile.properties:
5 | MYPARAMETER5_DOUBLE=123.45 |
We need the librairies commons-configuration-1.5.jar and commons-collections-3.2.jar:
01 | package com.huo.test.javaconfig; |
03 | import java.text.MessageFormat; |
05 | import org.apache.commons.configuration.PropertiesConfiguration; |
06 | import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy; |
09 | * Class Test for test loading configuration |
11 | public class TestConfig { |
13 | public static void main(String[] args) { |
15 | System.out.println( " ############################## EXECUTION " +TestConfig. class + " - START ############################## " ); |
16 | PropertiesConfiguration docbaseConfig = new PropertiesConfiguration(MessageFormat.format( "{0}/src/com/huo/test/javaconfig/myfile.properties" , System.getProperty( "user.dir" ))); |
17 | docbaseConfig.setListDelimiter( ',' ); |
18 | docbaseConfig.setReloadingStrategy( new FileChangedReloadingStrategy()); |
20 | final String parameter1 = docbaseConfig.getString( "MYPARAMETER1" ); |
21 | System.out.println( "The value of MYPARAMETER1 is :" +parameter1); |
22 | final String parameter2 = docbaseConfig.getString( "MYPARAMETER2" , "" ); |
23 | System.out.println( "The value of MYPARAMETER2 is :" +parameter2); |
24 | final String parameter3 = docbaseConfig.getString( "MYPARAMETER3" , "" ); |
25 | System.out.println( "The value of MYPARAMETER3 is :" +parameter3); |
27 | final int parameter4 = docbaseConfig.getInt( "MYPARAMETER4_INT" ); |
28 | System.out.println( "The value of MYPARAMETER4_INT is :" +parameter4); |
29 | final double parameter5 = docbaseConfig.getDouble( "MYPARAMETER5_DOUBLE" ); |
30 | System.out.println( "The value of MYPARAMETER5_DOUBLE is :" +parameter5); |
31 | final boolean parameter6 = docbaseConfig.getBoolean( "MYPARAMETER6_BOOL" ); |
32 | System.out.println( "The value of MYPARAMETER6_BOOL is :" +parameter6); |
35 | } catch (Throwable e) { |
38 | System.out.println( " ############################## EXECUTION " +TestConfig. class + " - END ############################## " ); |
The outputs are:
1 | ############################## EXECUTION class com.huo.test.javaconfig.TestConfig - START ############################## |
2 | The value of MYPARAMETER1 is :VALUE1 |
3 | The value of MYPARAMETER2 is :VALUE2 |
4 | The value of MYPARAMETER3 is :VALUE3 |
5 | The value of MYPARAMETER4_INT is :123 |
6 | The value of MYPARAMETER5_DOUBLE is :123.45 |
7 | The value of MYPARAMETER6_BOOL is :true |
8 | ############################## EXECUTION class com.huo.test.javaconfig.TestConfig - END ############################## |
Others solutions
An other solution without Apache would be the use of Java Scanner class (java.util.Scanner) which was introduced in Java 1.5 as a simple text scanner which can parse primitive types and strings using regular expressions.
Java Scanner class can be used to break the input into tokens with any regular expression delimiter and it’s good for parsing files also.
Java Scanner class can be used to read file data into primitive and it types, it also extends String split() functionality to return tokens as String, int, long, Integer and other wrapper classes.
Example from Test from http://www.journaldev.com/872/java-scanner-class-java-util-scanner
Example 1 :
001 | package com.huo.test.java.parser.scanner.test1; |
004 | import java.io.IOException; |
005 | import java.util.Scanner; |
011 | public class JavaFileScanner { |
014 | * Java Scanner class (java.util.Scanner) was introduced in Java 1.5 as a simple text scanner which can parse primitive types and strings using regular expressions. |
016 | * Java Scanner class can be used to break the input into tokens with any regular expression delimiter and it’s good for parsing files also. |
018 | * Java Scanner class can be used to read file data into primitive and it types, it also extends String split() functionality to return tokens as String, int, long, |
019 | * Integer and other wrapper classes. |
022 | * @throws IOException |
024 | public static void main(String[] args) throws IOException { |
031 | * My website is journaldev.com |
034 | String fileName = "C:\\Workspaces\\TestDCTMHUO\\src\\com\\huo\\test\\java\\parser\\scanner\\test1\\source.txt" ; |
038 | Scanner scanner = new Scanner( new File(fileName)); |
039 | scanner.useDelimiter(System.getProperty( "line.separator" )); |
040 | while (scanner.hasNext()){ |
041 | System.out.println( "Lines: " +scanner.next()); |
058 | scanner = new Scanner( new File( "C:\\Workspaces\\TestDCTMHUO\\src\\com\\huo\\test\\java\\parser\\scanner\\test1\\data.csv" )); |
059 | scanner.useDelimiter(System.getProperty( "line.separator" )); |
060 | while (scanner.hasNext()){ |
062 | Employee emp = parseCSVLine(scanner.next()); |
063 | System.out.println(emp.toString()); |
073 | System.out.println( "Read from system input:" ); |
074 | scanner = new Scanner(System.in); |
075 | System.out.println( "Input first word: " +scanner.next()); |
082 | private static Employee parseCSVLine(String line) { |
083 | Scanner scanner = new Scanner(line); |
084 | scanner.useDelimiter( "\\s*,\\s*" ); |
085 | String name = scanner.next(); |
086 | int age = scanner.nextInt(); |
087 | String gender = scanner.next(); |
088 | JavaFileScanner jfs = new JavaFileScanner(); |
089 | return jfs. new Employee(name, age, gender); |
092 | public class Employee{ |
095 | private String gender; |
097 | public Employee(String n, int a, String gen){ |
104 | public String toString(){ |
105 | return "Name=" + this .name+ "::Age=" + this .age+ "::Gender=" + this .gender; |
data.csv
source.txt
2 | My website is journaldev.com |
Output:
1 | Lines: My Name is Pankaj |
2 | Lines: My website is journaldev.com |
3 | Lines: Phone : 1234567890 |
4 | Name=Pankaj::Age= 28 ::Gender=Male |
5 | Name=Lisa::Age= 30 ::Gender=Female |
6 | Name=Mike::Age= 25 ::Gender=Male |
Example 2 :
01 | package com.huo.test.java.parser.scanner.test2; |
04 | import java.io.IOException; |
05 | import java.util.Scanner; |
08 | * This example uses Scanner. Here, the contents of a file containing name-value pairs is read, and each line is parsed into its constituent data. |
12 | public class ReadWithScanner { |
15 | private final String fFilePath; |
16 | private final static String ENCODING = "UTF-8" ; |
18 | private static void log(Object aObject) { |
19 | System.out.println(String.valueOf(aObject)); |
22 | private String quote(String aText) { |
24 | return QUOTE + aText + QUOTE; |
28 | public static void main(String... aArgs) throws IOException { |
29 | ReadWithScanner parser = new ReadWithScanner( "C:\\Workspaces\\TestDCTMHUO\\src\\com\\huo\\test\\java\\parser\\scanner\\test2\\test.txt" ); |
30 | parser.processLineByLine(); |
38 | * full name of an existing, readable file. |
40 | public ReadWithScanner(String aFileName) { |
41 | fFilePath = aFileName; |
44 | /** Template method that calls {@link #processLine(String)}. */ |
45 | public final void processLineByLine() throws IOException { |
46 | Scanner scanner = new Scanner( new File(fFilePath), ENCODING); |
47 | while (scanner.hasNextLine()){ |
48 | processLine(scanner.nextLine()); |
53 | * Overridable method for processing lines in different ways. |
56 | * This simple default implementation expects simple name-value pairs, |
57 | * separated by an '=' sign. Examples of valid input: |
58 | * <tt>height = 167cm</tt> <tt>mass = 65kg</tt> |
59 | * <tt>disposition = "grumpy"</tt> |
60 | * <tt>this is the name = this is the value</tt> |
62 | protected void processLine(String aLine) { |
64 | Scanner scanner = new Scanner(aLine); |
65 | scanner.useDelimiter( "=" ); |
66 | if (scanner.hasNext()) { |
68 | String name = scanner.next(); |
69 | String value = scanner.next(); |
70 | log( "Name is : " + quote(name.trim()) + ", and Value is : " + quote(value.trim())); |
72 | log( "Empty or invalid line. Unable to process." ); |
test.txt
4 | this is the name = this is the value |
Output:
1 | Name is : 'height' , and Value is : '167cm' |
2 | Name is : 'mass' , and Value is : '65kg' |
3 | Name is : 'disposition' , and Value is : '"grumpy"' |
4 | Name is : 'this is the name' , and Value is : 'this is the value' |
That’s all!!
Huseyin OZVEREN
Related