Java templates (code generator)

Ever wanted to use Java to read a code template and generate code performing variable substitution? Here's how:

1) Create a file named Class.template with the following content:
public class %1$s {
  public static void main(String[] args) {
    System.out.println("Class %1$s says \"%2$s\"");
  }
}


2) Create a code generator which will read your template above and perform the variables substitution:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class CodeGen {
  public static void main(String[] args) {
    String className = args[0];
    String output = "Hello World!";
    try {
      BufferedReader in = new BufferedReader(new FileReader("Class.template"));
      StringBuilder sb = new StringBuilder();
      String line;
      while ((line = in.readLine()) != null) {
        sb.append(line);
      }
      in.close();
      BufferedWriter out = new BufferedWriter(new FileWriter(className + ".java", false));
      out.write(String.format(sb.toString(), generatedClassName, output));
      out.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}


3) Test it:
Compile and run your code generator:
javac CodeGen.java
java CodeGen MyClass

Compile and run your generated class MyClass:
javac MyClass.java
java MyClass


Happy templating!

Add a favicon to your blog

Do the following to add a favicon to your blog (instructions given for Blogger):

Create an image file named favicon.ico (this name is mandatory). You'll have to create this image with GIMP or other application which allows you to save your file as a .ico. If you prefer you can create a 16x16 .png with your favorite application an then use png2ico to convert your image to an icon file.

To add this icon to your blog click Customize > Layout > Edit HTML and right after your <head> tag add these lines adjusting your favicon url:
<link href='http://url.com/favicon.ico' rel='shortcut icon' type='image/x-icon'/>
<link href='http://url.com/favicon.ico' rel='icon' type='image/x-icon'/>

Save your template and you should now see your favicon appear in your blog. If not try clearing your browser cached data and refresh your blog.

Have fun!

Search inside text files in Linux

To search for files with a certain name pattern use find:
find ~ -name "*.sh"
This command will list all files with sh extension in your home folder (~) and subfolders.

Now if you want to search for a particular string inside these files all you have to do is:
grep xpto `find ~ -name "*.sh"`

That's it!

StringBuilder versus StringBuffer in Java

Both StringBuilder and StringBuffer allow you to concat several String objects in only one object without the overhead of creating one object for each concatenation.

The difference between these two is that StringBuffer is synchronized, thus slower. So if you intend to use it in a single-threaded program you should choose StringBuilder.

Example:

StringBuilder sb = new StringBuilder("Hello");
sb.append(" world!");
System.out.println(sb.toString()); // Will print Hello world!

In loops you should always use either StringBuilder or StringBuffer instead of + or .concat()!

Happy programming!