How to ping in Java

To ping in Java you can use the following snippet.

Sample Java

public static void pingUrl(String url, Integer timeOut)
{
	url = url.replace("http://", "");
	try {  
		InetAddress address = InetAddress.getByName(url);  
		System.out.println("Hostname: " + address.getHostName());  
		System.out.println("IP-Address: " + address.getHostAddress());  
		System.out.println("Reachable in " + ((double)timeOut / (double)1000) + " Seconds: " + address.isReachable(timeOut));  
	}  
	catch (UnknownHostException e) {
		System.err.println("Unable to lookup " + url + "; error: " + e);  
	}  
	catch (IOException e) {  
		System.err.println("Unable to reach " + url);  
	}  
}

Please rate this snippet

How to list the avaiable fonts in Java

To list the avaiable fonts in Java you can use the following snippet.

Sample Java

GraphicsEnvironment g = GraphicsEnvironment.getLocalGraphicsEnvironment();
String fonts[] = g.getAvailableFontFamilyNames();
for (int Counter = 0; Counter < fonts.length;Counter++ ){
	System.out.println(fonts[Counter]);
}

Please rate this snippet

How to join a array to string in Java

To join a array to string in Java you can use the following snippet.

Sample Java

public static String JoinArraylist(List<String> input, String delimiter, Boolean appendToEnd){
	Iterator<String> iteratorString = input.iterator();
	StringBuilder stringBuilder = new StringBuilder();
	for (;;) {
		stringBuilder.append(iteratorString.next());
		if (!iteratorString.hasNext()){ 
			break;
		}
		stringBuilder.append(delimiter);
	}
	if(appendToEnd){
		stringBuilder.append(delimiter);
	}
	return stringBuilder.toString();
}

Please rate this snippet

How to get the mime type of a file in Java

To get the mime type of a file in Java you can use the following snippet.

Sample Java

String fileName = "/path/to/file.zip";
MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();
String mimeType = mimeTypesMap.getContentType(fileName);
File file = new File(fileName);
mimeType = mimeTypesMap.getContentType(file);
System.out.println(mimeType);

Please rate this snippet

How to check if number is a prime in Java

To check if number is a prime in Java you can use the following snippet.

Sample Java

public static boolean IsPrime(long input) {
	boolean primeCheck = true;
	for (long count = 3; count <= Math.sqrt(input); count += 2)
		if (input % count == 0) {
			primeCheck = false;
			break;
		}
	if (( input%2 !=0 && primeCheck && input > 2) || input == 2) {
		return true;
	} else {
		return false;
	}
}

Please rate this snippet

How to read a file into a bytearray in Java

To read a file into a bytearray in Java you can use the following snippet.

Sample Java

public static byte[] ReadFileIntoByteArray(File fileName) throws IOException {
    InputStream inputStream = new FileInputStream(fileName);
    byte[] bytes;
        
    try {
        long fileLength = fileName.length();
        if (fileLength > Integer.MAX_VALUE) {
            bytes = new byte[(int)fileLength];
			int readOffset = 0;
			int readCount = 0;
			while ((readCount=inputStream.read(bytes, readOffset, bytes.length-readOffset)) >= 0 && readOffset < bytes.length ) {
				readOffset += readCount;
			}
			if (bytes.length > readOffset) {
				throw new IOException("Could not completely read file " + fileName.getName());
			}
        }
		else{
			throw new IOException("File " + fileName.getName() + " too large (>2GB)!");
		}
    }
    finally {
        inputStream.close();
    }
    return bytes;
}

Please rate this snippet