Header Ad

Showing posts with label java code. Show all posts
Showing posts with label java code. Show all posts

Wednesday, March 18, 2009

How to zip the files using java code

The following will show you how to zip the files using java code. This program will zip the files in the folder "TEST" in C drive.

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
public class Zip {
static final int BUFFER = 2048;
public static void main (String argv[]) {
try {
BufferedInputStream origin = null;
FileOutputStream dest = new
FileOutputStream("c:/myzip.zip");
ZipOutputStream out = new ZipOutputStream(new
BufferedOutputStream(dest));
//out.setMethod(ZipOutputStream.DEFLATED);
byte data[] = new byte[BUFFER];
// get a list of files from current directory
File f = new File("c:/test");
String files[] = f.list();
for (int i=0; i(lessthan)files.length; i++) {
System.out.println("Adding: "+files[i]);
FileInputStream fi = new
FileInputStream("c:/test/"+files[i]);
origin = new
BufferedInputStream(fi, BUFFER);
ZipEntry entry = new ZipEntry(files[i]);
out.putNextEntry(entry);
int count;
while((count = origin.read(data, 0,
BUFFER)) != -1) {
out.write(data, 0, count);
}
origin.close();
}
out.close();
} catch(Exception e) {
e.printStackTrace();
}
}
}

Thursday, January 17, 2008

Reading XLS Content Using Java Code.

Reading XLS Content Using Java Code.

We can read XLS content cell by cell by using java code.For this we need jxl jar file.

The following is the simple example to read .xls file and display its content.

Following is the link to download jxl jar file
http://fisheye1.cenqua.com/viewrep/jptools/jpTools/lib




import java.io.File;
import java.io.*;
import jxl.*;
import java.util.*;
import jxl.Workbook;
import jxl.read.biff.*;


class ReadXls
{
public static void main(String[] args)
{
try
{
String filename = "C:/ExcelFileName.xls";
WorkbookSettings ws = new WorkbookSettings();
ws.setLocale(new Locale("en", "EN"));

Workbook workbook = Workbook.getWorkbook( new File(filename),ws);

Sheet s = workbook.getSheet(0);
System.out.println("Sheet Content::"+s.getName());
readDataSheet(s);
workbook.close();
}
catch (IOException e)
{
e.printStackTrace();
}
catch (BiffException e)
{
e.printStackTrace();
}
private static void readDataSheet(Sheet s)
{
// Find the labeled cell from sheet
System.out.println(lc.getString());

//gets the value of cell at specified column and row
DateCell dc = (DateCell) s.getCell(20,1);
System.out.println(dc);

}
}

Friday, August 17, 2007

Reading XML from another Site using Java code

import="com.sun.org.apache.xerces.internal.util.URI"
import="java.io.InputStream"
import="java.net.URL"
import="javax.xml.parsers.DocumentBuilderFactory"
import="javax.xml.parsers.DocumentBuilder"
import="org.w3c.dom.Document"
import="org.w3c.dom.NodeList"
import="org.w3c.dom.NamedNodeMap"
import="org.w3c.dom.Node"

URL url= new URL("www.google.com");
InputStream in=url.openStream();
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
Document document=builder.parse(in);

If you want to get retrive entire node list that can be done by

NodeList nodeList=document.getElementsByTagName("NameofTheNode");

By these all the node will come in that Node List.
If you want to retrive the attributes with in that Node,that can be done by

Node node= nodeList.item(indx);
NamedNodeMap nnm=node.getAttributes();
String href=nnm.getNamedItem("AttributeNamewithinthatNode").getNodeValue();