Tuesday, December 21, 2010

Java Resource Locator. Best practice to open a File

Here I would like to discuss about the Loading of a Resource in a Java Application.For example lets take the Java sample eclipse project Test. Here we have different Package as 1. org.work.corejava, 2. org.work.corejava.file.csvreader also 3.org.work.corejava.xml.parser,
In the above Example if i want to read the InputData.txt file need to be read in the ResouceLocator.java,
Normally we do as follows.

public class ResourceLocator {
 public static void main(String[] args) {
  File fileIn = new File("InputData.TXT");
  FileReader fileReader = null;
  BufferedReader reader = null;
  try {
   fileReader = new FileReader(fileIn);
   reader = new BufferedReader(fileReader);
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  }finally{
   try{
    reader.close();
   }catch(Exception e){}
  }
 }
}
But It will throw the following Exception 

java.io.FileNotFoundException: InputData.TXT (The system cannot find the file specified)
 at java.io.FileInputStream.open(Native Method)
 at java.io.FileInputStream.(Unknown Source)
 at java.io.FileReader.(Unknown Source)
 at org.work.corejava.ResourceLocator.main(ResourceLocator.java:25)
The reason behind this Exception is the Application is searching the InputData.TXT in the base class path. Basically inside the SRC Folder. But the File is in the src/org/work/corejava folder. To avoid the above exception we can simply copy the InputData.TXT to the src folder and the issue resolved. otherwise provide the Entire path as follows.
File fileIn = new File("D:\\Development\\workspace\\Test\\org\\work\\corejava\\InputData.TXT");

The best way to do this is by using the Class.getResource Method as follows.
URL url=ResourceLocator.class.getResource("InputData.TXT");
fileReader = new FileReader(url.getFile());
Here we will keep the File and the Java FIle in the same package.
The complete program is as follows.
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;

/**
 * To find the Resource by using the Java Resource Locator.
 * @author askeralim
 *
 */
public class ResourceLocator {
 public static void main(String[] args) throws IOException {
  FileReader fileReader = null;
  BufferedReader reader = null;
  try {
   URL url=ResourceLocator.class.getResource("InputData.TXT");
   fileReader = new FileReader(url.getFile());
   reader = new BufferedReader(fileReader);
   String data =reader.readLine();
   System.out.println(data);
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  }finally{
   try{
    reader.close();
   }catch(Exception e){}
  }
 }
}

Monday, December 20, 2010

The blind man with a vision

Breaking records has been a way of life for this 30-year-old banker from Mumbai -- be it in life, at work or in sports. But he is a trader with a difference.
Ashish Goyal works for J P Morgan Chase as a chief investment officer and is passionate about macroeconomics and the financial markets. And Mr Goyal is blind.
A New York Times article says looking at Ashish work one can hardly figure out that he is visually impaired-the speed and accuracy with which he manages billions of dollars of the bank's exposure to risks like foreign exchange fluctuations.
Outside his official assignment, Ashish represented the Metro London Sports Club in 2009 in the United Kingdom's domestic blind cricket league. In his very first year, he became a prominent member of the team contributing to winning the UK league. His friends find it difficult to keep pace with his social life that ranges from theatre, music, charity work to Formula F1, tennis and globetrotting to 'watching' cricket matches.
Ashish, who was born with perfect vision, suffers from a disease called retinitis pigmentosa, which robbed him of his sight after the age of 15. He did not lose his vision at one go, but gradually went blind over a period of three years. By 18, he couldn't see anything at all.
He was the first blind student to make it to Wharton Business School, Philadelphia, four years ago. If that isn't enough, Ashish cleared his MBA with honours and went on to win the Joseph P Wharton award, given to one student every year who symbolizes Wharton's way of life.
Ashish, who now lives in London, is the first blind trader at J P Morgan, and possibly in any bank anywhere in the world. His near-impossible feat has earned him the National Award for the Empowerment of Persons with Disabilities, 2010, an honour that he will receive at the hands of the President of India this week.
Ironically, while Ashish will return to India to receive his award, this country has been a trifle hostile to him during his first attempt at entering the job market. He had a tough time getting a job in India, despite securing a second rank in his batch while doing an MBA at NMIMS.
Copied From :Yahoo Finance