Friday 12 December 2014

Using single line to Copy a File in Java


You can do copy a file using single line of code using FileUtils from Apche. To do this you have to get the jar file and import to your project. Now by calling a function in your Java code you can copy a file, and the function is "FileUtils.copyFile(source file, destination path)" for example look below code.
 

Code:

import java.io.File;
import java.io.IOException;
import java.util.Date;
import org.apache.commons.io.FileUtils;


public class copy {
    public static void main(String args[]){
        try {
            String source = "/home/arul/Desktop/IJITCS.pdf";
            String destination = "/home/arul/Test/";
            File f1 = new File(source);
            String fname = f1.getName();
            System.out.println("File :: "+fname);
            File f2 = new File(destination+fname);
            //Copy a file
            FileUtils.copyFile(f1, f2);
            System.out.println("File copied Sucessfully !!! ");
        } catch (Exception e) {
            System.err.println("ERR "+e.getMessage());
        }
    }
}

Output:

File :: IJITCS-V4-N10-9.pdf

File copied Sucessfully !!! 

Jar File:

Click here to get Jar and add it to your library.

Sunday 18 May 2014

Install and Execute CloudSim in Netbeans

Download CloudSim-3.0.3(.zip for windows & .tar.gz for linux) from this link.

Extract it cloudSim-3.0.3 compressed file into local system.

In the folder you can find the folder named 'jars', here you can see four jar files.


Now open Netbeans and do the following,

1. File -> New Project  (Shortcut : Ctrl + Shift + N)

2. Select "Java" from categories and "Java Application" from projects, click next.



3. Enter project name, for example 'CloudSimulator'.



4. Select project location for storage and select the option for creating main class with project name(optional;if else you have to create main class manually), click finish.

Now you can see the project 'CloudSimulator' in projects. It will contain a package with same name and main class 'CloudSimulator.java'.



Now right click the project 'CloudSimulator' and select properties,




Select libraries,
Click the button "Add JAR/Folder", go to the path where you are stored cloudsim-3.03 folder, open the folder jars and select jar file named as "cloudsim-3.0.3.jar" click 'OK'.

Now the jar file has been added in your project library, check it and click 'OK'.









Open "CloudSimulator.java" or else create new java class. Select and delete the class with main class.




Now open folder cloudsim-3.0.3, and open examples - > org - > cloudbus - > cloudsim - > examples



Open any one example(CloudExample1.java consists single Datacenter in will increase one by one upto 8 in CloudExample8.java).

I did CloudExample1.java,



Copy the code from imports and paste it into "CloudSimulator.java",


Change the class name from "CloudSimExample1" to "CloudSimulator",


Right click - > Run File (or) Press Shift + F6, and the output will be shown in console,

In code you can edit VM properties,

            // VM description
            int vmid = 0;
            int mips = 1000;
            long size = 10000; // image size (MB)
            int ram = 512; // vm memory (MB)
            long bw = 1000;
            int pesNumber = 1; // number of cpus
            String vmm = "Xen"; // VMM name

You can check with eight example by changing properties and you can get different outputs for every change. Given examples allow you to create upto 8 data centres . And you can add or change in code for different result.

Thank you.

Next time I will show you cloud network simulation using cloudsim.


For doubts and feedback, comment it...