Java代码15个非常有用的Jav

下面是15个非常有用的java程序片段,希望能对你有用。1.字符串有整型的相互转换

Stringa=String.valueOf(2);//integertonumericstring

inti=Integer.parseInt(a);//numericstringtoanint

复制代码

2.向文件末尾添加内容

BufferedWriterout=null;

try{

out=newBufferedWriter(newFileWriter(”filename”,true));

out.write(”aString”);

}catch(IOExceptione){

//errorprocessingcode

}finally{

if(out!=null){

out.close();

}

}

复制代码

3.得到当前方法的名字

StringmethodName=Thread.currentThread().getStackTrace()[1].getMethodName();

复制代码

4.转字符串到日期

strongjava.util.Date=java.text.DateFormat.getDateInstance().parse(dateString);/strong

复制代码

或者是:

SimpleDateFormatformat=newSimpleDateFormat(dd.MM.yyyy);

Datedate=format.parse(myString);

复制代码

5.使用JDBC链接Oracle

publicclassOracleJdbcTest

{

StringdriverClass=oracle.jdbc.driver.OracleDriver;

Connectioncon;

publicvoidinit(FileInputStreamfs)throwsClassNotFoundException,SQLException,FileNotFoundException,IOException

{

Propertiesprops=newProperties();

props.load(fs);

Stringurl=props.getProperty(db.url);

StringuserName=props.getProperty(db.user);

Stringpassword=props.getProperty(db.password);

Class.forName(driverClass);

con=DriverManager.getConnection(url,userName,password);

}

publicvoidfetch()throwsSQLException,IOException

{

PreparedStatementps=con.prepareStatement(selectSYSDATEfromdual);

ResultSetrs=ps.executeQuery();

while(rs.next())

{

//dothethingyoudo

}

rs.close();

ps.close();

}

publicstaticvoidmain(String[]args)

{

OracleJdbcTesttest=newOracleJdbcTest();

test.init();

test.fetch();

}

}

复制代码

你也可以看看:Java建立和Mysql、sqlserver和Oracle数据库的连接6.把Javautil.Date转成sql.Date

java.util.DateutilDate=newjava.util.Date();

java.sql.DatesqlDate=newjava.sql.Date(utilDate.getTime());

复制代码

7.使用NIO进行快速的文件拷贝

publicstaticvoidfileCopy(Filein,Fileout)

throwsIOException

{

FileChannelinChannel=newFileInputStream(in).getChannel();

FileChanneloutChannel=newFileOutputStream(out).getChannel();

try

{

//inChannel.transferTo(0,inChannel.size(),outChannel);//original--apparentlyhastroublecopyinglargefilesonWindows

//magicnumberforWindows,64Mb-32Kb)

intmaxCount=(64**)-(32*);

longsize=inChannel.size();

longposition=0;

while(positionsize)

{

position+=inChannel.transferTo(position,maxCount,outChannel);

}

}

finally

{

if(inChannel!=null)

{

inChannel.close();

}

if(outChannel!=null)

{

outChannel.close();

}

}

}

复制代码

8.创建图片的缩略图

privatevoidcreateThumbnail(Stringfilename,intthumbWidth,intthumbHeight,intquality,StringoutFilename)

throwsInterruptedException,FileNotFoundException,IOException

{

//loadimagefromfilename

Imageimage=Toolkit.getDefaultToolkit().getImage(filename);

MediaTrackermediaTracker=newMediaTracker(newContainer());

mediaTracker.addImage(image,0);

mediaTracker.waitForID(0);

//usethistotestforerrorsatthispoint:System.out.println(mediaTracker.isErrorAny());

//determinethumbnailsizefromWIDTHandHEIGHT

doublethumbRatio=(double)thumbWidth/(double)thumbHeight;

intimageWidth=image.getWidth(null);

intimageHeight=image.getHeight(null);

doubleimageRatio=(double)imageWidth/(double)imageHeight;

if(thumbRatioimageRatio){

thumbHeight=(int)(thumbWidth/imageRatio);

}else{

thumbWidth=(int)(thumbHeight*imageRatio);

}

//draworiginalimagetothumbnailimageobjectand

//scaleittothenewsizeon-the-fly

BufferedImagethumbImage=newBufferedImage(thumbWidth,thumbHeight,BufferedImage.TYPE_INT_RGB);

Graphics2Dgraphics2D=thumbImage.createGraphics();

graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);

graphics2D.drawImage(image,0,0,thumbWidth,thumbHeight,null);

//savethumbnailimagetooutFilename

BufferedOutputStreamout=newBufferedOutputStream(newFileOutputStream(outFilename));

JPEGImageEncoderencoder=JPEGCodec.createJPEGEncoder(out);

JPEGEncodeParamparam=encoder.getDefaultJPEGEncodeParam(thumbImage);

quality=Math.max(0,Math.min(quality,));

param.setQuality((float)quality/.0f,false);

encoder.setJPEGEncodeParam(param);

encoder.encode(thumbImage);

out.close();

}

复制代码

9.创建JSON格式的数据

请先阅读这篇文章了解一些细节,并下面这个JAR文件:json-rpc-1.0.jar(75kb)

importorg.json.JSONObject;

...

...

JSONObjectjson=newJSONObject();

json.put(city,Mumbai);

json.put(country,India);

...

Stringoutput=json.toString();

...

复制代码

10.使用iTextJAR生成PDF

阅读这篇文章了解更多细节

fontcolor=rgb(51,51,51)fontstyle=background-color:rgb(,,)fontface=宋体importjava.io.File;

importjava.io.FileOutputStream;

importjava.io.OutputStream;

importjava.util.Date;

import







































白癜风的症状图片初期
北京治疗白癜风技术



转载请注明:http://www.jiaju1314.com/zyyd/1204.html