File editing program I have written a Java class to replace text in file.  The file contains database configurations and is as follows:

test.properties

#local
db.url=jdbc:oracle:thin:@localhost:test

#db.user=testa
#db.password=testa

db.user=testb
db.password=testb

#db.user=testc
#db.password=testc


# is a comment character. Now db schema testb is in use.

Here is my Java class to switch between schemas

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class SwitchTo
{
  public static void main(String[] args)
  {
    Map&lt;String, String&gt; schemaMap = new HashMap&lt;String, String&gt;();
    schemaMap.put("a", "testa");
    schemaMap.put("b", "testb");
    schemaMap.put("c", "testc");

    String newText = "";
    for (int i = 0; i &lt; args.length; i++)
    {
      newText = args[i];
    }

    String newdb = "";
    newdb = schemaMap.get(newText);
    if (newdb == null || (newdb != null &amp;&amp; newdb.length() &lt;= 0))
    {
      System.out.println("Schema not available, please select : ");
      for (Map.Entry&lt;String, String&gt; entry : schemaMap.entrySet())
      {
        System.out.println(entry.getKey() + " for " + entry.getValue());
      }
      return;
    }

    try
    {
      BufferedReader file = new BufferedReader(new FileReader("test.properties"));
      String total = "";
      String line = "";
      while ((line = file.readLine()) != null)
      {
        if (!line.startsWith("#") &amp;&amp; line.contains("db.user"))
        {
          line = "#" + line;
        }

        if (!line.startsWith("#") &amp;&amp; line.contains("db.password"))
        {
          line = "#" + line;
        }

        if (line.startsWith("#") &amp;&amp; line.contains(newdb))
        {
          line = line.substring(1);
        }

        total = total + line + "\n";
      }

      FileOutputStream File = new FileOutputStream("test.properties");
      File.write(total.getBytes());
      System.out.println("Switched to schema " + schemaMap.get(newText));
    }
    catch (FileNotFoundException e)
    {
      e.printStackTrace();
    }
    catch (IOException e)
    {
      e.printStackTrace();
    }
  }
}


and I have created a batch file to run this program from command line which is as follows :

switch.bat
java SwitchTo %1


Now when write at command line

c:\&gt; switch a


Then it will comment testb in the file and testa will be uncommented.

Please suggest other improvement for this code.


Is using map for storing schemas is a good choice?
How can I make it generic to accommodate newly added schemas without need to compile this class again?
Is there any better algorithm for solving this?
Actually, this can be more generic.

Instead of hard-coding your map; fill it from your properties file.
So change your prop file to : 

#db.id=a
#db.user=testa
#db.password=testa

db.id=b
db.user=testb
db.password=testb


Fill your map like this :

private Map&lt;String, String&gt; schemaMap = new HashMap&lt;String, String&gt;();

public void fillMap() throws FileNotFoundException, IOException {
    String line;
    String key = null;
    String value = null;
    try (BufferedReader file = new BufferedReader(new FileReader("test.properties"))){
        while ((line = file.readLine()) != null) {

            if (line.contains("db.id")) {
                key = line.substring(line.indexOf('='));// could be you have to do an + 1
            }
            if (line.contains("db.user")) {
                value = line.substring(line.indexOf('='));
            }
            if (key!=null &amp;&amp; value != null) {
                schemaMap.put(key, value);
                key = null;
                value = null;
            }
        }
    } catch (IOException ex) {
         // report with Logger or System.out what happend.
    }
}


Secondly : In your main you use the args.
You do not check for how many args you have, you just take the last arg.
Maybe a check for how many args you have is appropriate here, and make the program do what you want then. (like quitting with error message or taking default arg).

Third : Always close your resources(reader/writer in this case).
Here it is done with a Try-with-resources available from java 7, otherwise you will need a finally block where you close it in.