非常有用的java程序片段

非常有用的java程序片段

ID:6054094

大小:72.00 KB

页数:22页

时间:2018-01-01

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

《非常有用的java程序片段》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库

1、非常有用的Java程序片段1.字符串有整型的相互转换  Java代码  Stringa=String.valueOf(2);  //integertonumericstring  inti=Integer.parseInt(a);//numericstringtoanint  2.向文件末尾添加内容  Java代码  BufferedWriterout=null;  try{  out=newBufferedWriter(newFileWriter(”filename”,true));  out.write(”aString”);  }

2、catch(IOExceptione){  //errorprocessingcode  }finally{  if(out!=null){  out.close();  }  }  3.得到当前方法的名字  Java代码  StringmethodName=Thread.currentThread().getStackTrace()[1].getMethodName();  4.转字符串到日期  Java代码  java.util.Date=java.text.DateFormat.getDateInstance().parse(da

3、teString);  或者是:  SimpleDateFormatformat=newSimpleDateFormat("dd.MM.yyyy");  Datedate=format.parse(myString);  5.使用JDBC链接Oracle  Java代码  publicclassOracleJdbcTest  {  StringdriverClass="oracle.jdbc.driver.OracleDriver";  Connectioncon;  publicvoidinit(FileInputStreamfs)t

4、hrowsClassNotFoundException,SQLException,FileNotFoundException,IOException  {  Propertiesprops=newProperties();  props.load(fs);  Stringurl=props.getProperty("db.url");  StringuserName=props.getProperty("db.user");  Stringpassword=props.getProperty("db.password");  Class

5、.forName(driverClass);  con=DriverManager.getConnection(url,userName,password);  }  publicvoidfetch()throwsSQLException,IOException  {  PreparedStatementps=con.prepareStatement("selectSYSDATEfromdual");  ResultSetrs=ps.executeQuery();  while(rs.next())  {  //dothethingyo

6、udo  }  rs.close();  ps.close();  }  publicstaticvoidmain(String[]args)  {  OracleJdbcTesttest=newOracleJdbcTest();  test.init();  test.fetch();  }  }6.把Javautil.Date转成sql.Date  Java代码  java.util.DateutilDate=newjava.util.Date();  java.sql.DatesqlDate=newjava.sql.Date(ut

7、ilDate.getTime());  7.使用NIO进行快速的文件拷贝  Java代码  publicstaticvoidfileCopy(Filein,Fileout)  throwsIOException  {  FileChannelinChannel=newFileInputStream(in).getChannel();  FileChanneloutChannel=newFileOutputStream(out).getChannel();  try  {  //         inChannel.transferTo(

8、0,inChannel.size(),outChannel);     //original--apparentlyhastroublecopyinglargefilesonWindows  //magic

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

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

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