To Save Image By Using Image

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
/*
* @Author : Spice
* You Should give a File path to save
*/
public class SaveImage {
public static String stt;

public static void saveImage(File fileToSave )
{

// String stt=ImagePanel.pathname;
FileInputStream from = null;
FileOutputStream to = null;
try {
from = new FileInputStream(stt);
to = new FileOutputStream(fileToSave);
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = from.read(buffer)) != -1)
to.write(buffer, 0, bytesRead);// write
to.close();
}catch(NullPointerException e){
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
}

}

Reading XL Sheet By Using Java

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

/**
*
* @author spice
*/
public class MSPDetails {

final static String[] header = {"S.No", "Phone No", "Name", "Address"};

public static void main(String[] args) throws IOException, BiffException {
int option;
Workbook wb = Workbook.getWorkbook(new File("sample.xls"));
Sheet sheet1 = wb.getSheet(0);
Sheet sheet2 = wb.getSheet(1);
ArrayList notDisturbList=new ArrayList();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
do {
System.out.println("############# MENU ################");
System.out.println("Select the criteria: \n 1 for name \n 2 for address \n 3 for quit");
System.out.println("Enter your option :");
option = Integer.parseInt(br.readLine());
System.out.println("########################################");
switch (option) {
case 1:
System.out.println("Enter employee Name:");
findByCriteria(sheet1, sheet2, "name", br.readLine());
break;
case 2:
System.out.println("Enter employee Address:");
findByCriteria(sheet1, sheet2, "address", br.readLine());
break;
case 3:
System.out.println("Thank Q");
break;
default:
System.out.println("Invalid choise..Enter correct choise");
}
} while (option != 3);

}

static void findByCriteria(Sheet sheet1, Sheet sheet2, String criteria, String key) throws IOException {
boolean isFound = true;
Cell nameCell = sheet1.findCell(criteria);
int nameCellNo = nameCell.getColumn();
Cell[] listName = sheet1.getColumn(nameCellNo);
Cell phno = sheet2.findCell("phone no");
int phnoCellNo = phno.getColumn();
Cell[] dontDisturbList = sheet2.getColumn(phnoCellNo);
int phColNo = sheet1.findCell("phno").getColumn();

System.out.println("PHONE NUMBER");
for (int row = 1; row < listName.length; row++) {
Cell[] rowCell = sheet1.getRow(row);

if (criteria.equals("name") ? listName[row].getContents().equals(key) : listName[row].getContents().contains(key)) {

// System.out.println(rowCell[phColNo].getContents());

for (int ddRow = 1; ddRow < dontDisturbList.length; ddRow++) {

if (rowCell[phColNo].getContents().equals(dontDisturbList[ddRow].getContents())) {

isFound = false;

System.out.println("This is Dont Disturb Number");
}
}
}
if (isFound) {

System.out.println(header[phColNo] + ":" + rowCell[phColNo].getContents());
}
}
if (!isFound) {
System.out.println("Details with " + criteria + " " + key + " doesn't exist");
}

}


}

Important Methods in String class

Here most common used String Methods Discussed Here


  • charAt() - Returns the Character located at at the specified index for Example :
    String x="Computer";
    System.out.println(x.charAt(2)); // output is 'm'

  • concat() - Appends one String to the end of another ("+" also works), ex:
    String x="motor";
    System.out.println(x.concat(" cab"));
    // output is "motor cab"

  • equalIgnoreCase() - Determines the equality of two Strings,ignoring case.
    String x = "HERO"
    System.out.println(x.equalIgnoreCase("Hero"));
       //is "true"
    System.out.println(x.equalIgnoreCase("reho"));    is "false"

  • length() - Returns the number of characters in a String.
    String x="Computer"
    System.out.println(x.length());     
    //     is 8

  • replace() - Replaces occurences of character with a new character

  • substring () - Returns a part of String
    String x = "0123456789"
    System.out.println(x.substring(5));     // output is "56789"
    System.out.println(x.substring(5,8));     //output is "567"

  • toLowerCase() - Returns a String with uppercase characters converted
    String x= "COMPUTERS";

    System.out.println(x.lowerCase());   //output is "computers"

  • toString() - returns the value of String
    String x = "Rama";
    System.out.println(x.toString());     // for your exercise buddy...

  • toUpperCase() - Returns a String with lowercase characters converted
    String x= "science";

    System.out.println(x.lowerCase());   //output is "SCIENCE"

  • trim() - Removes white spaces from ends of the String
    String x = " hi ";
    System.out.println(x + "x");    // result is " hi x"
    System.out.println(x.trim() + "x");    // result is " hix"

The String



In java String are objects.Just like other objects, we can create an instance of a String with the new Keyword ,as follows

String s=new String();

s="abcdef";
As you might expect, the String class has about a zillion constructors,so you can use a more efficient shortcut :

String s = new String("abcdef");

And just because you'll use strings all the time,you can even say this:

String s="abcdef";

Strings are immutable , means Once you have assigned a String value that value can never change. The good news
is that while the String object is immutable, its reference variable is not,
lets discussion start....

String s="abcdef";
// create a new string object ,with value "abcdef",refer s to it
String s2 = s;
// create a 2nd reference variable refferring to the same String

S = s.concat("more stuff");
// create a new String object, with value "abcdef more stuff", refer s to it
// change s's reference from the old String to the new String. ( R emember s2 is still referring
to the original "abcdef" String.)

The figure will give detailed explaination: