Monday, April 14, 2014

Java program to create doc/docx file

Apache POI can be used to create a doc/docx file. To know more click here.

You can download latest version of jar files(i.e. poi-bin-3.10-FINAL-20140208.zip) form below link.
http://poi.apache.org/download.html

Add below mentioned Jar files in your classpath -

  • poi-3.10-FINAL-20140208.jar
  • poi-ooxml-3.10-FINAL-20140208.jar
  • poi-ooxml-schemas-3.10-FINAL-20140208.jar
  • poi-scratchpad-3.10-FINAL-20140208.jar
  • xmlbeans-2.3.0.jar
  • dom4j-1.6.1.jar

Please see the self explanatory java code below

Java program to read doc/docx file

Apache POI can be used to read doc/docx file. To know more click here.

You can download latest version of jar files(i.e. poi-bin-3.10-FINAL-20140208.zip) form below link.
http://poi.apache.org/download.html

Add below mentioned Jar files in your classpath -
  • poi-3.10-FINAL-20140208.jar
  • poi-ooxml-3.10-FINAL-20140208.jar
  • poi-ooxml-schemas-3.10-FINAL-20140208.jar
  • poi-scratchpad-3.10-FINAL-20140208.jar
  • xmlbeans-2.3.0.jar
  • dom4j-1.6.1.jar
Please see the self explanatory java code below


Monday, March 17, 2014

Shell script - function to split a string based on a delimiter

#!/bin/ksh

#function to split a string based on a delimiter
function split {

        string=$1
        delimiter=$2

echo "split string '"$string"' with delimiter '"$delimiter"'"
        for word in $(echo $string | tr "$delimiter" "\n")
        do
                echo $word
        done
}

#call the split function with arguments string and delimiter
split "transient;private;protected;public;synchronized;native" ";"

split "float|short|char|double" "|"

UNIX/LINUX - How to encrypt/decrypt a text file

crypt command can be used to encrypt/decrypt a particular text file in UNIX/LINUX. It is a simple encryption algorithm which works on a secret-key basis. It encrypts or decrypts a stream of data from standard input, and writes the result to standard output. Encryption and decryption is a symmetric process.

Encryption
This will create an encrypted file enTextFile.txt  from a text file textFile.txt. You can use either of the commands both does one and the same task.

crypt ENCRYPTION_KEY < textFile.txt > enTextFile.txt

OR

cat textFile.txt | crypt > enTextFile.txt

Decryption
This will decrypt an encrypted file enTextFile.txt and store the output in textFile.txt

crypt ENCRYPTION_KEY < enTextFile.txt (to show data on console)
crypt ENCRYPTION_KEY < enTextFile.txt > textFile.txt

OR

cat enTextFile.txt | crypt > textFile.txt

Thursday, March 13, 2014

Linux - Copy/paste block of codes from Windows editor to Vi editor

Below is the command used to Copy/paste a block of code(s) from Windows editor to Vi editor. Basically to replace complete text from a particular file.

First copy the block of code(s) from windows editor then go to Vi editor and use the following command sequentially as given below

<ESC>
d
Shift+g
i
Shift+Insert
<ESC>

Above command says delete (d) the codes form specific line number (0 in this case) upto end of the file i.e. last line (note - capital G, hence Shift+g). Then go to insert mode (i) and paste the copied lines.