20个非常有用的JAVA程序片段

20个非常有用的JAVA程序片段

ID:37837858

大小:120.00 KB

页数:12页

时间:2019-06-01

20个非常有用的JAVA程序片段_第1页
20个非常有用的JAVA程序片段_第2页
20个非常有用的JAVA程序片段_第3页
20个非常有用的JAVA程序片段_第4页
20个非常有用的JAVA程序片段_第5页
资源描述:

《20个非常有用的JAVA程序片段》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库

1、下面是20个非常有用的Java程序片段,希望能对你有用。1.字符串有整型的相互转换1.Stringa=String.valueOf(2);//integertonumericstring2.inti=Integer.parseInt(a);//numericstringtoanint2.向文件末尾添加内容1.BufferedWriterout=null;2.try{3.out=newBufferedWriter(newFileWriter(”filename”,true));4.out.write(”aString”);5.}catch(IOExce

2、ptione){6.//errorprocessingcode7.}finally{8.if(out!=null){9.out.close();10.}11.}3.得到当前方法的名字1.StringmethodName=Thread.currentThread().getStackTrace()[1].getMethodName();4.转字符串到日期1.java.util.Date=java.text.DateFormat.getDateInstance().parse(dateString);或者是:1.SimpleDateFormatforma

3、t=newSimpleDateFormat("dd.MM.yyyy");2.Datedate=format.parse(myString);5.使用JDBC链接Oracle1.publicclassOracleJdbcTest2.{3.StringdriverClass="oracle.jdbc.driver.OracleDriver";4.5.Connectioncon;6.7.publicvoidinit(FileInputStreamfs)throwsClassNotFoundException,SQLException,FileNotFoun

4、dException,IOException8.{9.Propertiesprops=newProperties();10.props.load(fs);11.Stringurl=props.getProperty("db.url");12.StringuserName=props.getProperty("db.user");13.Stringpassword=props.getProperty("db.password");14.Class.forName(driverClass);15.16.con=DriverManager.getConne

5、ction(url,userName,password);17.}18.1.publicvoidfetch()throwsSQLException,IOException2.{3.PreparedStatementps=con.prepareStatement("selectSYSDATEfromdual");4.ResultSetrs=ps.executeQuery();5.6.while(rs.next())7.{8.//dothethingyoudo9.}10.rs.close();11.ps.close();12.}13.14.publics

6、taticvoidmain(String[]args)15.{16.OracleJdbcTesttest=newOracleJdbcTest();17.test.init();18.test.fetch();19.}20.}6.把Javautil.Date转成sql.Date1.java.util.DateutilDate=newjava.util.Date();2.java.sql.DatesqlDate=newjava.sql.Date(utilDate.getTime());7.使用NIO进行快速的文件拷贝1.publicstaticvoidf

7、ileCopy(Filein,Fileout)2.throwsIOException3.{4.FileChannelinChannel=newFileInputStream(in).getChannel();5.FileChanneloutChannel=newFileOutputStream(out).getChannel();6.try7.{8.//inChannel.transferTo(0,inChannel.size(),outChannel);//original--apparentlyhastroublecopyinglargefile

8、sonWindows9.10.//magicnumberforWindows,64Mb-32Kb)11.in

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。