Java - Write code to Convert a package string

Requirements

Write code to Convert a package string

Demo

//package com.book2s;
import java.io.File;

public class Main {
    public static void main(String[] argv) {
        String packageName = "com.book2s.example";
        System.out.println(convertPackageToPath(packageName));
    }//from ww  w .j  a  va  2s  .  c om

    /**
     * Convert a package string (e.g. com.book2s.plugin)
     * in dot notation to a system path string (e.g. com/book2s/plugin).
     *
     * @param packageName group id in the form of a package name
     * @return path string
     */
    public static String convertPackageToPath(final String packageName) {

        final StringBuffer buf = new StringBuffer();

        if (packageName != null) {
            final String[] tokens = packageName.split("\\.");
            for (int i = 0; i < tokens.length; i++) {
                buf.append(tokens[i]);
                if (i < tokens.length - 1) {
                    buf.append(File.separator);
                }
            }
        }

        return buf.toString();
    }
}