Saturday, 13 February 2016

Create a new directory using Java

In today's example we'll create a new directory using Java. Check out the following code below :

import java.io.*;

public class Create_Directory {

public static void main(String[] args) {
File dir = new File("//Users//ironcladzone//Downloads//TestDir");
boolean DirectoryCreated = dir.mkdir();
if (DirectoryCreated)
System.out.println("Created a new directory");
else
System.out.println("Directory not created");
}


}

Note that we have used the function mkdir() as expected. It returns the value "true" if the directory is created or "false" if not created. We use the variable 'DirectoryCreated' to store this boolean value, based on which it prints a message.

Try executing this program twice. The first time when you execute the program, a directory gets created and you get the output like :

Created a new directory

The second time when you execute the code, a directory does not get created since it already exists. So you'll get the output like :

Directory not created

Accept User Input in Java

For accepting user input in Java refer the following piece of code below :

import java.io.*;

public class User_Input {

public static void main(String[] args) {
String inp="";
System.out.println("Please enter your name : ");
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
inp=br.readLine();
}
catch(IOException e) {
e.printStackTrace();
}
System.out.println("Welcome " +inp);

}


}

Output:
Please enter your name : 
IronCladWriter
Welcome IronCladWriter

Make sure to import java.io.* so as to use the input/output functions. Notice that we have wrapped an InputStreamReader within a BufferedReader. Guys, do check out detailed documentation on BufferedReader and InputStreamReader for better understanding.

Friday, 12 February 2016

Basic string manipulation in Java with Real Examples : Part 1

Hi guys, how you doing today? I just noticed I haven't written anything about Java in quite some time now. Well let's bring Java back into the spotlight with this light subject on basic string manipulation.

Lets try to cover some simple examples covering different scenarios :

Illustration 1 : Extract a substring from a string

In this example we simply extract a part of the string.

public class String_manip {

public static void main(String[] args) {
 String str = "Hello guys. Welcome to IroncladZone";
 
 String sub_str = str.substring(23, 35);
 
 System.out.println (sub_str);
}

}

Output : IroncladZone

Illustration 2 : Replace a substring from a string

In this example we replace the word "guys" with "goodfellas" within the string.

public class String_manip {

public static void main(String[] args) {
String str = "Hello guys. Welcome to IroncladZone";
String replaced_str = str.replace("guys", "goodfellas");
System.out.println(replaced_str);

}

}

Output : Hello goodfellas. Welcome to IroncladZone

Note that strings are immutable. Their value cannot be changed once its created. However we can create a new string with the replaced word/part.

Illustration 3 : Join / Combine two strings i.e Concatenation

In this we simply join 3 strings together using +

public class String_manip {

public static void main(String[] args) {
String str1 = "Welcome";
String str2 = "to";
String str3 = "IroncladZone";
System.out.println(str1 + str2 + str3);
System.out.println(str1 + " " + str2 + " " + str3);

}

}

Output : 
WelcometoIroncladZone
Welcome to IroncladZone

Illustration 4 : Print the length of the string


public class String_manip {

public static void main(String[] args) {
String str = "Welcome to IroncladZone";
System.out.println(str.length());

}

}

Output :
23

Note the length() function used to show the length of the string.

Illustration 5 : Split a string on a whitespace

public class String_manip {

public static void main(String[] args) {
String str = "Welcome to IroncladZone";
String[] new_str = str.split("\\s+");
System.out.println(new_str[0]);

}


}

Output :
Welcome

Notice the \\s+ regex for the whitespace.

That's it for today fellas. I'll try to include some more advanced scenarios in a 2nd part of this post. There's more to come guys, stay tuned...

Thursday, 11 February 2016

How to list all Components of a Stream in IBM RTC

Inorder to list all the components of a stream in IBM RTC from the command line, run the following command.

Usage :

lscm list components -r repository-connection-name -w stream-name

Eg : 

lscm list components -r https://jtsrv.uk.hibm.company:9094/jazz -w htse.mydocs.midata.dev

Notice that for this command we haven't used quotes anywhere in the syntax. Also note for the -w switch for workspace, we need to supply the stream name.

The output would be obviously the list of components as expected, something like :

Workspace: (1001) "htse.mydocs.midata.dev"
     Component: (1091) "htse-mydocs-abc"
     Component: (1092) "htse-mydocs-xyz"
     Component: (1093) "htse-mydocs-pqr"

How to list all Streams of a project area in IBM RTC

In IBM Rational Team Concert, if you want to list all streams of a project area from the command line, here's a quick reference.

Usage :

lscm list streams --projectarea "project-area-name" -r repository-connection-name

Eg :

lscm list streams --projectarea "pdfservice.channels.group.prd" -r https://jtsrv.uk.hibm.company:9094/jazz

The output would be the names of all streams something like :

(1000) "htse.midata.dev"
(1001) "htse.mydocs.dev"
(1002) "pdfservice.channels.group.stream"

Kindly note that as per the syntax we need to use quotes only for the project area name and not for the repository connection.

How to list all Project Areas in IBM RTC from command line

Inorder to list all the project areas of a repository connection in IBM Rational Team Concert, run the following command :

lscm list projectareas -r repo-name

Eg :

lscm list project areas -r https://jtsrv.uk.hibm.company:9094/jazz

The output will list all the available project areas for only that repository.

Create Runnable Jar in Eclipse

Here's a quick pictorial on how to create an executable jar in Eclipse. Assuming you have a Java project in place, let's take a quick look into the steps.

Go to File menu -> Export. Expand Java to choose Runnable JAR File and click Next. Check out the screenshot for reference.


In the next screen, you simply have to choose the Destination folder where you want to save the executable Jar. Also choose the java application's Launch configuration. It also lets you save an Ant script describing the Jar file that you created. Check out this link for official detailed reference.
Related Posts Plugin for WordPress, Blogger...
eXTReMe Tracker