Rabu, 02 Maret 2016

How To Detect A Copy, Paste, And Cut Event Using jQuery

To detect a copy, paste, and cut event in jQuery, you can use bind function.

Example


Following the jQuery snippet code using bind function to detect a copy, paste, and cut event in the textfield.

$(document).ready(function() {
    $("#textfield").bind({
        copy : function(){
        $('#message').text('copy event detected!');
        },
        paste : function(){
        $('#message').text('paste event detected!');
        },
        cut : function(){
        $('#message').text('cut event detected!');
        }
    });
}); 

Complete code:

<html>
    <head>
        <script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
        <style type="text/css">

        span{
            color:red;
            font-weight:bold;
        }

        </style>

    </head>

    <body>

        <h1>Detect Cut, Copy and Paste with jQuery</h1>

        <form action="#">
            <label>Text Box : </label>
            <input id="field1" type="text" size="60" value="Please Copy, paste or cut any text here" />
        </form>

        <span id="message"></span>

        <script type="text/javascript">

        $(document).ready(function() {
            $("#field1").bind({
                copy : function(){
                $('#message').text('copy event detected!');

                },

                paste : function(){
                $('#message').text('paste event detected!');
                },

                cut : function(){
                $('#message').text('cut event detected!');
                }

            });

        });

        </script>
    </body>
</html>

Senin, 29 Februari 2016

How To Create An Empty File In Java I/O

Following the Java I/O code example how to create an empty file. We use File.createNewFile method to create a file. This method return a boolean value. Return true if the file is successfully created, and return false if the file already exists.


Source Code


File Name : CreateEmptyFileDemo.java

import java.io.File;
import java.io.IOException;

public class CreateEmptyFileDemo {
 public static void main(String[] args) {
  try {

   File file = new File("c:\\test.txt");

   if (file.createNewFile()) {
    System.out.println("File " +file.getName()+ " is successfully created!");
   } else {
    System.out.println("File " +file.getName()+ " already exists.");
   }

  } catch (IOException e) {
   e.printStackTrace();
  }
 }
}

Output


File test.txt is successfully created!


How To Get Mac Address Using Java Network

Following the Java Network example code to display a mac address in hexadecimal format.

Source Code


File Name : GetMacAddressDemo.java

import java.net.InetAddress;
import java.net.NetworkInterface;

public class GetMacAddressDemo {

 public static void main(String[] args) throws Exception {
     InetAddress inetAddress = InetAddress.getLocalHost();
     System.out.println("IP address : " + inetAddress.getHostAddress());

     NetworkInterface networkInteface = NetworkInterface.getByInetAddress(inetAddress);
     byte[] mac = networkInteface.getHardwareAddress();
     System.out.print("MAC address : ");

     StringBuilder sb = new StringBuilder();
     for (int i = 0; i < mac.length; i++) {
       sb.append(String
           .format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
     }
     System.out.println(sb.toString());
   }
}


Output


IP address : 192.168.1.101
MAC address : 68-5D-43-93-07-3D

How To Get Total Space And Free Space Using Java I/O

Following the Java I/O code example to get total space and free space on a drive:

Source Code


File Name: TotalSpaceAndFreeSpaceDemo.java

import java.io.File;

public class TotalSpaceAndFreeSpaceDemo {
 public static void main(String[] args){
  File file = new File("D:");
     long totalSpace = file.getTotalSpace();
     System.out.println("Total space on drive " + file + " = " + totalSpace + "bytes");

     // Check the free space in C:
     long freeSpace = file.getFreeSpace();
     System.out.println("Free space on drive " + file + " = " + freeSpace + "bytes");
 }
}

Output


Total space on drive D: = 299266732032bytes
Free space on drive D: = 38464524288bytes