Saturday, June 14, 2014

Reading Integer values from File



There are different ways to read the Integer values from the files

Intergr.parsent():

The common way used by all are the reading character by character from the files and convert that value to integer and catching the NumberFormat exception for non-integer values.

import java.io.FileInputStream;
import java.io.IOException;

public class convertInt {

            public static void main(String[] args) throws IOException {

                        FileInputStream file = null;
                        char cBuf;
                        file = new FileInputStream("Test.txt");
                        while(file.available() > 0) {
                                    cBuf = (char) file.read();
                                    try {
                                                Integer.parseInt(""+cBuf);
                                                System.out.print(cBuf);
                                    } catch (NumberFormatException e) {

                                    }         
                        }
            }
}

Text.txt:
Employee Number: 786798

O/p:
786798

Disadvantage  with above approach:

  •  It is not a good practice to catch runtime exception. (NumberFormatException is a runtime exception)
  •  If we are catching one exception need to handle it correctly. Empty catch blocks are not good approach.
  • If we used the above approach in applications and projects, later in some time if code clean or if they introduce the code check list to clean up the code, most of the check list contain enter log for catch block and during mass change in each code they will right error log for this above NumberFormatException.And later in log file it will create confusion for all. And also it will increase log size that will decrease performance.

So don’t use this method in large applications.

Matching String:

Using pattern matching we can make Array of {‘0’,’1’,’2’ .. ‘9’} and check each file characters with this Array.
import java.util.ArrayList;
import java.util.Arrays;
public class convertInt {
            public static void main(String[] args) throws IOException {
                        FileInputStream file = null;
                        char cBuf;
                        String[] arrayNum = {"0","1","2","3","4","5","6","7","8","9"};
                        file = new FileInputStream("test.txt");
                        ArrayList aList = new ArrayList<String>(Arrays.asList(arrayNum));
                        while(file.available() > 0) {
                                    cBuf = (char) file.read();
                                    if(aList.contains(""+cBuf)) {
                                                System.out.print(cBuf);
                                    }
                        }         
            }
}

Creating a String array (arrayNum) and converting that to list using: Arrays.asList(arrayNum) method

Since List has contains method, we can easily check.
  
Disadvantage:
We are creating a separate array and making a list, so unnecessarily using memory.

Convert to TOASCII and check:

This is a good approach compare to above to approach

public class AsciiNumCheck {

            public static void main(String[] args) throws IOException {

                        FileInputStream file = null;
                        char cBuf;
                        int  i ;
                        file = new FileInputStream("Test.txt");
                        while(file.available() > 0) {
                                    cBuf = (char) file.read();
                                    i = (int) cBuf;
                                    if( i >= 48 && i <= 57) {
                                                System.out.print(cBuf);
                                    }
                        }
            }
}


The i = (int) cBuf; will convert the character to ASCII value.
The Ascii value for 0 is = 48 and for 9 is = 57

{0,1,2 … 9 } for ASCII are continuous values from {48,49,50 … 57}
So integer will come between these values.



import java.util.InputMismatchException;
import java.util.Scanner;

public class ScannerIntCheck {
            public static void main(String[] args) throws FileNotFoundException {
                        Scanner scanner = new Scanner(new File("Test.txt"));
                        scanner.useDelimiter
                        (System.getProperty("line.separator"));
                        String line = "";

                        while(scanner.hasNext())
                        {
                                    line = scanner.next();
                                    Scanner lineScanner = new Scanner(line);
                                    while(lineScanner.hasNext()) {
                                                try {
                                                System.out.println(lineScanner.nextInt()+" ");
                                                } catch (InputMismatchException exce) {
                                                            lineScanner.next();
                                                }
                                    }
                        }
            }
}

Using scanners we will be able to read values from files. But only issue with Scanner is that it need delimiter to read the values.

Here first we read each line using line.separator as delimiter. After that we separate each values in line using ‘space’, that is default separator, no need to specify delimiter as space.

Here if the value is not number  nextInt() will throw InputMismatchException as runtime exception.

This is good in with file contain delimiter separated values.
 


I/P:

443 45wer
343 5
455 8

Ouput:

443 343 5 455 8

Thursday, May 15, 2014

System.out.println



System           

final class
out      

static member of final class and of type printStream
println 
method  of class printStream


System

 System is a Final class from java.lang package

public final class System extends object

Since it is a final class it  can’t be instantiated and inherited by other classes
err,in and out are some static fields in System class.


Out [public static final PrintStream out]
Of type printStream,  already open and ready to accept output data and print data to outputstream [console]
In  [public static final InputStream in]
Of type inputStream, open and ready to supply input data ,generally keyboard
err  [public static final PrintStream err]
Of type printStream, already open and ready to accept output data, and print data to standard error output stream[error console in eclipse]


Out

  A static member of  final System class  and type of PrintStream (standard output stream)

public static final PrintStream out

This stream is already opened by JVM during start-up and ready to accept output data. It is mapped to standard O/P console host.

When JVM is initialized it will call initializeSystemClass() and will initialize the system class and the out variable.  This will use the setout() to set the output to console.

println

A method of class PrintStream . Used to print on standard output. It will print the data to console and a newline. Internally it will call print()and then write() + newLine()

User can customize the out object setout() method. By default the O/P the data to console,  we can redirect it to any file using this setout()

      System.setOut(new PrintStream(new FileOutputStream("print.txt")));
      System.out.println(" output from console ");

In the above example, all the contents of the println will be written to the file “print.txt”




Disadvantage:

Use System.out.println only for our learning and internal debugging purpose. It will degrade performance . Since just println will call a sequence of print + write+ newline. Also system.out is not synchronized , implementing synchronization is very complex and that will degrade performance.
Use  Log4J for debugging purpose in your code , it has multiple levels of debugging options. Also Loggers are synchronized.




Wednesday, May 14, 2014

Developing simple Webservices using Jersey


Jersey is an open source RESTful webservice framework in Java. It provides lots of utilities and features to simplify the RESTful service development. Here, I will explain how to develop a simple hello world program using Jersey 2.x. 

1. Download the Jersey JAX-RS 2.0 RI bundle from https://jersey.java.net/download.html

2. Create a dynamic web project in Eclipse. Keep all the JAR files downloaded in step 1 in the WebContent/WEB-INF/lib folder.

3. Create a web.xml file inside WebContent/WEB-INF directory as follows.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  
<servlet>
    <servlet-name>myAction</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>com.test.jersey.action</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet> 
<servlet-mapping>
    <servlet-name>myAction</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>


In the above web.xml, any requests coming to this application will be routed to the servlet class org.glassfish.jersey.servlet.ServletContainer which will redirect the request to appropriate Java class present inside the package com.test.jersey.action. 

If you are using Jersey 1.x version, the servlet class name and init param to be used is,


<servlet-name>myAction</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.test.jersey.action</param-value>
 </init-param>



4. Create a Java file inside the package com.test.jersey.action (or in its subpackage) which will map the request to corresponding action


package com.test.jersey.action;

import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.ws.rs.Path;

@Path("/hello")
public class HelloWorldProgram {

@GET
@Produces("text/html")
public String getHelloMessage() {
return "<html><body>Hello World</body></html>";
}
}




5. Build the application and deploy it in any server. Now if you hit the url http://localhost:8080/hello you will get the message "Hello World". (The portnumber may differ based on your server setup).


If you need to accept input parameters from client, you can do so by using @PathParam annotation as below.


@GET
@Path("/user/{id}")
@Produces("text/html")
public String getHelloMessage(@PathParam("id") String name) {
return "<html><body>Hello "+name+"</body></html>";
}


If you hit the service using the url  http://localhost:8080/hello/user/ABC, it will return a text "Hello ABC".

Jersey is one of the best choice to develop simple light weight Webservices in Java.

Tuesday, May 13, 2014

Writing to multiple log files in Java

   In Java, normally we use log4j or java.util.Logger to create log files. Log4j uses the attributes mentioned in log4j.properties file for this. By default, log4j will look in to the WebContent/WEB-INF/classes directory for the properties file. A sample log4j property file is given below.

log.dir=/mylogs

rrd.dir=${log.dir}/rrd
datestamp=yyyy-MM-dd HH:mm:ss.SSS
roll.pattern.hourly=.yyyy-MM-dd.HH
roll.pattern.daily=.yyyy-MM-dd


log4j.rootLogger=DEBUG,testlogger
log4j.appender.testlogger=org.apache.log4j.DailyRollingFileAppender
log4j.appender.testlogger.File=${log.dir}/test.log
log4j.appender.testlogger.DatePattern=${roll.pattern.daily}
log4j.appender.testlogger.layout=org.apache.log4j.PatternLayout



Here, the logs will be generated in a log file named test.log inside the directory mylogs.


Using log4j, it is possible to write logs to different files based on the package name. For that, we need to mention different log handler based on the package name. For eg: suppose you want to print all the logs generated by the java classes present under the package com.test. For this, you need to define a handler as follows.


log4j.logger.com.test=DEBUG, testpackageLogger

Now you can mention the file where the logs from com.test package needs to be printed as below.

log4j.appender.testpackageLogger.File=${log.dir}/testpackage.log


Similarly, for different package named com.temp, you can define a new handler similar to above in the same log4j.properties file


log4j.logger.com.temp=DEBUG, temppackageLogger

log4j.appender.temppackageLogger.File=${log.dir}/temppackage.log


The logs generated by the classes which are not present in these packages will get printed in the log file mentioned under rootLogger configuration mentioned above.


Please note that the logs generated by the classes present under com.test and com.temp will also get generated inside the log file mentioned in rootLogger. To avoid that, you can use log4j additivity property


log4j.additivity.com.test=false

By setting its value as false, the logs will get printed only inside the package specific log file.