Tuesday, 24 July 2012

Convert CSV file to Excel File

Convert CSV file to Excel File 

Add this poi-2.5.1.jar to class path



import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.StringTokenizer;
import java.util.*;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.*;
import java.io.*;

public class ExcelConverter {
public static void main(String args[]) throws IOException {
ArrayList arList = null;
ArrayList al = null;
String fName = "c:\\hai.csv";
String thisLine;
int count = 0;
FileInputStream fis = new FileInputStream(fName);
DataInputStream myInput = new DataInputStream(fis);
int i = 0;
arList = new ArrayList();
while ((thisLine = myInput.readLine()) != null) {
al = new ArrayList();
String strar[] = thisLine.split(",");
for (int j = 0; j < strar.length; j++) {
al.add(strar[j]);
}
arList.add(al);
System.out.println();
i++;
}

try {
HSSFWorkbook hwb = new HSSFWorkbook();
HSSFSheet sheet = hwb.createSheet("new sheet");
for (int k = 0; k < arList.size(); k++) {
ArrayList ardata = (ArrayList) arList.get(k);
System.out.println("ardata " + ardata.size());
HSSFRow row = sheet.createRow((short) 0 + k);
for (int p = 0; p < ardata.size(); p++) {
System.out.print("  data is "+ ardata.get(p));
HSSFCell cell = row.createCell((short) p);
cell.setCellValue(ardata.get(p).toString());
}
System.out.println("  control reached aa");
FileOutputStream fileOut = new FileOutputStream(
"C:\\excel\\xfile.xls");
System.out.println("file output stream");
hwb.write(fileOut);
fileOut.close();
System.out.println("Your excel file has been generated");
}
} catch (Exception ex) {
} // main method ends
}
}

Monday, 23 July 2012

HashMap and HashTable Example

HashMap and HashTable Example



package com.demo;

import java.util.Hashtable;

public class HashExample {

public static void main(String[] args) {
Hashtable<String, Object> sample = new Hashtable<String, Object>();
sample.put("name", "namratha");
/*if(sample.contains("namratha"))
{
System.out.println("hello if block success ");
}
else
System.out.println("else block");*/
/* if(sample.containsKey("name"))
System.out.println("if block");
else
System.out.println("else block");*/
/*if(sample.containsValue("namratha"))
System.out.println("if block");
else
System.out.println("else block");*/
System.out.println("get value is "+sample.get("name"));
if(sample.get("name").equals("namratha"))
{
System.out.println("if block success");

}
else
System.out.println("else");



}


}








package com.demo;

import java.util.HashMap;

public class HashMapClass{
public static void main(String[] args) {
HashMap<String,String> sample = new HashMap<String, String>();
sample.put("name", "asher");
/*if(sample.containsKey("name"))
System.out.println("if block");
else
System.out.println("else block");*/
/* if(sample.containsValue("asher"))
System.out.println("if block");
else
System.out.println("else block");*/
if(sample.get("name").equals("asher"))
System.out.println("if block");
else
System.out.println("else block");
}

}

Sunday, 22 July 2012

String in ascending order

String in ascending order

package com.demo;

public class StringAscending {
public static void main(String[] args) {
String[] names = {"namratha","anita","asher","heena","jagruthi"};
String str = "this is my new string";
char[] content = str.toCharArray();
java.util.Arrays.sort(content);
 String sorted = new String(content);
 System.out.println(content);
}

}

Display String array in ascending and descending order

Display String array in ascending and descending order

package com.demo;

import java.util.Arrays;

import java.util.Comparator;

public class StringOperations {
public static void main(String[] args) {

String[] names = {"Chenni", "Mumbai", "Delhi","Agra","Andhra","Patna"};

// Ascending
System.out.println("Ascending");
System.out.println("---------");
Arrays.sort(names);
displayLoop(names);
// Descending
System.out.println("Descending");
System.out.println("----------");
Comparator<String> descending = new DescendingComparator();
Arrays.sort(names,descending);
displayLoop(names);
}

private static void displayLoop(String[] names) {
for(String namedisplay:names)
{
System.out.println(""+namedisplay);
}
}
}


package com.demo;

import java.util.Comparator;

public class DescendingComparator implements Comparator<String> {

@Override
public int compare(String str1, String str2) {
// TODO Auto-generated method stub
return str2.compareTo(str1);
// return str1.compareTo(str2);
// return str1.concat(str2);
}

}

ArrayList Sorting Example


ArrayList Sorting Example


package com.demo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class SortArrayList {

 public static void main(String[] args) {

   //create an ArrayList object
   List<String> arrayList = new ArrayList<String>();

   //Add elements to Arraylist
/* arrayList.add("A");
   arrayList.add("B");
   arrayList.add("C");
   arrayList.add("D");
   arrayList.add("E");*/

   arrayList.add("1");
   arrayList.add("5");
   arrayList.add("8");
   arrayList.add("2");
   arrayList.add("0");

   /*
     To get comparator that imposes reverse order on a Collection use
     static Comparator reverseOrder() method of Collections class
   */
   Collections.sort(arrayList);

    System.out.println("sorted list is "+arrayList);
 

   Comparator comparator = Collections.reverseOrder();

   System.out.println("Before sorting ArrayList in descending order : "
                                                             + arrayList);

   /*
     To sort an ArrayList using comparator use,
     static void sort(List list, Comparator c) method of Collections class.
   */

   Collections.sort(arrayList,comparator);
   System.out.println("After sorting ArrayList in descending order : "
                                                             + arrayList);

 }
}

Comparable and Comparator usage for ascending and descending order Obj ascending and descending

Comparable and Comparator usage for ascending and descending order
Obj ascending and descending



package com.demo;

import java.util.Arrays;


public class SortFruitObject{
 
public static void main(String args[]){
Fruit[] fruits = new Fruit[4];
Fruit pineappale = new Fruit("Pineapple", "Pineapple description",70); 
Fruit apple = new Fruit("Apple", "Apple description",100); 
Fruit orange = new Fruit("Orange", "Orange description",80); 
Fruit banana = new Fruit("Banana", "Banana description",90); 
fruits[0]=pineappale;
fruits[1]=apple;
fruits[2]=orange;
fruits[3]=banana;
//to sort based on one property 
//Arrays.sort(fruits);
//to sort based on more than one property
// Arrays.sort(fruits,Fruit.FruitNameComparator);
int i=0;
for(Fruit temp: fruits){
  System.out.println("fruits " + ++i + " : " + temp.getFruitName() + 
", Quantity : " + temp.getQuantity());
}
}
}






package com.demo;

import java.util.Comparator;

public class Fruit implements Comparable<Fruit> {

private String fruitName;
private String fruitDesc;
private int quantity;

public Fruit(String fruitName, String fruitDesc, int quantity) {
super();
this.fruitName = fruitName;
this.fruitDesc = fruitDesc;
this.quantity = quantity;
}

public String getFruitName() {
return fruitName;
}

public void setFruitName(String fruitName) {
this.fruitName = fruitName;
}

public String getFruitDesc() {
return fruitDesc;
}

public void setFruitDesc(String fruitDesc) {
this.fruitDesc = fruitDesc;
}

public int getQuantity() {
return quantity;
}

public void setQuantity(int quantity) {
this.quantity = quantity;
}

public int compareTo(Fruit compareFruit) {

int compareQuantity = ((Fruit) compareFruit).getQuantity();

// ascending order
return this.quantity - compareQuantity;

// descending order
// return compareQuantity - this.quantity;

}

// use this to sort based on more than one property

public static Comparator<Fruit> FruitNameComparator = new Comparator<Fruit>() {

public int compare(Fruit fruit1, Fruit fruit2) {

String fruitName1 = fruit1.getFruitName().toUpperCase();
String fruitName2 = fruit2.getFruitName().toUpperCase();

// ascending order
return fruitName1.compareTo(fruitName2);

// descending order
// return fruitName2.compareTo(fruitName1);
}

};
}


String Display in ascending and descending using comparator interface in java

String Display in ascending and descending using comparator interface in java


package com.demo;

import java.util.Arrays;
import java.util.Comparator;

public class StringOperations {
public static void main(String[] args) {

String[] names = {"Chenni", "Mumbai", "Delhi","Agra","Andhra","Patna"};

// Ascending
System.out.println("Ascending");
System.out.println("---------");
Arrays.sort(names);
displayLoop(names);
// Descending
System.out.println("Descending");
System.out.println("----------");
Comparator<String> descending = new DescendingComparator();
Arrays.sort(names,descending);
displayLoop(names);
}

private static void displayLoop(String[] names) {
for(String namedisplay:names)
{
System.out.println(""+namedisplay);
}
}
}


Class DescendingComparator


package com.demo;

import java.util.Comparator;

public class DescendingComparator implements Comparator<String> {

@Override
public int compare(String str1, String str2) {
// TODO Auto-generated method stub
return str2.compareTo(str1);
// return str1.compareTo(str2);
// return str1.concat(str2);
}

}


Saturday, 21 July 2012

Java program which takes path of directory as input and displays all files and its names with out extension present in the directory


Java program which takes path of directory as input and displays all files and its names with out extension present in the directory


Put Commons-io 2.3 version jar in lib folder and add to class path and then run


package com.demo;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.TrueFileFilter;

public class InputFolder {
public static void main(String[] args) throws IOException {
InputStreamReader inp = new InputStreamReader(System.in);
   BufferedReader br = new BufferedReader(inp);

   System.out.println("Enter folder name : ");
 

String str = br.readLine();
File f1 = new File(str);
System.out.println("folder name is "+str);
System.out.println("file is "+f1);

fetch_files(f1);

}

private static void fetch_files(File fpath) throws IOException {



List<File> files = (List<File>) FileUtils.listFiles(fpath, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE);
for (File file : files) {
System.out.println("file: " + "  path is  " +file.getCanonicalPath()+"   name is  "+file.getName().substring(0, file.getName().lastIndexOf('.')));
//System.out.println("ar is "+list.getName().substring(0, list.getName().lastIndexOf('.')));

}


}
}

Java program which creates file dynamically

Java program which creates file dynamically


Put Commons-io 2.3 version jar in lib folder and add to class path and then run


package com.demo;

import java.io.File;

import java.io.IOException;

public class CreateFIle{
 public static void main(String[] args) throws IOException {
 File f;
 f=new File("c:\\example" + File.separator + "kop.txt");
 System.out.println("file is "+f);
 System.out.println("check "+f.exists());
 System.out.println("create "+f.createNewFile());
 System.out.println("The absolute path of the file is: "
 +f.getAbsolutePath());  
}
}

stored as karaf documents in sent mail in sonuasher89@gmail.com and even felix document also stored there

Karaf commands in console how to start and stop the bundles


stored as  karaf documents in sent mail in sonuasher89@gmail.com and even felix document also stored there

Tuesday, 17 July 2012

Json Object conversion it is stored with jsonobject subject

Json Object conversion it is stored with jsonobject subject in sonuasher89@gmail.com


package com.demo;

import javax.xml.bind.annotation.XmlElement;

import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlRootElement(name = "book")
// If you want you can define the order in which the fields are written
// Optional
@XmlType(propOrder = { "author", "name", "publisher", "isbn" })
public class Book {

private String name;
private String author;
private String publisher;
private String isbn;

// If you like the variable name, e.g. "name", you can easily change this
// name for your XML-Output:
@XmlElement(name = "title")
public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getAuthor() {
return author;
}

public void setAuthor(String author) {
this.author = author;
}

public String getPublisher() {
return publisher;
}

public void setPublisher(String publisher) {
this.publisher = publisher;
}

public String getIsbn() {
return isbn;
}

public void setIsbn(String isbn) {
this.isbn = isbn;
}

}







package com.demo;

import java.io.ByteArrayInputStream;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.io.Writer;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import org.apache.commons.io.FileUtils;
import org.codehaus.jackson.map.ObjectMapper;

public class BookMain {

private static final String BOOKSTORE_XML = "C:/senddata/iPod.txt";

public static void main(String[] args) throws JAXBException, IOException {
try {
String userDataJSON = getUserDataJSON(BOOKSTORE_XML);
 InputStream json = new ByteArrayInputStream(userDataJSON.getBytes());
File destDir = new File("JSON.txt");
FileUtils.copyInputStreamToFile(json, destDir);
System.out.println(userDataJSON);
} catch (Exception e) {
e.printStackTrace();
}
}
public static String getUserDataJSON(String BOOKSTORE_XML) throws Exception
{
JAXBContext context = JAXBContext.newInstance(BookStore.class);
Unmarshaller um = context.createUnmarshaller();
BookStore bookstore2 = (BookStore) um.unmarshal(new FileReader(BOOKSTORE_XML));
// System.out.println(bookstore2.getBooksList());
// for(Book book : bookstore2.getBooksList())
// {
// System.out.println(book.getAuthor() + "  " + book.getName());
// }
ObjectMapper mapper = new ObjectMapper();
Writer strWriter = new StringWriter();
mapper.writeValue(strWriter, bookstore2);
return strWriter.toString();
}
}








package com.demo;

import java.util.ArrayList;



import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;

//This statement means that class "Bookstore.java" is the root-element of our example
@XmlRootElement(name = "bookstore")
public class BookStore {

// XmLElementWrapper generates a wrapper element around XML representation
@XmlElementWrapper(name = "bookList")
// XmlElement sets the name of the entities
@XmlElement(name = "book")
private ArrayList<Book> bookList;
private String name;
private String location;

public void setBookList(ArrayList<Book> bookList) {
this.bookList = bookList;
}

public ArrayList<Book> getBooksList() {
return bookList;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getLocation() {
return location;
}

public void setLocation(String location) {
this.location = location;
}
}




package com.demo;

import org.json.JSONObject;

import com.liferay.portal.kernel.json.JSONFactoryUtil;


class Sample
{
public static void main(String[] args) {



JSONObject jsonObj = (JSONObject) JSONFactoryUtil.createJSONObject();
}
}



POM



<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.demo</groupId>
<artifactId>TestProj</artifactId>
<version>0.0.1-SNAPSHOT</version>

<dependencies>

<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-jaxrs</artifactId>
<version>1.9.6</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.6</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.6</version>
</dependency>

<dependency>
<groupId>com.liferay.portal</groupId>
<artifactId>portal-service</artifactId>
<version>6.0.4</version>
</dependency>

<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20090211</version>
</dependency>
</dependencies>

<build>
<plugins>

<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>1.4.0</version>
<extensions>true</extensions>
<configuration>
<instructions>

<Export-Package>com.demo;version="0.0.1-SNAPSHOT",
</Export-Package>


</instructions>
</configuration>
</plugin>
</plugins>
</build>


</project>

Java property file example

Java program which takes the value from external files

It is stored with property file in sonuasher89 in inbox


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

public class PropertiesClass {

public static void main(String[] args) 
{
Properties props = new Properties();
try {
props.load(new FileInputStream(new File("C:/Documents and Settings/folder/Wrk_SPS/PropertiesTest/src/sample.properties")));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(props.getProperty("sample"));
}
}



Saturday, 14 July 2012

Performace testing set up for Jclouds video upload to amazon s3 is done using Junit perf and is present in sent mails in sonuasher89@gmail.com with the subject  Test_set_up_performace_testing
 It can be run in eclipse just we need to set up maven plugin in the eclipse

Tuesday, 10 July 2012

Storage Mechanism of CBS


My  CBSArchiver Client  and and CBSArchiver are stored in mail with name updated_client and updated_server it is stored in sent mails in namratha.asher@gmail.com it was made run in netbeans

Wednesday, 4 July 2012

Jclouds Karaf Usage necessary Urls



install karaf : http://karaf.apache.org/index/community/download.html
starting karaf : Unzip the download go into folder and run bin\karaf.bat
Make sure maven is in your path
Install jclouds. On karaf shell
Features:addUrl mvn:org.jclouds.karaf/jclouds-karaf/1.5.0-alpha.6/xml/features
Features:install jclouds-api-filesystem [ call features:list | grep jclouds for exact name]
Compile the attached source code The zip only contains the src files and features.xml directory
Features:addUrl file://///features.xml/features.xml  (This is present in project)





To change the mvn url use following on karaf shell:
Features:listUrl
Features:removeUrl <Url with jclouds in it>
Features:addUrl <Url with jclouds. Change .6 to .1>
Features:install jclouds-api-filesystem



.cfg file


public_key_file=C:/AE2-035/cloudStorage/public.key
private_key_file=C:/AE2-035/cloudStorage/private.key
secret_key=value of secnote
access_key=value of accnote
cloud_provider=filesystem
filesystem_base_directory=C:/
mRootContainer=CBSCloudArchive

Tuesday, 3 July 2012

Class Interaction example

Class Interaction



package com.demo;

public class VideoConfig {
VideoConfig configdata;
String public_key;
String private_key;
public String getPublic_key() {
return public_key;
}
public void setPublic_key(String public_key) {
this.public_key = public_key;
}
public String getPrivate_key() {
return private_key;
}
public void setPrivate_key(String private_key) {
this.private_key = private_key;
}

}


package com.demo;

public class VideoConfigMng {
public static void main(String[] args) {
VideoConfigMng sample;
VideoConfig configobj;
VideoConfig configdata = new VideoConfig();
configdata.setPrivate_key("pri");
configdata.setPrivate_key("pri");
configobj = configdata;
VideoUploadImpl obj_impl = new VideoUploadImpl(configdata);
System.out.println("output "+configobj.getPrivate_key());
}

}



package com.demo;

public class VideoUploadImpl {
VideoConfig mconfigObj;

public VideoUploadImpl(VideoConfig configobj) {
mconfigObj = configobj;
System.out.println("output in contructor is"+mconfigObj.getPrivate_key());
}

}



Pass a HashMap from Angular Client to Spring boot API

This example is for the case where fileData is very huge and in json format   let map = new Map<string, string>()      map.set(this.ge...