Interview Questions

I/O

Java - Read last line from a file

The following code is used to read last non-blank line from a file using java.

public String getLastNonBlankLine(File file) throws Exception
        {
                RandomAccessFile fileHandler = null;
                try
                {
                        fileHandler = new RandomAccessFile(file, "r");
                        long fileLength = file.length() - 1;
                        StringBuilder sb = new StringBuilder();

                        for (long filePointer = fileLength; filePointer != -1; filePointer--)
                        {
                                fileHandler.seek(filePointer);
                                int readByte = fileHandler.readByte();

                                if (readByte == 0xA)
                                {
                                        if (filePointer == fileLength)

Java File Operations

/**
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS

Java Directory Operations

/**
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE IS DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS

Java - Socket Programming - User Datagram Protocol (UDP)

In this article let’s discuss about UDP Socket programming in Java.

Socket :

A socket is an endpoint for communication between two different machines. An application program can send/receive messages from a socket. Each socket has a port associated with it.

Socket Programming in java is used for inter process communication between two processes (programs) running in two different machines. There are two basic protocols can be used to transmitting the message.

1.) TCP/IP - Connection-Oriented Protocol

Java - Ping Host or Ip Address

In this article lets discuss about how to ping a host using java.

Consider a scenario where you need to determine whether a host is reachable from your machine or not. You can easily find it by using ping command in Windows or ipconfig in Linux systems. But what if this is part of your requirement in your application project. It can be easily done using java.net package available as a part of java api.

Consider the following program which pings a host.

PingPoller.java

package in.techdive.java;

import java.io.IOException;

Java - Socket Programming - Transmission Control Protocol (TCP)

In this article lets discuss about socket programming using java.

What is a Socket?

A socket is an endpoint for communication between two different machines. An application program can send/receive messages from a socket. Each socket has a port associated with it.

Socket Programming in java is used for inter process communication between two processes (programs) running in two different machines. There are two basic protocols can be used to transmitting the message.

1.) TCP/IP - Connection-Oriented Protocol

Java - File Read/Write Operations

The below code sample describes the File Read and Write Operation.

1. File Read
Read all the contents from the File which exists in the same machine.
Create a File in "C:/Src.txt" with some content inside. Executing the below program will fetch all the contents and print in the console.

2. File Write
Write the contents in to the File.
Executing the below program will overwrite the File in "C:/Src.txt" with the content given in the sample program.

/**
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''

Java - Read/Write Properties file

Having a Properties file in any Java application in one of the mandatory things which needs to be done in order to achieve generic functionality. The reason behind this statement is that if any constant values are placed in a Final Java class, then if the value needs to be changed at a later time, then it involves changing the source code of the class which would result in re-deployment of the application.

In order to make it simpler, if the properties file have those constant values then changing the Properties file alone would achieve the functionality. Its so simple right!