字符串有整型的相互转换
Stringa=StringvalueOf();//integertonumericstring
inti=IntegerparseInt(a);//numericstringtoanint
向文件末尾添加内容
BufferedWriterout=null;
try{
out=newBufferedWriter(newFileWriter(”filename”true));
outwrite(”aString”);
}catch(IOExceptione){
//errorprocessingcode
}finally{
if(out!=null){
outclose();
}
}
得到当前方法的名字
StringmethodName=ThreadcurrentThread()getStackTrace()[]getMethodName();
转字符串到日期
javautilDate=javatextDateFormatgetDateInstance()parse(dateString);
或者是
SimpleDateFormatformat=newSimpleDateFormat("ddMMyyyy");
Datedate=formatparse(myString);
使用JDBC链接Oracle
publicclassOracleJdbcTest
{
StringdriverClass="oraclejdbcdriverOracleDriver";
Connectioncon;
publicvoidinit(FileInputStreamfs)throwsClassNotFoundExceptionSQLExceptionFileNotFoundExceptionIOException
{
Propertiesprops=newProperties();
propsload(fs);
Stringurl=propsgetProperty("dburl");
StringuserName=propsgetProperty("dbuser");
Stringpassword=propsgetProperty("dbpassword");
ClassforName(driverClass);
con=DriverManagergetConnection(urluserNamepassword);
}
publicvoidfetch()throwsSQLExceptionIOException
{
PreparedStatementps=conprepareStatement("selectSYSDATEfromdual");
ResultSetrs=psexecuteQuery();
while(rsnext())
{
//dothethingyoudo
}
rsclose();
psclose();
}
publicstaticvoidmain(String[]args)
{
OracleJdbcTesttest=newOracleJdbcTest();
testinit();
testfetch();
}
}
把JavautilDate转成sqlDate
javautilDateutilDate=newjavautilDate();
javasqlDatesqlDate=newjavasqlDate(utilDategetTime());
使用NIO进行快速的文件拷贝
publicstaticvoidfileCopy(FileinFileout)
throwsIOException
{
FileChannelinChannel=newFileInputStream(in)getChannel();
FileChanneloutChannel=newFileOutputStream(out)getChannel();
try
{
//inChanneltransferTo(inChannelsize()outChannel);//originalapparentlyhastroublecopyinglargefilesonWindows
//magicnumberforWindowsMbKb)
intmaxCount=(**)(*);
longsize=inChannelsize();
longposition=;
while(position<size)
{
position+=inChanneltransferTo(positionmaxCountoutChannel);
}
}
finally
{
if(inChannel!=null)
{
inChannelclose();
}
if(outChannel!=null)
{
outChannelclose();
}
}
}
创建图片的缩略图
privatevoidcreateThumbnail(StringfilenameintthumbWidthintthumbHeightintqualityStringoutFilename)
throwsInterruptedExceptionFileNotFoundExceptionIOException
{
//loadimagefromfilename
Imageimage=ToolkitgetDefaultToolkit()getImage(filename);
MediaTrackermediaTracker=newMediaTracker(newContainer());
mediaTrackeraddImage(image);
mediaTrackerwaitForID();
//usethistotestforerrorsatthispoint:Systemoutprintln(mediaTrackerisErrorAny());
//determinethumbnailsizefromWIDTHandHEIGHT
doublethumbRatio=(double)thumbWidth/(double)thumbHeight;
intimageWidth=imagegetWidth(null);
intimageHeight=imagegetHeight(null);
doubleimageRatio=(double)imageWidth/(double)imageHeight;
if(thumbRatio<imageRatio){
thumbHeight=(int)(thumbWidth/imageRatio);
}else{
thumbWidth=(int)(thumbHeight*imageRatio);
}
//draworiginalimagetothumbnailimageobjectand
//scaleittothenewsizeonthefly
BufferedImagethumbImage=newBufferedImage(thumbWidththumbHeightBufferedImageTYPE_INT_RGB);
GraphicsDgraphicsD=thumbImagecreateGraphics();
graphicsDsetRenderingHint(RenderingHintsKEY_INTERPOLATIONRenderingHintsVALUE_INTERPOLATION_BILINEAR);
graphicsDdrawImage(imagethumbWidththumbHeightnull);
//savethumbnailimagetooutFilename
BufferedOutputStreamout=newBufferedOutputStream(newFileOutputStream(outFilename));
JPEGImageEncoderencoder=JPEGCodeccreateJPEGEncoder(out);
JPEGEncodeParamparam=encodergetDefaultJPEGEncodeParam(thumbImage);
quality=Mathmax(Mathmin(quality));
paramsetQuality((float)quality/ffalse);
encodersetJPEGEncodeParam(param);
encoderencode(thumbImage);
outclose();
}
创建JSON格式的数据
importorgjsonJSONObject;
JSONObjectjson=newJSONObject();
jsonput("city""Mumbai");
jsonput("country""India");
Stringoutput=jsontoString();
使用iTextJAR生成PDF
importjavaioFile;
importjavaioFileOutputStream;
importjavaioOutputStream;
importjavautilDate;
importcomlowagietextDocument;
importcomlowagietextParagraph;
importcomlowagietextpdfPdfWriter;
publicclassGeneratePDF{
publicstaticvoidmain(String[]args){
try{
OutputStreamfile=newFileOutputStream(newFile("C:Testpdf"));
Documentdocument=newDocument();
PdfWritergetInstance(documentfile);
documentopen();
documentadd(newParagraph("HelloKiran"));
documentadd(newParagraph(newDate()toString()));
documentclose();
fileclose();
}catch(Exceptione){
eprintStackTrace();
}
}
}
HTTP代理设置
SystemgetProperties()put(""someProxyURL");
SystemgetProperties()put(""someProxyPort");
SystemgetProperties()put(""someUserName");
SystemgetProperties()put(""somePassword");
单实例Singleton示例
publicclassSimpleSingleton{
privatestaticSimpleSingletonsingleInstance=newSimpleSingleton();
//Markingdefaultconstructorprivate
//toavoiddirectinstantiation
privateSimpleSingleton(){
}
//GetinstanceforclassSimpleSingleton
publicstaticSimpleSingletongetInstance(){
returnsingleInstance;
}
}
另一种实现
publicenumSimpleSingleton{
INSTANCE;
publicvoiddoSomething(){
}
}
//CallthemethodfromSingleton:
SimpleSingletonINSTANCEdoSomething();
抓屏程序
importjavaawtDimension;
importjavaawtRectangle;
importjavaawtRobot;
importjavaawtToolkit;
importjavaawtimageBufferedImage;
importjavaximageioImageIO;
importjavaioFile;
publicvoidcaptureScreen(StringfileName)throwsException{
DimensionscreenSize=ToolkitgetDefaultToolkit()getScreenSize();
RectanglescreenRectangle=newRectangle(screenSize);
Robotrobot=newRobot();
BufferedImageimage=robotcreateScreenCapture(screenRectangle);
ImageIOwrite(image"png"newFile(fileName));
}