Skip to main content

Posts

Good Stub from other sites : Spring+Hibernate Application with zero XML Spring framework came up with Annotation support since 2.5 version which eases the  development. Whether Annotation based approach better or XML approach is better is  depends on the project  and their personal preference. This is very good example for zero xml. - See more at: http://www.sivalabs.in/2011/02/springhibernate-application-with-zero.html#sthash.G1pYeuBl.dpuf

Copy Content of One file to another file using Java

Copy Content of One file to another file using Java. package com.pukhraj.blog; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class FileCopy { public static void main(String[] args) throws IOException {   File inFile = new File("FullPath/inputFileName.extension"); File outFile = new File("FullPath/outputFileName.extension");    FileReader in = new FileReader(inFile);    FileWriter out = new FileWriter(outFile);    int i;    while ((i = in.read()) != -1)      out.write(i);    in.close();    out.close();   } }
Read a file using Java package com.pukhraj.blog; import java.io.BufferedReader; import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; public class ReadSimpleFile { public static void main(String[] args) { String inputFileName =  "FullPath/filename.extension"; try{ FileInputStream fstream = new FileInputStream(inputFileName); DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine; //Read File Line By Line while ((strLine = br.readLine()) != null)   { } }catch(IOException e){ e.printStackTrace(); } } }
Read Properties file using java   package com.pukhraj.blog; import java.io.FileInputStream; import java.util.Properties; public class ReadPropertiesFile {         public static void main(String[] args) {                 Properties prop = new Properties();                 try {                 FileInputStream in = new FileInputStream("PropertiesFilePath");                          prop.load(in);                         String name = prop.getProperty("name");                         String address = prop.getProperty("address");                         String city = prop...
Generate One Pixel image using Servlet package com.pukhraj.blog; //Generate One Pixel image using Servlet import java.awt.image.BufferedImage; import java.io.IOException; import java.io.OutputStream; import javax.imageio.ImageIO; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class PixelImage extends HttpServlet  { private BufferedImage pixel;  public void init(ServletConfig config) throws ServletException {    super.init(config);    pixel = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);    pixel.setRGB(0, 0, (0xFF));  } public void process(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("image/png"); OutputStre...
Uncompress GZip file package com.pukhraj.blog; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.OutputStream; import java.util.zip.GZIPInputStream; public class UncompressGZip {   public static void main(String[] argv) throws Exception {     String source = "path/FileName.gzip";     GZIPInputStream in = new GZIPInputStream(new FileInputStream(source));     String target = "path/outfilename";     OutputStream out = new FileOutputStream(target);     byte[] buf = new byte[1024];     int len;     while ((len = in.read(buf)) > 0) {       out.write(buf, 0, len);     }     in.close();     out.close();   } }
Compress File in GZip Format using java package com.pukhraj.blog; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.util.zip.GZIPOutputStream; public class CompressFile {   public static void main(String[] args) throws Exception {     InputStream in = new FileInputStream("InputFilePath/fileName.txt");     OutputStream out = new FileOutputStream("OutputFilePath/fileName.txt.gz");     GZIPOutputStream gzout = new GZIPOutputStream(out);     for (int c = in.read(); c != -1; c = in.read()) {       gzout.write(c);     }     gzout.close();   } }
Calculating IPs with the help of IP Subnet Mask (CIDR Notation ) //Library Required : commons-net-3.0.1.jar package com.pukhraj.blog; import org.apache.commons.net.util.SubnetUtils; public class IPSubnetMask { public static void main(String[] args) { String subnet = "IP/SubnetMask"; SubnetUtils utils = new SubnetUtils(subnet); String lowip = utils.getInfo().getLowAddress(); System.out.println("Low IP "+lowip); String highip = utils.getInfo().getHighAddress(); System.out.println("High IP "+highip); String[] addresses = utils.getInfo().getAllAddresses(); System.out.println("Total IPs : "+addresses.length); System.out.println("isInRange : "+utils.getInfo().isInRange("IP")); } }
Read Compressed gz file. package com.pukhraj.blog; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.util.zip.GZIPInputStream; public class ReadZipFile { public static void main(String[] args) { String FILENAME ="Path";  try{  FileInputStream fin = new FileInputStream(FILENAME);  GZIPInputStream gzis = new GZIPInputStream(fin);  InputStreamReader xover = new InputStreamReader(gzis);  BufferedReader is = new BufferedReader(xover);  String strLine;  while ((strLine = is.readLine()) != null){  System.out.println(strLine);  }  }catch(FileNotFoundException e){  e.printStackTrace();  } catch (IOException e) { e.printStackTrace(); } } }