Wednesday, January 28, 2009

Going through a file line by line in java

BufferedReader inFile = new BufferedReader("filename");
String line = null;
try {
   while ((line=inFile.readLine())!=null) {
      //do something with the line here
   }
}
catch (IOException ioe) {
   ioe.printStackTrace();
}

Monday, January 26, 2009

Converting a java.util.Date object into a java.sql.Date object


public class ConvertDate {
   public java.sql.Date convertDate(java.util.Date date) {
      return new java.sql.Date(date. getTime() );
   }
}

Monday, January 19, 2009

Some basic SED commands

'sed' is the search replace command that can be used in Linux.

Here are some examples on how to use 'sed':


sed 's/ //g'
- remove the spaces from the file. The 'g' indicates that the spaces should be replaced globally.

sed 's/cat/dog/g' - replace all instances of the word 'cat' with the word 'dog' in the file.

sed 's/2008-...../&,/g' - search for '2008-' followed by any 5 characters and append a ',' to the end of the string. The '&' indicates what was searched for which in this case was '2008-.....'.

All of the above examples can be used to alter a file and written to a new file e.g. cat filename | sed 's/2008/2007/g' > new_filename

Tuesday, January 6, 2009