StringBuffer Trim Java Example

[ad_1]

 /*
StringBuffer Trim Java Example
This example shows how to trim StringBuffer object in Java using substring method.
*/

public class JavaStringBufferTrimExample {

public static void main(String[] args) {

//create StringBuffer object
StringBuffer sbf = new StringBuffer(" Hello World ! ");

/*
* Method 1: convert StringBuffer to string and use trim method of
* String.
*/

String str = sbf.toString().trim();

System.out.println("StringBuffer trim: "" + str +"""""");

[ad_2]

10a. Advanced WebDriver – Using Apache ANT

[ad_1]

Hiya folks! Do you have ANTs in your build? Just kidding! Actually, I am not. Agreed that it sounds weird, but don’t fear as I am here! (Now, that’s what I call, rhyming.)

Today, let us ponder about what exactly this Apache ANT is and why it takes its place as an important building block in the construction of a WebDriver framework. By the end of this post, you should be able to handle any ANT project in general.

Jumping right in:

Another Neat Tool provided by The Apache Software Foundation is a build tool written in Java. It is old (Maven is winning this space and Gradle is getting along), quite popular and can still be found in most of the projects. The beauty of Apache ANT is that it only does what it is told to do. There isn’t much magic that is happening under the hood. (Beware, this can be a double-edged sword at times!)

To create an executable would mean,

  • Compiling the required .java files under the src folder
  • Creating the Manifest file for the JAR
  • Zipping all the .class files along with the manifest file
  • Using the jar command to create the executable

But with Apache ANT all these steps can be handled with a simple Target (we will see what a Target is, in a minute!).

Everything to be achieved is specified in an XML file, in a structured and modular way making it very easy to identify if something breaks. And this file is known as the famous, “build.xml”.

Apache ANT comes with a default buildfile named, build.xml. We can always edit or create a new XML file to suit our needs. This is basically a command-line tool. So, it is very easy to run Apache ANT from the command prompt using the command, “ant build_file_name.xml”. The best part here is, in case you did not rename the build.xml (just modified the provided default or created your own and named it build.xml), then just the command, “ant” would automatically know to look for build.xml and do what it is made for!

Apache ANT gives a lot of power, enough to cross the Thank You threshold! We can,

  • Clean up the project
  • Compile source code
  • Generate a distributable JAR or WAR
  • Handle code from version control systems such as CVS, SVN etc.
  • Echo messages to loggers and listeners at a specified level (error, warning, info, verbose, debug)
  • Create or delete directories
  • Copy, move, delete files
  • Can zip, unzip, tar, untar, unjar, unwar files
  • Execute JUnit tests, test scripts etc.
  • Execute a series of SQL statements via JDBC to a database
  • Generate JUnit test report

And do much much more…

And guess what, it sounds intimidating but every single thing that I have mentioned so far can be achieved with just the buildfile.

Time to juggle with the elements of the much-hyped, buildfiles. These are written in XML.

Sticky Note: Below information is enough to get familiarized with an Apache ANT buildfile. For in depth knowledge, please visit the Apache ANT user manual. (http://ant.apache.org/manual/)

Each buildfile has,

  1. Project – contains a minimum of one Target
  2. Target – set of Tasks that perform a unit of work
  3. Task – piece of code that can be executed

Sample build.xml file is as follows,

sample build.xml

Let us distill the buildfile one tag at a time.

Project: This has three attributes.

  1. name – The name of the project
  2. default – The default target that should be executed in case nothing is provided
  3. baseDir – The base directory from which the relative paths in the file are calculated. “.” refers to the current directory from which execution takes place

Target: As shown in the sample buildfile, a project can have one or more targets. Target is a set of Tasks. We can select which targets we would like ANT to execute by their names separated by commas. When no target is specified, then the default target is executed.

The important attribute to note here is “depends”. This specifies the target name that it is dependent on. For example, the compile target will execute only after its dependent, init target is executed. Thus, depends attribute specifies the order in which targets are to be executed.

Task: This is a piece of code that can be executed. Each task can have one or more attributes as key-value pairs.

Syntax:

<name attribute1=”value1attribute2=”value2” … />

name is the name of the task, attributeN and valueN are the attribute and value names respectively.

Here is a long list of built-in tasks that come right out of the box. We can code our own tasks as well. (Remember? It is all written in Java).

You must be wondering, why to go so deep into buildfiles rather than just installing it straight away and start some action! There is a reason for imparting so much ANT knowledge. And you will appreciate me for that as we go further in our journey. Just wait and watch…

Down to business now!

Step 1: Go to, ‘https://ant.apache.org/bindownload.cgi’ and click on ‘apache-ant-1.10.2-bin.zip’ to download the .zip file under ‘Current Release of Ant’.

Download link

Step 2: Once the .zip archive is downloaded, extract all the files to a directory on your local machine.

Step 3: Set the environment variables, ANT_HOME and Path as follows,

Right click on ‘Computer’ -> Properties -> Advanced system settings -> ‘Advanced’ tab -> Environment Variables -> Click ‘New’ under system variables.

ANT_HOME is set to the path of the folder in which Apache ANT files are extracted.

System variable settings

Similarly edit the Path variable to include %ANT_HOME%bin.

Step 4: Verify if Apache ANT is installed by typing the following command in ‘Command Prompt’

ant -version

ANT version check in cmd

Step 5: Next task is to open Eclipse IDE,

  • Right-click on the Java project -> Export
  • Under ‘General’, select ‘Ant Buildfiles’ and click ‘Next’
  • Make sure the required project is selected
  • Uncheck ‘Create target to compile project using Eclipse compiler’ in order to remove any dependency on Eclipse

Generating ANT buildfile

Click on ‘Finish’ to see an eclipse auto-generated build.xml file.

And that’s it! You are all set to run your project as ANT build. It is laughably simple, isn’t it?

Sticky Note: Eclipse comes integrated with ANT. In order to make sure Eclipse’s ‘Ant Home’ points to the latest version of ANT, click on Windows -> Preferences -> Ant -> Runtime. Click on the ‘Classpath’ tab. Expand ‘Ant Home Entries’ and verify the path. If it is pointing to a different version, then click ‘Ant Home’ and browse the location of the folder in which Apache ANT files are extracted.

In our next post let us do just that and generate a JUnit report with the help of an ANT target.

Happy installing!

Series Navigation

<< 9z. WebDriver – Window maximize

[ad_2]

StringBuffer toString Java Example

[ad_1]

/*

StringBuffer toString Java Example

This example shows how to convert StringBuffer to String in Java using

toString method of String class.

*/

 

public class JavaStringBufferToStringExample {

 

public static void main(String args[]){

//create StringBuffer object

StringBuffer sbf = new StringBuffer(“Hello World!”);

/*

* To convert StringBuffer to String object, use

* String toString() method of StringBuffer class.

*/

String str = sbf.toString();

System.out.println(“StringBuffer to String: “ + str);

}

}

 

/*

Output of above given StringBuffer to String example would be

StringBuffer to String: Hello World!

*/

[ad_2]

Java tutorials

[ad_1]

Java tutorials are the best if you are a beginner or even an experienced programmer. The good thing with Java tutorials is that they are free on the internet what you only need is an internet connection to access them and get what you want regarding Java programming.Instead of going around shopping for books you can try Java tutorials, you will be amazed by the information you will find available online about java.Java tutorials is one of the easiest and fastest means to get information about Java tips with no cost and much hassle as many think.

Internet Java tutorials are the best even to those who are total beginners who may be wishing to learn about Java than attending to class courses. Beginners can find anything by getting started with some basic skills in the Java language, essential classes and get into more specialized tutorials such as graphics, security in Java, how to work with databases and many more. There are so many websites online today that are specialized in giving Java tutorials and tips to all those who may need them. They were written by experts and then placed in different categories where they fall at for easy access. The best thing to do to understand is reading more tutorials that you come across and than combine them with the useful and short Java tips you find.
For the experienced programmers, you should share your knowledge with the newbies by writing Java tips and tutorials to help those who may need them.
All you should ensure at large when writing Java tutorials are making yourself clear and understood. You should also ensure you cover all the aspects of the problem you are writing. There are some rules you should also follow if you need people to find and understand all information you are delivering efficiently. Some of the rules are such like choosing an interesting title for your Java tip. The title should capture what you are exactly presenting. Your writing should also be accompanied by the best subtitle which will expand a little bit on what you are writing about. You can also if you wish a short and precise comment or summary to describe what your Java tips do. After completing all these, you go directly to the introduction, code listing of your Java tip, write an explanation for your tip and finally end with a conclusion. You can also add a link to direct your readers to more detailed Java tutorials on the particular subject you are writing. Java tips and tutorials are the best learning resources and improving skills in Java.

[ad_2]

StringBuffer Reset Java Example

[ad_1]

/*

StringBuffer Reset Java Example

This example shows how to reset StringBuffer object to null or empty vale using delete

method.

*/

 

public class StrinBufferResetExample {

public static void main(String[] args) {

//create StringBuffer object

StringBuffer sbf = new StringBuffer(“Hello World!”);

System.out.println(“StringBuffer content: “ + sbf) ;

/*

* To reset StringBuffer to empty value, user

* StringBuffer delete() method of StringBuffer class.

*/

sbf.delete(0, sbf.length());

System.out.println(“StringBuffer content after reset:” + sbf);

}

}

 

/*

Output of above given Java StringBuffer Reset example would be

StringBuffer content: Hello World!

StringBuffer content after reset:

*/

[ad_2]

Java StringBuffer to InputStream Example

[ad_1]

/*

Java StringBuffer to InputStream Example

This example shows how to convert StringBuffer to InputStream in Java using

ByteInputStream class.

*/

 

 

import java.io.ByteArrayInputStream;

import java.io.InputStream;

 

public class StringBufferToInputStreamExample {

public static void main(String args[]){

//create StringBuffer object

StringBuffer sbf = new StringBuffer(“StringBuffer to InputStream Example”);

/*

* To convert StringBuffer to InputStream in Java, first get bytes

* from StringBuffer after converting it into String object.

*/

byte[] bytes = sbf.toString().getBytes();

/*

* Get ByteArrayInputStream from byte array.

*/

InputStream inputStream = new ByteArrayInputStream(bytes);

System.out.println(“StringBuffer converted to InputStream”);

}

}

[ad_2]