Academic Java    Java Tutorial  >  Input-Output  >  Properties File

Properties File Example

Java Properties File EXAMPLE output Java Properties File EXAMPLE output This program uses a properties file to determine how to process another file containing html tags. The properties file indicates whether to convert tags to uppercase or lowercase. The properties file also indicates whether or not to display the updated file.

As shown, the properties file "Tagger.properties" contains instructions to convert tags to uppercase and display the updated file.
// Properties File Example
import java.io.*;
import java.util.*;

class PropertiesFileExample {

   public static void main(String[] args) {

      try {
         File propFile = new File("FileStore"+File.separator+"Tagger.properties");
         FileInputStream propStream = new FileInputStream(propFile);
         Properties props = new Properties();
         props.load(propStream);
         String caseProperty = props.getProperty("case");
         String displayProperty = props.getProperty("display");

         FileInputStream fis = new FileInputStream(
               new File("FileStore"+File.separator+"wine.html")
            );
         FileOutputStream fos = new FileOutputStream(
               new File("FileStore"+File.separator+"wineUPDATED.html")
            );
         boolean inTag = false;
         int input;
         while((input = fis.read()) != -1) {
            if(input == '<') inTag = true;
            if(input == '>') inTag = false;
            char ch = (char)input;
            if(inTag) {
               try {
                  if(caseProperty.equals("upper")) ch = Character.toUpperCase(ch);
                  else ch = Character.toLowerCase(ch);
               }
               catch(Exception e) {}
            }
            fos.write(ch);
            if(displayProperty.equals("yes")) System.out.print(ch);
         }
         fis.close();
         fos.close();
      }
      catch(Exception e) {System.out.println(e.getMessage());}
   }

}