Disclaimer

The views expressed on this blog are my own and do not necessarily reflect the views of Oracle.
Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Sunday, May 31, 2009

Alternate to deprecated session.putValue

Just came across in servlet 2.2 that session.putValue is deprecated. So, the alternate is to use
session.setAttribute(string, object).

Wednesday, April 29, 2009

Useful tips to improve your java code for better performance

Olaf posted on real good java performance improvement tips while using loops, collections, jdbc api, and lot more.

Please have a look at: http://blogs.oracle.com/olaf/2009/04/java_performance_the_return_of.html

Monday, April 6, 2009

Code Smell

Ever heard of the term 'Code Smell'? Well it is a way to describe code in need of improvement.
So, to improve code you may need in times the measures to find the code which needs improvement. And for that there is a requirement of some tools and methodology to analyze code.

One such measure is called Cyclomatic Complexity. This is a measure of different control flows in a method in your java code or any other programming language method or procedure. It is counted as simple as number of if -else flows.. Its indicated that if a method has cyclomatic complexity of 10 or higher then it is considered to be a candidate for looking for defects.

One improtant derivation from cyclomatic complexity is you should have equal number of test cases to the cyclomatic complexity. I found a brilliant article on the same here

Tuesday, January 8, 2008

Files reading and writing in Java

Lot of times we need to read or write files using java code.

Here is an useful example:
import java.io.*;

class FileCopyUtility
{
public static void main(String[] args)
{
try
{
//create the file objects from the source and target file names
File fileToRead = new File("source.txt");
File fileToWrite = new File("target.txt");

//create File Input stream reader and writer
FileInputStream streamIn = new FileInputStream(fileToRead);
FileOutputStream streamOut = new FileOutputStream(fileToWrite);

int c;
//this loop will run till there is something to read in the input stream
while ((c = streamIn.read()) != -1)
{
//read stream will be written to the output stream
streamOut.write(c);
}

//close the file input and output streams once the copy operation is done.
streamIn.close();
streamOut.close();
}
catch (FileNotFoundException e)
{
System.err.println("FileCopy: " + e);
}
catch (IOException e)
{
System.err.println("FileCopy: " + e);
}
}
}

Sunday, October 14, 2007

Reading from a property file in a web application without servlet context

Sometimes, there is a requirement for reading a file like a property file containing database connection setting or some other required information which you dont want to hard code in the java class. If servlet context is available then it can be done using <>.getContext() method or using getRealPath().

In case ServletContext is not available you can use the following code:

final URL url = this.getClass().getResource("DbSettings.properties");
private Properties p;
try {
p = new Properties();
InputStream stream = new FileInputStream(url.getFile());
p.load(stream);
String prop1 = p.getProperty("property1");
stream.close();
} catch (Exception e) {
System.out.println("E :: " + e.getMessage());
}


In the above code, DbSettings.properties is the file name place in the same path where the java class containing this code is located. property1 is the name of the property you want to read from the file. You can define a property file as name-value pair as follows:
property1=value1
property2=value2
... and so on

Please note that the values should be specified without single or double quotes.