A typical File Utility class

February 23, 2009 by navnith

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

public class FileUtils{
private static Logger log = Logger.getLogger(FileUtils.class);

public static void copyFile(String fileSourcePath, String fileOuputPath) throws IOException {
File inputFile = new File(fileSourcePath);
File outputFile = new File(fileOuputPath);

FileReader in;
FileWriter out;
in = new FileReader(inputFile);
out = new FileWriter(outputFile);
int c;

while((c = in.read()) != -1)
out.write(c);

in.close();
out.close();
}

public static void copyDirectory(File srcPath, File dstPath, String... extensionsToCopy) throws IOException{
if(srcPath.isDirectory()){
if(srcPath.isHidden() == false || srcPath.getName().startsWith(".") == false){
if(!dstPath.exists()){
dstPath.mkdir();
}
String files[] = srcPath.list();
for(int i = 0; i < files.length; i++){
copyDirectory(new File(srcPath, files[i]), new File(dstPath, files[i]), extensionsToCopy);
}
}
}
else{
if(!srcPath.exists()){
log.error("Cannot copy dirs");
}
else{
boolean existInList = false;
for(String tmpSt : extensionsToCopy){
if(dstPath.getPath().indexOf(tmpSt, dstPath.getPath().length() - tmpSt.length() - 1) != -1){
existInList = true;
}
}
if(srcPath.canRead() && existInList){
BufferedReader in = new BufferedReader(new FileReader(srcPath));
BufferedWriter out = new BufferedWriter(new FileWriter(dstPath));
String line = in.readLine();
while(line != null){
out.write(line);
out.newLine();
line = in.readLine();
}
in.close();
out.close();
}
}
}
}

public static void writeToFile(String filename, String content) throws IOException{

PrintWriter pw = null;
File file = null;

try{
file = new File(filename);
pw = new PrintWriter(new FileWriter(file));
pw.print(content);
pw.flush();
}
finally{
// Close the PrintWriter
if(pw != null) pw.close();
}
}

public static boolean renameFile(String oldFileName, String newFileName){
boolean isRenamed = false;
File oldFile = new File(oldFileName);
if(oldFile.exists()) isRenamed = oldFile.renameTo(new File(newFileName));
return isRenamed;
}

public static List getAllFiles(File srcPath, List paths, String... extensionsNeeded){
if(paths == null){
paths = new ArrayList();
}
if(srcPath.isDirectory()){
if(srcPath.isHidden() == false || srcPath.getName().startsWith(".") == false){
String files[] = srcPath.list();
for(int i = 0; i < files.length; i++){
getAllFiles(new File(srcPath, files[i]), paths, extensionsNeeded);
}
}
}
else{
for(String tmpSt : extensionsNeeded){
if(srcPath.getPath().indexOf(tmpSt, srcPath.getPath().length() - tmpSt.length() - 1) != -1){
paths.add(srcPath.getPath());
return paths;
}
}
}
return paths;
}

}

What is the difference between JRE and JVM

January 19, 2009 by navnith

This question perplexed me for quite some time. No other site explains it as this :
It is plain and easy to understand.

Hope this helps

January 12, 2009 by navnith

“Life is not a journey to the grave with the intention of arriving
safely in one pretty and well preserved piece, but to skid broadside,
thoroughly used up, worn out, and shouting GERONIMO!”
Hunter S. Thompson

 
People are weak. They lack the will to deny themselves the opiates that they know hold them back. They are slaves to their bodies and emotions. They would rather be distracted than face the work. They worry about others. They worry about right and wrong

The significant problems we have cannot be solved at the same level of thinking with which we created them.
- Albert Einstein

 

Place your burden at the feet of the Lord of the Universe who accomplishes everything.Remain all the time steadfast in the heart,in the Transcendental Absolute.God knows the past, present and future.He will determine the future for you and accomplish the work.What is to be done will be done at the proper time. Don’t worry.Abide in the heart and surrender your acts to the divine.- Ramana Maharshi

List of AR Rahman’s Inspirations

January 12, 2009 by navnith

I have been itching to write about this for such a long time.
Rahman is truly a genious. However he is not insulated from “being inspired”
Following is my observation of his “inspirations”. I will keep adding to this list. I spoke about this to a good old friend of mine who is a huge Rahman fan, and he said they are all tunes/loops available on the net for anyone to download. However, following are my observations

Luka chhupi (Rang de basanti): The intro Guitar is a straight rip from the intro that comes in a song called “Dogs” by Pink Floyd

Background score of Rangeela and a lot of the songs in there: Are a direct inspiration from music by Deep Forest (their first album, the one with a green color album cover).

Telephone (Hindustani) : is directly inspired from a song called “Happy Nation”

Dead Characters

January 8, 2009 by navnith

I wrote something which was on top of my head since so many days. I feel so much better now. I cannot speak of it to people, because they wont understand. This is only for the set of people who can actually feel it. There is this strong feeling of pity I have for the entire manhood. Atleast to the bunch of people who subscribe to such things.
It does not reflect on nationalities or cultures. It is universal. we are f*cking the laws of Universal Love and grace. It is so painful to see kids going against their parents. Speaking ill of/to them. Beating them, overall treating them like shit. We will have to grow up and mature into thinking that our reason of existence has completely been our parents’ choice. You were born either by mistake or by total planning. Irrelevant. the reason you are living and actually enjoying this life is because your parents gave birth to you. this can be the single most reason for any person to completely dedicate their lives to serve their parents and to make sure they do not have to suffer. It is completely by our own choices that we have been born in such conditions. We have decided already who are parents will be. We have decided what our course of life will be, even before we are born. Lot of clinical psychological proof exists to prove this. During the course of life opinions may differ. Thats normal. Completely going off the hook and ill treating them is a sin. I feel so happy when I come to meet of people who still live a life in dedication to their parents. there are still a few good men who will do anything to accomplish this. They will not go against whatever has been told to them. They will blindly do whatever is told to them. They arent fools and they arent blind. They are the living examples through whom the energy of the universe flows. God is happy seeing such humans, I guess. All men and women go through shit. The elders have been through it. They deserve compassion, love, healing, attention and peace. ….
Then I take the public transport to go to work. It is such a pity that we have still, not learnt to wait. We have still not learnt to respect, to obey rules, to become better human beings, to be able to bring a difference to our standards of life. We have agreed to be crabs. We have agreed to hurt others, agreed to be fools, agreed to be treated like slaves (once for the British and now for the lack of common sense). Shit. What a pity. No one wishes to give a loving smile to the other human. No one gets up and offers his seat to a person who is elder to him. My heart bleeds when i have to see such things. Every body wants to be there “first”. Everybody is in a hurry. Everybody wants to overtake. Everybody wants to be abusive. Chivalry is dead. Why ?Everybody wants to follow the herd.When are we going to stand up and say that this is wrong and this is the way it needs to be corrected. ” I am comfortable, who cares about the other person ? ” F*ck. When are we gonna have more compassion ? When are we going to have more love and respect for others ?
We seriously dont deserve this independence. The independence of deciding, using our own heads. Guys who died fighting for it, I bet, are repenting ( if they can see the state of affairs down here ). We dont deserve. We shall not desire. I guess we (atleast Indians)are too far in civilisation where a change can be brought about. A change in the way we treat the lesser fortunate. A change in the way we live, a change in the way our thought process is. This is most painful. We will live like this forever. Things will get worse. this is not development neither is it stepping back through strides of success. We have created a stupid thoughtless parallel way of humanity, which has no love and peace but a painful road, which a lot of people coming after us will have to tread. So many will be happy(in their own sense). So many will cry. So many will be unhappy. So many will want to die. We are done.

I would love to read your valuable responses to this post.I do not know if I am wrong or right in thinking in this way. I do not do drugs neither am I a hallucinated fart who is lost in himself or is in search of something unknown.
May be this is just a foolish search for Utopia… or just a search for a land where common sense with normal people dwells.
Peace and bliss, (o’Lord let all, including me, have it)
Navnit

Tomcat startup window disappears ?

December 31, 2008 by navnith

There are times when you start tomcat and suddenly the black little window that shows the status of the server, disappears, and you are like “hey wait, where did that window disappear ?”. You do this cycle a zillion times and still the black little window just appears and disappears, more or less like my salary :)

You were so far probably using startup.bat or wttomcat_start to start tomcat.
Here is what you can do so you can make that window start.
at the command line, am sure you know what that means, tab into the bin of tomcats and type: catalina.bat run
this will ensure that the window stays and shows you what the error was which was causing the window to disappear, wait, what did you think, the window just like that disappeared or what ?
Only in case your catalina.bat file doesnt exist for some reason then, edit tomcat/bin/startup.bat in one of the last lines change the keyword “start” to “run”.  Then run startup.bat

hope this was of some help to you

Translation and transliteration made easy

October 10, 2008 by navnith

This is freaking cool.
I stumbled on this API by google and it looks very decent.
It is just one line of code it takes to translate your text to a different language.
Proper examples on translation and transliteration are on http://code.google.com/apis/ajaxlanguage/documentation/
Its ajax obviously, and performs very well.
http://googleajaxsearchapi.blogspot.com/2008/03/introducing-ajax-language-api-tools-for.html serves as a good first step to integrate this google API into your page

Hope this helps someone

How to use an elevator

October 10, 2008 by navnith

Is the lift going up or down ? Common question, huh ?
One of my friends who got pissed off with the increase in such questions whenever he was in a lift started answering this question.
Whenever anyone asked if the lift was going up or down, he answered, “neither ways mate, tis going to London”
I wonder at times, what is it about elevators and the keys that control it, that people cant understand ?
Highly educated people. Software Professionals, Project managers and all those “big” people who get paid till their throats choke, also falter on this.
Hell, money can buy you expensive perfumes and cars not brains !
Typically there are two keys that you can press. Those keys mean : If you want to go down, press the key which points downwards and if you want to go up than the level you are on, then press the key which sort of looks like it is pointing up.
Thats about it. How hard can this be.

Getting a value from scriptlet to javascript

July 16, 2008 by navnith

Getting value from a scriptlet into javascript is pretty simple. However the otherway round which means getting a value from a scriptlets into javascript may not be too simple.
You could probably have a hidden parameter, have its

value = <%=request.getParameter(”value”) %>

and how this request parameter is going to get populated is, when you have the jsp submit the request to itself, i.e., have the action = to the same jsp page. For submitting the page to another action, you will have to write a submitForm() method and have it populated with this.form.action=youraction and then submit it.
Now on to the main topic, to get a value from scriptlet into javascript you could :

String a = “abc”;

Then make a hidden element in our good old HTML

<input type=”hidden” name=”testa” id=”testa” value=<%= a%> >

In the javascript where you want to fetch the value, you can do a

document.getElementById(”testa”).value;

thats all.

Date Validity Check

July 9, 2008 by navnith

This logic can be used to check validty of date in date month year format :

boolean checkDate(int mm, int dd, int yy){
boolean validDate = false;
if((mm == 1 || mm == 3 || mm == 5 || mm == 7 || mm == 8 || mm == 10 || mm == 12) && yy > 2000 ){ //this means the month has 31 days fro sure
if(dd>0 && dd 0 && dd 2000)){
validDate = true;
}else if(mm == 2)
validDate = ((yy%4 ==0)&& (dd>0 && dd0 && dd<29))? true: false; //figuring out leap year
else
validDate = false;

return validDate;
}