Tuesday 11 March 2014

ANT - Build Script to run JAR file

Build.xml to run a JAR file
<project name="MyProject" default="all" basedir=".">
    <property name="jarfile" value="Sample.jar" description="JAR Folder" />
    <target name="runJar" description="execute jar">
        <java jar="${jarfile}.jar" fork="true"/>
    </target>
    <target name="all" depends="runJar">
        <echo message="Build complete." />
    </target>
 </project>
Build.xml to run a JAR file which has arguments
<project name="MyProject" default="all" basedir=".">
    <property name="jarfile" value="Sample.jar" description="JAR Folder" />
    <target name="runJar" description="execute jar">
        <java jar="${jarfile}.jar" fork="true">
            <!-- Pass Arguments Here -->
            <arg line="${arg1}"/>
            <arg line="${arg2}"/>
        </java>
    </target>
    <target name="all" depends="runJar">
        <echo message="Build complete." />
    </target>
</project>

ANT - Build.xml to detect Operating System

Build.xml
<project name="MyProject" default="main">
   <target name="main" description="execute commands depending on OS">
       <condition property="isUnix">
          <os family="unix" />
       </condition>
       <condition property="isWindows">
          <os family="windows" />
       </condition>
       <antcall target="setUnixEnv" />
       <antcall target="setWindowsEnv" />
   </target>
 
   <target name="setUnixEnv" if="isUnix">
       <echo>This is an Unix machine.</echo>  
       <!-- Put your Logic here . An example is shown below-->
       <exec executable="sh">
          <arg value="-c" />
          <arg value="chmod 777 file.js" />
       </exec>
   </target>
 
   <target name="setWindowsEnv" if="isWindows">
       <echo>This is a Windows machine.</echo>
       <!-- Put your Logic here . An example is shown below-->
       <exec executable="cmd">
          <arg value="/c"/>
          <arg value="ipconfig"/>
       </exec>
   </target>
</project>

  

Monday 10 March 2014

File Reading in Java

File Reading will be a part of any project or application.
Depending on the requirements, there may be need to either read the file line by line or character by character or word by word.
The below program has three methods which can perform each task separately.
Grab your method and start Coding!!!

Program

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

/**
 * This program is used to play around with different methods of File Reading
 * @author Nijin Vinodan
 *
 */
public class FilesReader {
                /**
                 * This method is used to read file line by line
                 * @param fileName
                 */
                public void readFileLineByLine(String fileName){
                                try{
                                                FileInputStream fstream = new FileInputStream(fileName);
                                                DataInputStream in = new DataInputStream(fstream);
                                                BufferedReader br = new BufferedReader(new InputStreamReader(in));
                                                String strLine;
                                                while ((strLine = br.readLine()) != null){
                                                                System.out.println(strLine);
                                                }
                                                in.close();
                                }
                                catch(FileNotFoundException e){}
                                catch(IOException e){}
                }
               
                /**
                 * This method is used to read file character by character
                 * @param fileName
                 */
                public void readFileLetterbyLetter(String fileName){
                                FileInputStream fileInput;
                                try {
                                                fileInput = new FileInputStream(fileName);
                                                int read;
                                                while ((read = fileInput.read()) != -1) {
                                                   char c = (char) read;
                                                   System.out.println(c);
                                                }
                                                fileInput.close();
                                }
                                catch (FileNotFoundException e) {}
                                catch (IOException e) {}
                }
               
                /**
                 * This method is used to read file word by word
                 * @param fileName
                 */
                public void readFileWordByWord(String fileName){
                                File file = new File(fileName);
                                try {
                                                Scanner scanInput = new Scanner(new FileReader(file));
                                                while(scanInput.hasNext()){
                                                                System.out.println(scanInput.next());
                                                }
                                                scanInput.close();
                                }
                                catch (FileNotFoundException e) {}
                }
}