Get IP Address from hostname (loopback, local & standard hosts) in java (example)

Given hostname in java, we would like to find out the corresponding IP address. We will discuss out the following scenarios: Input loopback hostname & get corresponding ip address. We will use API InetAddress.getLoopbackAddress() to get loopback address. Input local hostname & get local IP address. We will use API InetAddress.getLocalHost() to get local host… Read More »

List or display contents of compressed or zipped file in java (example)

Given a zipped or compressed file in java. We would like to display or list the contents of zipped file. We will display the name and size of uncompressed files. Program – list or display contents of zipped /compressed file in java package org.learn; import java.io.IOException; import java.util.Enumeration; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; public class DisplayZipContents {… Read More »

Compress or zip directory (sub-directories) recursively – ZipOutputStream in java (example)

Given an input directory containing files and sub-directories. We would like to compress or zip all files present in a input directory. We will traverse input directory recursively to compress all files. Java framework has provided couple of classes to zip a file(s). ZipOutputStream: ZipOutputStream class implements an output stream filter for writing files in… Read More »

Create compress /zip file in java (ZipOutputStream class /example)

Given the input files in java, create zip or compress files using ZipOutputStream class . Java framework has provided couple of classes to create a zip & compress file. ZipOutputStream: ZipOutputStream class implements an output stream filter for writing files in ZIP file format. ZipEntry: ZipEntry class represents a ZIP file entry. e.g. If we want to… Read More »

Write or redirect standard output error stream to a file in java

We write error of an application(s) to standard error stream using System.err.print*. System.err.println(“1. Write error to a file”); We would like to redirect the application errors to a file.  e.g. System.err.println(“1. Write error to a file”) should be written to a file. We would like to set output stream, so that all errors will be… Read More »

Write or redirect console output to a file & again reset to standard output in java

In java application, we write to console output using System.out.print*. We would like to redirect console output to a file. (Stream output -> File) e.g. System.out.println(“1. Console output written to file”) should written to a file. Stream written to console output will be redirected to a file. We will demonstrate to reset the console output (Opposite… Read More »

Convert String to Integer in java (with example)

Given an String in java, we would like convert String to integer. We can convert String to integer using following methods. Convert String to integer using Integer.parseInt. Convert String to integer using Integer.valueOf. Convert String to integer using Integer constructor. Program – convert String to Integer in java (with example) package org.learn; public class ConvertStringToInt { public… Read More »

Page 5 of 43« First...34567...102030...Last »