Java-Friend has articles on Java, J2EE, Java SCJP Dumps, Core Java Concepts, Java eBooks, Java Database Connectivity (JDBC) , Java Servlets , Java server Pages (JSP) , Java Versions, Struts Framework, Hibernate , WebLogic Application Server, Swings, Spring Framework , AJAX , JMS , Enterprise Java Beans (EJB) , Java Script. Also get Java faqs, Interview Questions.
Friday, April 18, 2014
Why wait, notify and notifyAll is defined in Object Class in Java
Sunday, September 9, 2012
Increase Java Heap Memory for Maven 2
Update build.bat with following command
set MAVEN_OPTS=-Xmx512m
Update build.sh with following command
export MAVEN_OPTS=-Xmx512m
Wednesday, July 13, 2011
How to Read File in Java
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
* This program reads a file line by line and print to the console.
*
*/
public class FileRead {
public static void main(String[] args) {
File file = new File("C:\\MyFile.txt");
FileInputStream fis = null;
BufferedInputStream bis = null;
DataInputStream dis = null;
try {
fis = new FileInputStream(file);
//BufferedInputStream is added for fast reading.
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
// dis.available() returns 0 if the file does not have more lines.
while (dis.available() != 0) {
// Reads the line from the file and print it to the console.
System.out.println(dis.readLine());
}
// close all the resources after using them.
fis.close();
bis.close();
dis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Monday, August 9, 2010
String.contains and String.indexOf
To check if a string contains a substring, I usually use:
if(s.indexOf(sub) >= 0)For JDK 5 or later, a
contains method can also be used, which seems to be a little more readable:if(s.contains(sub))The signature of
contains method is:public boolean contains(java.lang.CharSequence s);Note that
CharSequence is a super-interface of String, StringBuffer, StringBuilder, java.nio.CharBuffer, javax.swing.text.Segment. So you can pass any of the 5 types to contains method.The current implementation of
contains method just convert the param to String and calls indexOf.
Monday, June 21, 2010
Factorial Using Recursion - Java
import java.io*; class Factorial{
public static void main(String[] args) {
try{
BufferedReader object = new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter the number");
int a= Integer.parseInt(object.readLine());
int fact= 1;
System.out.println("Factorial of " +a+ ":");
}
catch (Exception e){}
}
int factorial(int n)
{
if (n == 1) {
return n;
}
else {
return n * factorial(n - 1);
}
}Factorial Examples - Java
import java.io*; class Factorial{
public static void main(String[] args) {
try{
BufferedReader object = new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter the number");
int a= Integer.parseInt(object.readLine());
int fact= 1;
System.out.println("Factorial of " +a+ ":");
for (int i= 1; i<=a; i++){
fact=fact*i;
}
System.out.println(fact);
}
catch (Exception e){}
}
}
Friday, June 11, 2010
Syntel interview questions
Difference between hashmap and hashset?
Difference between include and forward?
Difference between forward and sendredirect?
can abstract class contain concrete methods(one with implementation)? if yes then why to make that class abstract and not simple java class?
why do we use/need session beans?
which jdbc driver you have used?
To check object's uniqueness which methods we need to override?
How many times servlet's init() method is called in it's one lifecycle?
Can we call servlet's destroy() method from init() method?
Can we control number of servlet instances created by container?
Monday, April 27, 2009
How to read MS Excel file in Java
HSSF
Here Excel 97 file format is called "HSSF," which stands for, you guessed it, Horrible SpreadSheet Format. (We admire their method of making simple things complicated and oversimplifying things that should have been done with more flexibility.) HSSF may have a comical name, but is a very serious API. HSSF lets you read, write, and modify Excel files using nothing but Java.
HSSF APIs
Go to the Jakarta.apache.org/poi site and download the latest binary for the POI project.HSSF has two APIs for reading: usermodel and eventusermodel. The former is most familiar, and the latter is more cryptic but far more efficient. The usermodel consists primarily of the classes in the org.apache.poi.hssf.usermodel package, as well as org.apache.poi.hssf.eventusermodel. (In earlier versions of HSSF, this was in the eventmodel package.) The usermodel package maps the file into familiar structures like Workbook, Sheet, Row, and Cell. It stores the entire structure in memory as a set of objects. The eventusermodel package requires you to become more familiar with the actual low-level structures of the file format. It operates in a manner similar to XML's SAX APIs or the AWT event model (the origin of the name)--and can be trickier to use. It is also read-only, so you cannot modify files using the eventusermodel.
code:
import java.util.*;
import java.io.*;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.util.Region;
public class ReadExcelSheet
{
public static void main(String []args)
{
ReadExcelSheet readExcel=new ReadExcelSheet();
String path="D:/TestExcel.xls"; //name of Excel file.
try
{
readExcel.readSheet(path);
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void readSheet(String path)
{
try
{
InputStream inp = new FileInputStream(path);
HSSFWorkbook wb = new HSSFWorkbook(inp);
for (int k = 0; k < wb.getNumberOfSheets(); k++)
{
HSSFSheet sheet = wb.getSheetAt(k);
int rows = sheet.getPhysicalNumberOfRows();
System.out.println("\nSheet " + k + " \""+ wb.getSheetName(k) + "\" has "+ rows + " row(s).");
for (int r = 0; r < rows; r++)
{
HSSFRow row = sheet.getRow(r);
if (row == null) {
continue;
}
int cells = row.getPhysicalNumberOfCells();
System.out.println("\nROW " + row.getRowNum()+ " has " + cells + " cell(s).");
for (int c = 0; c < cells; c++)
{
HSSFCell cell = row.getCell(c);
String value = null;
//System.out.println("cell.getCellType()= "+cell.getCellType());
switch (cell.getCellType())
{
case HSSFCell.CELL_TYPE_FORMULA :
value = "FORMULA value="+ cell.getCellFormula();
break;
case HSSFCell.CELL_TYPE_NUMERIC :
value = "NUMERIC value="+ cell.getNumericCellValue();
break;
case HSSFCell.CELL_TYPE_STRING :
value = "STRING value="+ cell.getStringCellValue();
break;
default :
}
System.out.println("CELL col="+ cell.getColumnIndex()+ " VALUE=" + value);
}
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}