2009 November | DeepakGaikwad.net
Home » Archive

Articles Archive for November 2009

Java »

[25 Nov 2009 | No Comment | ]

Many applications require to check if the file being accessed is really a file or a directory. Also if it is a directory then you would need to traverse through the contents of directory. Here is the code snippet that shows how to do it. In addition to this you may have to handle exceptions. Also ensure the file is closed at the end of everything in finally block.

Java »

[24 Nov 2009 | No Comment | ]

Almost all applications are reading something from the file system. All of them have some kind of properties stored outside the application Java code, so that it can be changed without having to recompile entire code. These properties can be initial configuration properties, database connection properties, or even exception messages. You can find many ‘.properties’ files in any application classpath. Here is a code snippet that can be used to read these property files. Here are the points to remember

Close the reader after you are done with reading i.e. after …

Java »

[24 Nov 2009 | No Comment | ]

In this article we append to a text file using Java 6. Again we go to java.io package. Following points you need to consider while doing this.

Close the file in finally block. You can set the BufferWriter to null also.
FileWriter constructor has two input values. First is the file, and second whether to create new file each time. If you give ‘true’ as input, then the file will not be created instead appended.
Handle exception appropriately.

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class AppendToAFile {

public static void main(String[] args) {
AppendToAFile appendToAFile = new …

Java »

[23 Nov 2009 | No Comment | ]

Here is code you will need in Java 6 to read a file on the machine. It uses BufferReader through FileReader. Some of the things you should remember are -

Close the BufferReader in finally block so that it is closed any case.
In BufferReader.close you can opt to ignore exception.
Check the size of file you are reading as buffer.

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class FileLineByLine {

public static void main(String[] args) {
FileLineByLine fileLBL = new FileLineByLine();
fileLBL.readFileLineByLine();

}

public void readFileLineByLine(){
BufferedReader buffReader = null;
try{
buffReader = new BufferedReader (new FileReader(“C:\\SampleFile.txt”));
String line = buffReader.readLine();
while(line != null){
System.out.println(line);
line = buffReader.readLine();
}
}catch(IOException …

Jsp »

[17 Nov 2009 | No Comment | ]

JSPs are an integral part of a web application. If jsps are not coded properly, then it becomes a most unorganized piece of code. You must have seen huge jsps, which end up as a source of defects, and asking you to change them many times. Maintaining such code becomes a very expensive activity. Also it impacts stability of entire application. Following few simple practices can change working on jsp into a happy activity.
1. Reuse Common HTML
There are two main advantages of it. First is – you are maximising reuse …