code: private static FileWriter createPackageFile(String name, String packageName, String directory, String outDir)
           throws IOException
     {
        if (packageName != null && !packageName.trim().equals(""))
        {
           directory = directory + File.separatorChar +
                 packageName.replace('.', File.separatorChar);
        }
  
        File path = new File(outDir, directory);
        if (!path.exists())
        {
           if (!path.mkdirs())
              throw new IOException("outdir can't be created");
        }
  
        File file = new File(path.getAbsolutePath() + File.separatorChar + name);
  
        if (file.exists())
        {
           if (!file.delete())
              throw new IOException("there is exist file, please check");
        }
  
        return new FileWriter(file);
     }
docs: Create file in the package
  
  @param name        The name of the class
  @param packageName The package name
  @param directory   layout directory
  @param outDir      output directory
  @return The file
  @throws IOException Thrown if an error occurs