How To Download a File in Java

Here is a snippet that shows how to download a file in java. The snippet is tested and works just fine:


	static public void download(String address, String localFileName) 
											throws MalformedURLException
												 , FileNotFoundException
												 , IOException
	{
		

		URL url = new URL(address);
		
		OutputStream out = new BufferedOutputStream(
								new FileOutputStream(localFileName));
		URLConnection conn = url.openConnection();
		InputStream in = conn.getInputStream();

		byte[] buffer = new byte[1024];
		int numRead;
		int progress = 0;
		while ((numRead = in.read(buffer)) != -1) {
			out.write(buffer, 0, numRead);
			progress += 1024;
			System.out.print("\r" + (int)(progress / 1000)+ "kb");
		}            

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

How to use Post Method using Apache HttpClient 4

HttpClient is an apache java library that can be used to read pages over http. It can be used mainly for webpages and provide a well defined API that can handle Cookies, Sessions,… It offers support for both Get and Post methods, so it’s very useful for writing http java clients that can login and perform different actions on webpages.

Starting with the version 4 the classes were drastically changed. Old tutorials don’t works anymore and the class names and methods were changed to some degree which makes old code pretty useless if you want to switch to a version 4.
Continue reading

How to Read/Write Java Properties Files

Java properties files are just simple text files that are widely used in Java to store different properties which should not be hard coded in java source files. By convention, properties files should have the .properties extension. They have a simple structure, each line defining a key/value pair. It is also possible to define larger text values on multiple lines. The main benefit of the properties files is the one that they can be easily edited by hand using any text editor.
Continue reading

How To Use Ant

Ant is a build tool written in Java, intended to be used for Java build processes. Ant is used for building small and large projects as well. You can use ant not only for automation of building Java projects, but for automating any complicated and repetitive tasks. Ant is using xml files to define tasks that should be run during a process. Continue reading

How to write an Ant xml build file to Generate JAXB Code and Compile and Build it to a Jar

There is a pretty common situation to use a java classes generator and to create classes based on a defined pattern. Hibernate, Castor, Jaxb are a few examples. I prefer to have those classes packed in a separate jar file. Since they are generated files it’s not a good practice to modify them, so there is not need to have them in your project. Debugging is the case when you need the source of generated classes. In this case you can simply attach the jar containing sources in your IDE (eclipse supports it, I think other IDE support it either). Continue reading

Drawbacks of Marker Interface Design Pattern.

A marker interface is a design pattern consisting in an interface with no methods declared. All the classes implementing this interface don’t have to implement a method for the interface(since it doesn’t have an interface), but we can say that the classes implementing the interfaces are marked. For example in Java we have the Serializable interface. It is a Marker Interface, when we create a class in order to mark it as a serializable class, it will implement the serializable, so everyone using it will know that the class is serializable.
Continue reading

How to Create Jar Files from Ant

Lets take this time in consideration a real life project. Most of the time Ant is used to compile and build java projects. This means generating, compiling and building jar files. For this we are going to define severals tasks. Lets consider the we create a buid.xml file in the directory where sources are located. The sources are put in the subdirectory called src. We also need to include a jar in the classpath while compiling. We are going to create a separate property file to define the location of this file: Continue reading