Interview Questions

Java

JAXB - Generate Stubs from XSD

This article describes the method of generating the JAXB Stubs from the XML schema using ANT build.

Consider the following ANT build xml which reads the XML schema from "C:\Sample.xsd" and generates the JAXB stubs in the "C:\jaxbSrc" location.

<?xml version="1.0" encoding="UTF-8" ?>

<project basedir="." default="build" name="Jaxb-StubGenerationDemo">

        <taskdef name="xjc" classname="com.sun.tools.xjc.XJCTask">
          <classpath>
            <fileset dir="C:\lib\" includes="jaxb-xjc.jar" />
          </classpath>
        </taskdef>

    <target name="build">

Java Create Cron Expression

The following code example provides a way to generate cron expressions in java.

/**
 * 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

Java Password Validation

The following code sample is used to perform simple password validation in java.

class PasswordValidator
{
        public PasswordValidator()
        {
                super();
        }

        public static void main(String[] args)
        {
                PasswordValidator passwordValidator = new PasswordValidator();
               
                String userName = "techdive";
                String passWord = "java2011";
                System.out.println("Input : UserName "+userName+" PassWord -> "+passWord);
               
                passwordValidator.passwordValidation(userName,passWord);
                System.out.println();
                passWord = "Java2011*";

JavaScript - Add table rows dynamically

To add rows dynamically to a HTML table use the following Java Script code.

<html>
<body>

<script type="text/javascript">
function addRow()
{
      var num = document.getElementById('sequenceId');
      var table = document.getElementById('dynamicTable');
     
      var table = document.getElementById('dynamicTable');
      var val = num.value * 1;

      for(i=0;i<val;i++)
      {
             var rowCount = table.rows.length;
             var row = table.insertRow(rowCount);
             var cell3 = row.insertCell(0);

Java - equals() and hashcode() Implementation

The below code sample provides a basic implementation of equals() and hashcode() methods of Object class in Java. Consider a class named Device, with two attributes deviceId and deviceName. We assume that for two device objects to be equal, both their deviceId and deviceName should be the same. We have used the same attribute to calculate hashcode also. It takes each character's ASCII value in the deviceName and multiplies it with its position. Finally the resultant value is added to deviceId. In this way hashcode is calculated with both the attributes.

Arun's picture

FTP4j - File Transfer Protocol Tutorial

File Transfer Protocol (FTP) is used to transfer files from one host to another host. Using FTP, files can be shared across computers and can be easily accessed.

The operations performed in FTP are browsing remote FTP site, file upload, file download, file/directory creation, rename, delete etc.

FTP in Java :

Let us see an example to understand the usage of FTP in Java.

Attachments: 

Arun's picture

FTP4j - File download using Listener

In the previous section, we have seen how to upload file using FTP Listener. Let us see how to perform FTP file download using Listener implementation.

1. FTP Utility class
Refer the previous article for the implementation of this class

2. FTP Listener class
Refer the previous article for the implementation of this class

3. FTP - File Download with Listener

/**
    Copyright (C) 2011 TECHDIVE.IN

    This library is free software; you can redistribute it and/or

Arun's picture

FTP4j - File Upload with Listener

In the previous article, we have seen how to upload files from local directory to the remote FTP server. The previous example will try to upload the file to FTP server and doesn’t know the status of the FTP upload operation. For an error free application or system, we should know the status of the file upload or file download, only then we can retry the same FTP operation.

In this section, Let us see how to get the status of the File upload operation using Listener implementation. This can be easily achieved using the below steps.

Arun's picture

FTP4j - File Upload

In this section, Let us see how to upload file from local directory to the remote FTP Server.

Perform the below steps to upload files to remote FTP Server.
1. Get the FTP Client object using IP address, username and password of the remote FTP server.
2. Create the File object to upload

File fileUpload = new File ("C:\sample.txt");

3. Use the below FTP4j api to upload files.
ftpClient.upload(fileUpload); // This will upload the local file to the remote server

FTP - File Upload
/**

Arun's picture

FTP4j - List Files in Remote FTP Server

In this section, the following Java FTP client connects to the remote server and lists all the files in the remote directory.

The below api has to be used for listing files in the remote directory.

FTPFile[] fileArray = ftpClient.list();

Java FTP – List Files

/**
    Copyright (C) 2011 TECHDIVE.IN

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either