Tag : java

ascii-art-java

ASCII Art Java example

A funny Java example to create an ASCII art graphic. The concept is simple, get the image’s rgb color in “integer mode”, later, replace the color’s integer with ascii text. P.S This example is credit to this post and this post. ASCIIArt.java package com.mkyong;   import java.awt.Font; import java.awt.Graphics; importContinue Reading

cvs-file

How to read and parse CSV file in Java

A comma-separated values (CSV) file is just a normal plain-text file, store data in column by column, and split it by a separator (e.g comma “,”). For example : US,United States MY,Malaysia In this tutorial, we show you how to read, parse and print out the values from a csvContinue Reading

world-country

Display a list of countries in Java

In this article, we show you how to use the Locale class to play around the list of countries. P.S Tested with JDK 1.6 1. List of Countries The Locale.getISOCountries() will return a list of all 2-letter country codes defined in ISO 3166. ListCountry.java package com.webmitta.model;   import java.util.Locale;  Continue Reading

301-redirect

Java HttpURLConnection follow redirect example

The HttpURLConnection‘s follow redirect is just an indicator, in fact it won’t help you to do the “real” http redirection, you still need to handle it manually. URL obj = new URL(url); HttpURLConnection conn = (HttpURLConnection) obj.openConnection(); conn.setInstanceFollowRedirects(true); //you still need to handle redirect manully. HttpURLConnection.setFollowRedirects(true); 1. Java Http RedirectContinue Reading

java_logo_100

How to get Http Response Header in Java

This example shows you how to get the Http response header values in Java. 1. Get response header from url “mkyong.com” URL obj = new URL("http://mkyong.com"); URLConnection conn = obj.openConnection(); Map<String, List<String>> map = conn.getHeaderFields(); 2. Get key’s (“Server”) value from the headers. Map<String, List<String>> map = conn.getHeaderFields(); List<String> serverContinue Reading

default thumbnail

How to get client Ip Address in Java

Normally, you can use servletRequest.getRemoteAddr() to get the client’s IP address that’s accessing your Java web application. String ipAddress = request.getRemoteAddr(); But, if user is behind a proxy server or access your web server through a load balancer (for example, in cloud hosting), the above code will get the IPContinue Reading

time-date-different-in-Java

How to calculate date and time difference in Java

In this tutorial, we show you 2 examples to calculate date / time difference in Java : Manual time calculation. Joda time library. 1. Manual time calculation Converts Date in milliseconds (ms) and calculate the differences between two dates, with following rules : 1000 milliseconds = 1 second 60 secondsContinue Reading

google pagerank

How to get Google PageRank (PR) in Java

In this example, we will show you how to get Google PageRank (PR) in Java. To request a PageRank for “mkyong.com”, you just need to send following HTTP request : http://toolbarqueries.google.com/tbr?client=navclient-auto&hl=en&ch=6236440745 &ie=UTF-8&oe=UTF-8&features=Rank&q=info:mkyong.com P.S Above URL is used by Google toolbar plugin. The tricky part is following hashing value : ch=6236440745Continue Reading

search-file

Search directories recursively for file in Java

Here’s an example to show you how to search a file named “post.php” from directory “/Users/mkyong/websites” and all its subdirectories recursively. FileSearch.java package com.mkyong;   import java.io.File; import java.util.ArrayList; import java.util.List;   public class FileSearch {   private String fileNameToSearch; private List<String> result = new ArrayList<String>();   public String getFileNameToSearch()Continue Reading

bash-console

How to execute shell command from Java

In Java, you can use the command Runtime.getRuntime().exec to execute shell commands, or any external system command. p = Runtime.getRuntime().exec("host -t a " + domain); p.waitFor();   BufferedReader reader = new BufferedReader(new InputStreamReader( p.getInputStream())); String line = reader.readLine(); while (line != null) { line = reader.readLine(); } 1. IP AddressContinue Reading

java_logo_100

How to count duplicated items in Java List

A Java example to show you how to count the total number of duplicated entries in a List, using Collections.frequency and Map. CountDuplicatedList.java package com.mkyong;   import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import java.util.TreeMap;   public class CountDuplicatedList {   public staticContinue Reading

java_logo_100

Java : getResourceAsStream in static method

A property file in project classpath. config.properties #config file json.filepath = /Users/mkyong/Documents/workspace/SnakeCrawler/data/ 1. Non Static Method Normally, you can access above properties file via getClass().getClassLoader().getResourceAsStream. package com.mkyong.crawler.util;   import java.io.IOException; import java.io.InputStream; import java.util.Properties;   public class FileHelper {   public String getFilePathToSave() {   Properties prop = new Properties();Continue Reading

java_logo_100

Java enum example

Yet another Java enum example, nothing special, just for self-reference. UserStatus.java – User’s status in enum structure package com.mkyong;   public enum UserStatus { PENDING("P"), ACTIVE("A"), INACTIVE("I"), DELETED("D");   private String statusCode;   private UserStatus(String s) { statusCode = s; }   public String getStatusCode() { return statusCode; }  Continue Reading

java_logo_100

How to work with Java 6′s NavigableSet and NavigableMap

You can use latest Java 6′s Collection API to navigate a set and Map collections. These API gives a lot of flexibility to find out required result from the collection. 1. NavigableMap Example package com.example.collection;   import java.util.NavigableMap; import java.util.TreeMap;   public class NavigableMapDemo {   public static void main(String[]Continue Reading

java_logo_100

How to use reflection to copy properties from Pojo to other Java Beans

Sometimes we need copy properties from a Java class to other, we can do it this manually or with our own reflection implementation, but in this case use us reflection for automate it with a utility from apache Requirements commons-beanutils , you can download from here http://commons.apache.org/beanutils/ commons-loging , youContinue Reading

Page 1 of 1512345...10...Last »