Posts which you will also like.

Monday, January 14, 2013

Using Regular Expressions in Java



In this java tutorial we will learn to use regular expression in java.Regular expression is very powerful tool for text manipulation and searching.It provides the fastest searching in huge amount of available data.So if you know it definitely you will save a lot time while doing these operations.In this tutorial I am going to describe how to use regular expression in java,I will not teach you the regular expression(If you are new to regular expression then please update yourself first via techcresendo.com)
In java we have java.util.regex package for regular expressions.This package basically contains three classes
  • Pattern:  This class represents the pattern in regular expression.Pattern is basically a format of text which needs to be matched in a group of texts.Patterns are constructed using regular expressions. e.g pattern
  • "[tT]rue" matches true or True

  • Matcher:  This class does the matching of the compiled pattern against an input string.

  • PatternSyntaxException: This class represents the exception which are thrown when your regular expression pattern is not correct.(It is unchecked exception)
Steps to create a pattern matching program:
  1. Create a pattern object.This object defines your regular expression.                                                                                                                           Pattern pattern=Pattern.compile(“regex”);

  2. Now with the help of Pattern object we can get Matcher object for a given input text(into which pattern to be  matched).                                                                                                                                                       Matcher matcher=pattern.matcher(INPUT_TEXT); 

  3. Now using this matcher object  we can easily find the pattern in the text as we will see in the example program.

package com.techy.rajeev;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PatternMatch {
  public static final String INPUT_TEXT = "This is a test for regular expression Testing.";
  public static final String PATTERN = "[tT]est";
  
  public static void main(String[] args) {
    Pattern pattern = Pattern.compile(PATTERN);
    Matcher matcher = pattern.matcher(INPUT_TEXT);
    while (matcher.find()) {
      System.out.print("Start pos: " + matcher.start());
      System.out.print(" End pos: " + matcher.end() + " ");
      System.out.println(matcher.group());
    }
  }
} 



Output:

Start pos: 10 End pos: 14 test
Start pos: 38 End pos: 42 Test
Read More ->>

Tuesday, December 25, 2012

How to create SFTP connection using java ?



In this blog post we will learn how to create a secure FTP connection using java.SFTP is much more secure than its predecessor FTP since SFTP is based on SSH.
Here We will develop a SFTP client.Basically SFTP clients are programs that use SSH to access, manage, and transfer files. SFTP clients are functionally similar to FTP clients, but they use different protocols.
To create a SFTP connection to a server for file transfer or any other FTP operation we will use an open source library called JSCh. You can download this library from http://www.jcraft.com/
Add this library to your class path and start creating the program.
This example shows how to get a file from the server using SFTP.
package com.techy.rajeev;

import com.jcraft.jsch.*;

    /**
     * @param args
     */

    public class SFTPTest {
    public static void main(String args[]) {
        JSch jsch = new JSch();
        Session session = null;
        String username="";//Put your username here
        String password="";//Put your password here
        String host="";//Hostname
        String sourceFile="";//Path of file on the server
        String destFile="";//Path of local file
        try {
            session = jsch.getSession(username,host,22);
            session.setConfig("StrictHostKeyChecking", "no");
            session.setPassword(password);
            session.connect();

            Channel channel = session.openChannel("sftp");
            channel.connect();
            ChannelSftp sftpChannel = (ChannelSftp) channel;

            sftpChannel.get(sourceFile,destFile);
            sftpChannel.exit();
            session.disconnect();
        } catch (JSchException e) {
            e.printStackTrace(); 
        } catch (SftpException e) {
            e.printStackTrace();
        }
    }
Read More ->>

Monday, December 17, 2012

Java program to generate all possible sub string of a string


This simple java program generates all possible sub string of given string.You can replace string with StringBuilder or StringBuffer where performance is issue.
package com.techy.rajeev;

public class GenStr {

    /**
     * @param args
     */
    public static void genAllSubStr(String str){
        for(int i=0;i<str.length();i++){
            for(int j=i;j<str.length();j++){
                System.out.println(str.substring(i, j+1));
            }
        }
    }
    public static void main(String[] args) {
        genAllSubStr("abc");
    }

}


Output:

a
ab
abc
b
bc
c

Read More ->>

Tuesday, December 4, 2012

Java program to generate permutation of a string


This post is about generating permutation of a string using a java program.This program has a static method  genPerm() method which is used for generation of permutation of the string. genPerm() method is recursive and takes two string arguments and prints the permutations of the second string.
Source Code:
public class GeneratePerm {

    /**
     * @param args
     */
    public static void genPerm(String pString,String sb){
        if(pString.length()==3)
            System.out.println(pString);
        for(int i=0;i<sb.length();i++){
            genPerm(pString+sb.charAt(i),sb.replaceFirst(sb.charAt(i)+"",""));
        }
    }
    public static void main(String[] args) {
        String sb=new String("abc");
        genPerm("",sb);
    }

}

Output:
abc
acb
bac
bca
cab
cba

Read More ->>
DMCA.com Protected by Copyscape Online Plagiarism Tool