Posts

Showing posts from February, 2012

Linux Reference Page

##Finding particular text in files Find all the *.java files which contains the string confirmStatus in directory src (relative to the current directory). command$  find ./src -name \*.java | xargs grep -il 'confirmStatus' find commands searches list of files and passes the list to xargs, which in turns split the list and passes the each file name as argument to grep command. ##Change Domain password from Linux smbpasswd -r -U ## Setting up Printer in Ubuntu Linux $ gksudo system-config-printer ## Mounting Share drive in Guest Ubuntu Oracle Virtual Box To mount a shared folder say C:\MyVideos as MySharedVideos sudo mount -t vboxsf  MySharedVideos   /media/siddshare2 where  MySharedVideos  is the shared folder name and /media/siddshare2 is the mount point location. ##  Printing all contents to a single line Where myfile.txt contains some text data which we want to print in a single line. cat myfile.txt | tr -d '\n' > oneline.txt ## Changin

JSON Manipulation in Java

JSON stands for Java Script Object Notation, in which essentially we represent data in name:value pair in a string. Example of JSON strin is as follows {    "lastName":"Smith",    "suffix":"Jr",    "city":"Foster City",    "country":"US",    "postalCode":"94404",    "firstName":"John" } Which represent the Data about a user. JSON is a standard notation to exchange data. To process Json String , like converting from Json String to Java or vice versa we have so many different libraries in Java.One of the Most prominent being Jackson, Bellow are the example or Some Use cases for Handling Json in Java. Converting JSON String to Java Object Using Jackson We can use ObjectMapper to convert a JSON String to java Object. ObjectMapper mapper = new ObjectMapper(); UserInfo userInfoObj = mapper.readValue(JSONStringSrc,  UserInfo.class); Where UserInfo Obje

String Manipulation in Java

Working with String like things in java is very common, and one of the common uses case. While we need to replace a substring inside a long String, we have couple of options in java. Java "String" itself has methods like replace and replaceAll which we can use to replace a substring from the main string. The other option is using StringBuilder  which has method like replace(start, end,str);   Which is better then the formar and we will see the difference in details next. We can also use Pattern and Matcher to replace substring from String like bellow. Pattern p = Pattern.compile(" \\$\\{USERNAME\\ }"); Matcher m = p.matcher(ORIGINAL_STRING); p = Pattern.compile(" \\$\\{USERNAME\\ }"); m = p.matcher(ORIGINAL_STRING); m.replaceAll(USERNAME_VALUE); But the for most of the cases much better and efficient way to replace substring is to use Apache StringUtils Utility Class. I have created some test methods which utilizes the above mentioned dif