Last active
August 29, 2015 14:18
-
-
Save VenkataRaju/4e003207490a512e667e to your computer and use it in GitHub Desktop.
Java Properties File Debug Helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package raju.javautil; | |
import java.io.BufferedReader; | |
import java.io.ByteArrayOutputStream; | |
import java.io.FileInputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.io.UnsupportedEncodingException; | |
import java.nio.charset.Charset; | |
import java.util.Map.Entry; | |
import java.util.Properties; | |
import java.util.Random; | |
public final class PropertiesFileDebugHelper | |
{ | |
public static void main(String[] args) throws IOException | |
{ | |
System.out.printf("1. Helpful in escaping special characters in key, values in properties file." | |
+ "%n2. Converters UTF-8 text to Properties file compatible ISO-8859-1 text" | |
+ "%n3. Prints the Key Value pairs, if properties file(s) are provided" | |
+ "%n%n"); | |
Properties props = new Properties(); | |
for (String propsFilePath : args) | |
{ | |
props.clear(); | |
InputStream is = new FileInputStream(propsFilePath); | |
try | |
{ | |
props.load(is); | |
System.out.println("File: " + propsFilePath); | |
for (Entry<Object, Object> entry : props.entrySet()) | |
System.out.printf("%s=%s%n", entry.getKey(), entry.getValue()); | |
System.out.println(); | |
} | |
finally | |
{ | |
is.close(); | |
} | |
} | |
if (args.length != 0) | |
return; | |
demo(); | |
String key, value; | |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); | |
BufferedReader br = new BufferedReader(new InputStreamReader(System.in, Charset.forName("UTF-8"))); | |
for (;;) | |
{ | |
key = readInput(br, "Key: "); | |
if (key == null) | |
break; | |
value = readInput(br, "Value: "); | |
if (value == null) | |
break; | |
props.clear(); | |
props.setProperty(key, value); | |
baos.reset(); | |
props.store(baos, null); | |
System.out.println(withoutComment(toUtf8(baos.toByteArray()))); | |
} | |
} | |
private static void demo() throws IOException | |
{ | |
System.out.printf("Usage%nKey: "); | |
String key = "X:y=2@", value = "23##@3", comment = "# In properties file you may write like this%n"; | |
printSlowly(key, 200); | |
System.out.printf("Value: "); | |
printSlowly(value, 200); | |
System.out.println(); | |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); | |
Properties props = new Properties(); | |
props.put(key, value); | |
props.store(baos, null); | |
System.out.printf(comment); | |
System.out.println(withoutComment(toUtf8(baos.toByteArray()))); | |
System.out.printf("Continue... 'Ctrl + c' to exit%n%n"); | |
} | |
private static void printSlowly(String str, int gapBetwenChars) | |
{ | |
Random random = new Random(); | |
for (int i = 0; i < str.length(); i++) | |
{ | |
System.out.print(str.charAt(i)); | |
sleep(50 + random.nextInt(201)); | |
} | |
System.out.println(); | |
} | |
private static void sleep(int millis) | |
{ | |
try | |
{ | |
Thread.sleep(millis); | |
} | |
catch (InterruptedException e) | |
{ | |
e.printStackTrace(); | |
} | |
} | |
private static String withoutComment(String str) | |
{ | |
return str.substring(str.indexOf('\n') + 1); | |
} | |
private static String readInput(BufferedReader br, String inputMsg) throws IOException | |
{ | |
System.out.printf(inputMsg); | |
return br.readLine(); | |
} | |
private static String toUtf8(byte[] bytes) throws UnsupportedEncodingException | |
{ | |
return new String(bytes, "UTF-8"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Executable jar file can be found at: https://sites.google.com/site/rajuutils/javautils
File name: properties-file-debug-helper-<version>.jar
Requires Java 1.5
I have used babun as it supports UTF-8 text (command line):