Sunday, May 11, 2014

Simple java program to copy files on any OS.


  • This is a simple java program to copy files from anywhere to anywhere in the file system and on any operating system.
  • Scanner in util package to make inputs dynamic.
  • Using the program you just have to enter source of input file with file name and destination where it has to be placed.
  • The code calculates the time taken for the processing too.

 package com.objectStream;  
 import java.io.File;  
 import java.io.FileInputStream;  
 import java.io.FileOutputStream;  
 import java.io.IOException;  
 import java.io.InputStream;  
 import java.util.Scanner;  
 public class DemoObjectStreamRead {  
      static long startTime = System.currentTimeMillis();  
      static Scanner scanner = new Scanner(System.in);  
      static final String FORWARD_SLASH = "/";  
      static final String COLON = ":";  
      public static void main(String[] args) throws IOException {  
           System.out.println("Enter source: ");  
           String inputFile = scanner.next();  
           String file = "";  
           file = inputFile.split(COLON)[1];  
           System.out.println("Enter destination folder(s)");  
           String outputFile = scanner.next();  
           if (!new File(outputFile).exists()) {  
                new File(outputFile).mkdirs();  
           }  
           outputFile = outputFile.concat(FORWARD_SLASH + file);  
           File ipFile = new File(inputFile);  
           File opFile = new File(outputFile);  
           InputStream is = new FileInputStream(ipFile);  
           int read = 0;  
           byte[] bytes = new byte[1024];  
           FileOutputStream os = new FileOutputStream(opFile);  
           while ((read = is.read(bytes)) != -1) {  
                os.write(bytes, 0, read);  
           }  
           is.close();  
           os.flush();  
           os.close();  
           long endTime = System.currentTimeMillis();  
           long time = endTime - startTime;  
           System.out.println("time taken: " + (double) time / 100 / 60 + " mins");  
      }  
 }  

Feel free to comment. Suggestions and comments are appreciated.

Saturday, February 15, 2014

Top 10 basic UNIX/Linux commands

Hi everyone,  here are the basic UNIX commands that will be useful for the daily usage in the UNIX or Linux operating systems...

1. cd - Change directory

This command will be used very frequently when you're in an UNIX/Linux terminal to change from one directory to another.


Syntax: cd directory_name
            cd .. gives directory above the current directory
            cd / there is root directory above all directories, which is /, this command will navigate you to that directory.
             cd ~ Tilde key refers to the home directory of the current user, this command will navigate one to that directory.             

2. pwd - Print working directory

This command will be helpful when you want to know which directory you're currently working in.

Syntax: pwd

3. whoami & who

   i.) whoami: Prints the effective user. This command will let you know the current user.
       Syntax: whoami
   ii.) who: Prints the logged users for the current session.
       Syntax: who

4.) ls

This command will list the current directory contents. This is a very useful command.

Syntax: ls: lists directories
            ls -a: lists all directories.

5. cat - concatenate command

This command  can concatenate the files and print the output on console. It basically can combine contents of files, it can help us view the contents on console.

Syntax: consider we have two files file1 and file2, cat command can be used like this:
cat file1 file2
the combined output will be shown on screen.
One can also view the output of a single file using the command like this:
cat file1

6. clear - clear command

This command helps the user to clear the screen.

Syntax:  clear

7. less - less command

Some files may have contents that exceed the screen view, one can use less command and view the contents of a file without missing the content of the file from the screen, clicking enter one can view the contents of the file moving line by line or one can also use up and down arrows on keyboard to view the whole contents of the file.

Syntax: less fileName

8. head - head command

This command helps one to view the first 10 lines of a file by default. One can modify the command to view more or less than 10 lines.

Syntax: head fileName - gives the first 10 lines of the file.
           head -20 fileName - gives the first 20 lines of the file.

9. tail - tail command

This command helps one to view the last 10 lines of a file by default. One can modify the command to view more or less than 10 lines.

Syntax: tail fileName - gives the last 10 lines of the file.
            tail -20 fileName - gives the last 20 lines of the file.

10. ps - Snapshot of the current processes

This command is very useful, this shows the processes currently running.

Syntax: ps -e : This command shows the process ID (PID) and the process associated with the PID. This command is identical to another command ps -A (shows all processes).
            ps -ef : This command shows the user ID (UID), Process ID (PID), start time, and process.

Bonus: chmod - command that changes file permissions.

This command helps one to change file permissions for different users. Users are differentiated as Users, Groups and all others.

Users:
u - users
g - groups
a- all

Permissions:
r - read
w - write
x - execute

one can change the file permissions as follows:

chmod uga+rwx fileName - this command gives read, write and execute permissions to all the users (u,g,a).
chmod u+rw fileName - this command gives read and write permissions to user.

Octal notations for chmod:
r - 4
w - 2
x - 1

One can use the combination of the above permissions adding digits, for example user wants read and write permission, then 4+2 (r+w) gives 6, user wants full permission, then 4+2+1 (r+w+x) gives 7, for example you want to give user full permissions, groups read and write permissions and all others only read permissions, then the command is used like this:

chmod 764 fileName - here first digit is 7 combination of 4+2+1 (r+w+x) for user, 6 combination of 4+2 (r+w) for groups, 4 read permission for all.

Note: All the above commands are case sensitive.

Sunday, June 30, 2013

Creating executable Jar files

    To make an executable jar file, one must have a .java file, let's create one sample:

Create a folder of some name and create the java file in there, so as to avoid confusion

HelloWorld.java


public class HelloWorld {

public static void main(String[] args) {

System.out.println("Hello World");

}
}

save it with.java extension, now we have HelloWorld.java

now open cmd from Run > cmd, hit enter

compile the java class, so to create a .class file like this:


Now we have a class file created in the drive, now create a manifest file with name like manifest.txt, this is a text file.

write in this txt (notepad) file, 

Main-Class: HelloWorld

In this file, HelloWorld is the name of Class that contains the main method, also hit enter in notepad after writing class name like this:



Now we have these files:

1. .java file
2. .class file
3. manifest file

now open the cmd and run this command in the folder that contains the java file, .class file, manifest file

jar cfm some_name.jar manifest.txt HelloWorld.class

you can use any name for the jar file that you are gonna create as long as there is a .jar after the name, when you hit enter it must not show any error like this:



 it must create a .jar file in the folder like this:


now if you double click this file it will not show you anything

right click on this file and open this file with winrar or some compression software, you may see something like this:


 Open the test.jar with winrar and it must show these files, the .class file and META-INF folder

Open META-INF folder and you'll see


open this file with notepad, like this:


and you'll see this:


After making sure these things are right, go to cmd and execute the following command:

java -jar test.jar

and this will print "Hello World", which is present in the java file


This is what we used in println in the java file, now the jar is ready and is executable.


Comments

blog comments powered by Disqus