创建ZIP和JAR文件java的代码.docx

上传人:b****6 文档编号:7950018 上传时间:2023-01-27 格式:DOCX 页数:14 大小:18.07KB
下载 相关 举报
创建ZIP和JAR文件java的代码.docx_第1页
第1页 / 共14页
创建ZIP和JAR文件java的代码.docx_第2页
第2页 / 共14页
创建ZIP和JAR文件java的代码.docx_第3页
第3页 / 共14页
创建ZIP和JAR文件java的代码.docx_第4页
第4页 / 共14页
创建ZIP和JAR文件java的代码.docx_第5页
第5页 / 共14页
点击查看更多>>
下载资源
资源描述

创建ZIP和JAR文件java的代码.docx

《创建ZIP和JAR文件java的代码.docx》由会员分享,可在线阅读,更多相关《创建ZIP和JAR文件java的代码.docx(14页珍藏版)》请在冰豆网上搜索。

创建ZIP和JAR文件java的代码.docx

创建ZIP和JAR文件java的代码

创建ZIP和JAR文件java的代码

阅读次数:

282次发布时间:

2010-08-0117:

48:

28发布人:

网络转载

来源:

网络转载

 importjava.util.zip.*;  import

java.io.*;  publicclassZipIt{  

publicstaticvoidmain(Stringargs[])throwsIOException

{  if(args.length<

2){  

System.err.println("usage:

javaZipItZip.zipfile1file2file3");  

System.exit

(-1);  }  

FilezipFile=newFile(args

[0]);  if

(zipFile.exists()){  

System.err.println("Zipfilealreadyexists,pleasetryanother");  

System.exit

(-2);  }  

FileOutputStreamfos=new

FileOutputStream(zipFile);  

ZipOutputStreamzos=new

ZipOutputStream(fos);  intbytesRead;  byte[]

buffer=newbyte[1024];  CRC32crc=newCRC32();  for(inti=1,n=args.length;i

Stringname

=args[i];  

Filefile=

newFile(name);  

if(!

file.exists()){  

&

nbsp; System.err.println("Skipping:

"+name);  

&

nbsp; continue;  

}

  BufferedInputStreambis=newBufferedInputStream(  

&

nbsp; newFileInputStream(file));  

crc.reset

();  

while

((bytesRead=bis.read(buffer))!

=-1){  

&

nbsp; crc.update(buffer,0,bytesRead);  

}

  bis.close();  

//Resetto

beginningofinputstream  

bis=new

BufferedInputStream(  

&

nbsp; newFileInputStream(file));  

ZipEntry

entry=newZipEntry(name);  

entry.setMethod(ZipEntry.STORED);  

entry.setCompressedSize(file.length());  

entry.setSize(file.length());  

entry.setCrc(crc.getValue());  

zos.putNextEntry(entry);  

while

((bytesRead=bis.read(buffer))!

=-1){  

&

nbsp; zos.write(buffer,0,bytesRead);  

}

  bis.close();  }

  zos.close

();  }  } 

importjava.util.zip.*;importjava.io.*;

publicclassZipIt{publicstaticvoidmain(Stringargs

[])throwsIOException{if

(args.length<2)

{System.err.println("usage:

javaZipItZip.zipfile1file2

file3");System.exit(-1);}

FilezipFile=newFile(args

[0]);if(zipFile.exists())

{System.err.println("Zipfilealreadyexists,pleasetry

another");System.exit(-2);}

FileOutputStreamfos=new

FileOutputStream(zipFile);ZipOutputStreamzos=newZipOutputStream

(fos);int

bytesRead;byte[]buffer=newbyte

[1024];CRC32crc=newCRC32

();for(inti=1,n=args.length;i

{Stringname

=args[i];Filefile=newFile

(name);if

(!

file.exists())

{  System.err.println("Skipping:

"+

name);&

nbsp;  

continue;}

BufferedInputStreambis=newBufferedInputStream

(  newFileInputStream

(file));crc.reset();while((bytesRead=bis.read(buffer))!

=-1)

{  crc.update(buffer,0,

bytesRead);}bis.close

();//Reset

tobeginningofinput

streambis=

newBufferedInputStream

(  newFileInputStream

(file));ZipEntryentry=newZipEntry

(name);entry.setMethod

(ZipEntry.STORED);&

nbsp;entry.setCompressedSize(file.length

());entry.setSize(file.length

());entry.setCrc(crc.getValue

());zos.putNextEntry

(entry);while((bytesRead=bis.read(buffer))!

=-1)

{  zos.write(buffer,0,

bytesRead);}bis.close

();}

zos.close();}}16.Parsing/ReadingXMLfileinJavaSampleXMLfile.

   

John 

12 

   

Mary 

11 

   

Simon 

18 

   

JohnB12MaryA<

AGE>11SimonA18

JavacodetoparseaboveXML.

packagenet.viralpatel.java.xmlparser;  importjava.io.File;  import

javax.xml.parsers.DocumentBuilder;  import

javax.xml.parsers.DocumentBuilderFactory;  import

org.w3c.dom.Document;  importorg.w3c.dom.Element;  

importorg.w3c.dom.Node;  import

org.w3c.dom.NodeList;  publicclassXMLParser

{  publicvoidgetAllUserNames(String

fileName){  try

{  

DocumentBuilderFactorydbf=DocumentBuilderFactory.newInstance();  

DocumentBuilderdb=dbf.newDocumentBuilder();  

Filefile=

newFile(fileName);  

if

(file.exists()){  

&

nbsp; Documentdoc=db.parse(file);  

&

nbsp; ElementdocEle=doc.getDocumentElement();  &

nbsp; //Printrootelementofthedocument  

&

nbsp; System.out.println("Rootelementofthedocument:

&

nbsp;         +docEle.getNodeName

());  &

nbsp; NodeListstudentList=docEle.getElementsByTagName

("student");  &

nbsp; //Printtotalstudentelementsindocument  

&

nbsp; System.out  

&

nbsp;         .println("Total

students:

"+studentList.getLength());  &

nbsp; if(studentList!

=null&&studentList.getLength()>0)

{  

&

nbsp;     for(inti=0;i

i++){  &

nbsp;         Nodenode=

studentList.item(i);  &

nbsp;         if(node.getNodeType()

==Node.ELEMENT_NODE){  &

nbsp;             

System.out  

&

nbsp;            

        .println

("=====================");  &

nbsp;             

Elemente=(Element)node;  

&

nbsp;             

NodeListnodeList=e.getElementsByTagName("name");  

&

nbsp;             

System.out.println("Name:

&

nbsp;            

        +nodeList.item(0).getChildNodes

().item(0)  

&

nbsp;            

             

;  .getNodeValue());  &

nbsp;             

nodeList=e.getElementsByTagName("grade");  

&

nbsp;             

System.out.println("Grade:

&

nbsp;            

        +nodeList.item(0).getChildNodes

().item(0)  

&

nbsp;            

             

;  .getNodeValue());  &

nbsp;             

nodeList=e.getElementsByTagName("age");  

&

nbsp;             

System.out.println("Age:

&

nbsp;            

        +nodeList.item(0).getChildNodes

().item(0)  

&

nbsp;            

             

;  .getNodeValue());  

&

nbsp;         }  

&

nbsp;     }  

&

nbsp; }else{  

&

nbsp;     System.exit

(1);  

&

nbsp; }  

}

  }catch(Exceptione)

{  

System.out.println(e);  }  }  public

staticvoidmain(String[]args){  XMLParserparser=newXMLParser

();  parser.getAllUserNames("c:

\\test.xml");  }

  } 

packagenet.viralpatel.java.xmlparser;

importjava.io.File;importjavax.xml.parsers.DocumentBuilder;import

javax.xml.parsers.DocumentBuilderFactory;

importorg.w3c.dom.Document;importorg.w3c.dom.Element;import

org.w3c.dom.Node;importorg.w3c.dom.NodeList;

publicclassXMLParser{

publicvoidgetAllUserNames(StringfileName){try

{DocumentBuilderFactorydbf=

DocumentBuilderFactory.newInstance();DocumentBuilderdb=

dbf.newDocumentBuilder();Filefile=newFile

(fileName);if(file.exists())

{Documentdoc=db.parse

(file);ElementdocEle=doc.getDocumentElement();

//Printrootelementofthe

documentSystem.out.println("Rootelementofthe

document:

"+docEle.getNodeName());

NodeListstudentList=docEle.getElementsByTagName

("student");

//Printtotalstudentelementsin

documentSystem.out&nb

sp;.println("Totalstudents:

"+

studentList.getLength());

if(studentList!

=null&&

studentList.getLength()

>0){for(inti=0;i<

studentList.getLength

();i++){

Nodenode=studentList.item(i);

if(node.getNodeType()==

Node.ELEMENT_NODE){

System.out    .println

("=====================");

Elemente=(Element)

node;NodeListnodeList=

e.getElementsByTagName

("name");System.out.println("Name:

"+

nodeList.item(0).getChildNodes().item(0)

.getNodeValue());

nodeList=

e.getElementsByTagName

("grade");System.out.println("Grade:

"+

nodeList.item(0).getChildNodes().item(0)

.getNodeValue());

nodeList=

e.getElementsByTagName

("age");System.out.println("Age:

"+

nodeList.item(0).getChildNodes().item(0)

.getNodeValue());}

}}else

{System.exit

(1);}

}}catch(Exceptione)

{System.out.println(e);}}

publicstaticvoidmain(String[]args){

XMLParserparser=newXMLParser

();parser.getAllUserNames("c:

\\test.xml");}}17.

ConvertArraytoMapinJava

importjava.util.Map;  import

mons.lang.ArrayUtils;  publicclassMain

{  publicstaticvoidmain(String[]args)

{  String[][]countries={{"UnitedStates",

"NewYork"},{"UnitedKingdom",

"London"},  {

"Netherland","Amsterdam"},{"Japan","Tokyo"},{"France","Paris"}

};  MapcountryCapitals=

ArrayUtils.toMap(countries);  System.out.println("CapitalofJapanis"+countryCapitals.get

("Japan"));  System.out.println("Capitalof

Franceis"+countryCapitals.get("France"));  }  

importjava.util.Map;importmons.lang.ArrayUtils;

publicclassMain{

publicstaticvoidmain(String[]args){String[][]

countries={{"UnitedStates","NewYork"},{"UnitedKingdom",

"London"},{"Netherland",

"Amsterdam"},{"Japan","Tokyo"},{"France","Paris"}

};

MapcountryCapitals=ArrayUtils.toMap(countries);

System.out.println("CapitalofJapanis"+

countryCapitals.get("Japan"));System.out.println("Capitalof

Franceis"+countryCapitals.get("France"));}}18.SendEmail

usingJava

importjavax.mail.*;  import

javax.mail.internet.*;  importjava.util.*;  publicvoidpostMail(Stringrecipients[],Stringsubject,Stringmessage,

String

from)throwsMessagingException  {  

booleandebug=false;  //Setthehostsmtpaddress  

Propertiesprops=newProperties();  

props.put("mail.smtp.host",

"");  //createsome

propertiesandgetthedefaultSession  Session

session=Session.getDefaultInstance(props,null);  

session.setDebug(debug);  //createamessage  Messagemsg=newMimeMessage(session);  //setthefromandtoaddress  

InternetAddressaddressFrom=newInternetAddress

(from);  msg.setFrom(addressFrom);  

InternetAddress[]addressTo=new

InternetAddress[recipients.length];  for(inti=

0;i

addressTo[i]=newInternetAddress

(recipients[i]);  }  

msg.setRecipients(Message.RecipientType.TO,

addressTo);  //Optional:

Youcan

alsosetyourcustomheadersintheEmailifyouWant  

msg.addHeader("MyHeaderName","myHeaderValue");  

//SettingtheSubjectandContent

Type  msg.setSubject(s

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 解决方案 > 学习计划

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1