TechieDrill Your World Of Technical Tutorials

Archive for the ‘Java’ Category

Reading image files from Java

01.05.2010 · Posted in Java

Here is the code to read a image file from java. Image file can be read even if it is located inside a compressed JAR file or even inside a folder.  imageIO class from javax is the main element of this functionality. Here is the code import java.awt.image.BufferedImage; import java.io.IOException; import javax.imageio.ImageIO; public class ReadingImage { public void getImage() { try { BufferedImage image = ...

Decompress/unzip java objects

01.02.2010 · Posted in Java

The java.util.zip.ZipEntry extends the Zip file entry. java.util.zip.ZipEntry implements java.util.zip.ZipConstants interface. In this sample code we pass the zip file name as an argument/parameter in the command line argument . To unzip, create object byte stream and give the size of an array perceiving the length of the text in the file import java.io.*; import java.util.zip.*; public class UnZipObjectSample { public ...

Creating a zip file using java ZipOutputStream

01.02.2010 · Posted in Java

Following example explains how to read a file using file input stream and zip that file using zip output stream.  ZipOutputStream comes in package import java.util.zip.ZipOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class FilesZippingSampleCode { public static void main(String argum[]) { try { String sourceFile = “sampleFile.txt”; String targetZip = “zipME.zip”; ZipOutputStream zipOutputStream = new ZipOutputStream(new ...

Retrieve Media Access Control Address (MAC) using java

01.02.2010 · Posted in Java

Try this code and get the MAC address of your machine.. Specify the ip address of the machine instead of localhost to get the MAC address of the specified ip address machine import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.net.UnknownHostException; public class MACAddressExample { public static void main(String arguments[]) { try { // To obtain internet address InetAddress inetaddress = InetAddress.getLocalHost(); ...

String to Date conversion using SimpleDateFormat

01.02.2010 · Posted in Java

Below code explains how to convert a string representing a date into an date object java.util.Date object. To accomplish this we use SimpleDateFormat which can be imported as  java.text.SimpleDateFormat which by default extends java.text.DateFormat abstract class.. import java.util.Date; import java.text.ParseException; import java.text.DateFormat; import java.text.SimpleDateFormat; public class StringToDateConversion { public static void main(String argu[]) { DateFormat dateFormat = new SimpleDateFormat(”MM/dd/yy”); try { Date ...

Creating an empty collection object

01.02.2010 · Posted in Java

Java methods at times need to acquire an empty object, this can be accomplished by using java.util.Collections class. Collections class namely have 3 static instance for the creation of static empty list, map and tree. This will also be helpful in creating type-safe collection objects See the below code for better understanding import java.util.Collections; import java.util.*; public class EmptyCollections ...

Ternary Operator in java

01.02.2010 · Posted in Java

Java ternary operator serves number of purpose. Ternary operator are basically a conditional operators. Ternary operator is compact version of if-then-else statement. Performance wise Ternary operator are good conditional operators. Ternary operator functions as if condition evaluation returns true, first value is returned else second value after the colon is returned Try this code below…. public class TernaryOperatorExample ...

Tricky Static

01.02.2010 · Posted in Java

Static is bit tricky in java programming. Using static imports we need not create an instance of the class to call the objects of the class. Have a look at the below code.. try it out, quite interesting Here we used static field min() and max() methods of Math class without creating an instance for the class, ...

Java Foreach Example

01.01.2010 · Posted in Java

Java For:each example Java for each loop will execute faster through the collection iterator. It executes faster for objects within the iterator than the generic for loop Sample code Illustrated below import java.util.*; public class ForEachExample { public static void main(String args[]) { List colors = new ArrayList(); colors.add(”RED”); colors.add(”BLUE”); colors.add(”GREEN”); colors.add(”YELLO”); colors.add(”VOILET”); for (Iterator iterator = colors.iterator(); iterator.hasNext();) ...

Ordinal Enumeration example

01.01.2010 · Posted in Java

Ordinal Enumeration generally returns the position value of the constants. Enumeration ordinal numbering starts from the initial constant value of the enumeration as ‘0′ and to the n-1 constants values Here is the best illustrated example enum Days { SUNDAY,MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY } public class EnumOrdinalExample { public static void main(String[] args) { //Give you the ordinal value of the enumeration constant. //Ordinal numbering ...