Monday, July 26, 2010

Reading MS Word Document With Java (Apache POI API)

public void readDocument(String fileName){ 
try {
            POIFSFileSystem poifs = new POIFSFileSystem(new FileInputStream(fileName));
            HWPFDocument doc = new HWPFDocument(poifs);
            WordExtractor extractor = new WordExtractor(doc);
            String[] paragraphs = extractor.getParagraphText();
            for (String paragraph : paragraphs) {
                paragraph = paragraph.replaceAll("\\cM?\r?\n", "");
                System.out.println(paragraph);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
}

Saturday, July 24, 2010

Reading System Environment With JAVA

Map sysEnv = System.getenv();
      
for (String key : sysEnv.keySet()) {
     System.out.println(key +" => "+ sysEnv.get(key));
}


System.getenv(); will return java.util.Map of system environment, which will be easy to iterate as other Map in Java.  will be the generic representation of Map

Append Property To System Properties With JAVA

 Properties sysProp = System.getProperties();
        sysProp.setProperty("country", "India");
        System.setProperties(sysProp);
        for (String key : sysProp.stringPropertyNames()) {
            System.out.println(key +" => "+ sysProp.getProperty(key));
        }


appending system property is easy: first get system property and than set new property as pair of key-value.