Skip to content

Instantly share code, notes, and snippets.

@VenkataRaju
Last active August 29, 2015 14:18
Show Gist options
  • Save VenkataRaju/4e003207490a512e667e to your computer and use it in GitHub Desktop.
Save VenkataRaju/4e003207490a512e667e to your computer and use it in GitHub Desktop.
Java Properties File Debug Helper
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");
}
}
@VenkataRaju
Copy link
Author

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):

{ MyApps }  » java -jar properties-file-debug-helper-0.4.jar                                           /cygdrive/d/MyApps
1. Helpful in escaping special characters in key, values in properties file.
2. Converters UTF-8 text to Properties file compatible ISO-8859-1 text
3. Prints the Key Value pairs, if properties file(s) are provided

Usage
Key: X:y=2@
Value: 23##@3

# In properties file you may write like this
X\:y\=2@=23\#\#@3

Continue... 'Ctrl + c' to exit

Key: This Key has chars to be escaped : \
Value: Contains UTF-8 text 禮記‧喪服小記》第一則少「箭笄終喪三年」and chars to be escaped : '
This\ Key\ has\ chars\ to\ be\ escaped\ \:\ \\=Contains UTF-8 text \u79AE\u8A18\u2027\u55AA\u670D\u5C0F\u8A18\u300B\u7B2C\u4E00\u5247\u5C11\u300C\u7BAD\u7B04\u7D42\u55AA\u4E09\u5E74\u300Dand chars to be escaped \: '

Key:

{ MyApps }  » java -jar properties-file-debug-helper-0.4.jar D:/MyApps/test.properties
1. Helpful in escaping special characters in key, values in properties file.
2. Converters UTF-8 text to Properties file compatible ISO-8859-1 text
3. Prints the Key Value pairs, if properties file(s) are provided

File: D:/MyApps/test.properties
Key1=Value1
Key2=Value2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment