java常用代码有:1、 字符串有整型的相互转换;2、向文件末尾添加内容;3、得到当前方法的名字 ;4、转字符串到日期;5、使用jdbc链接oracle;6、使用nio进行快速的文件拷贝。
java常用代码有:
1. 字符串有整型的相互转换
string a = string.valueof(2); //integer to numeric string int i = integer.parseint(a); //numeric string to an int相关学习推荐:java基础教程
2. 向文件末尾添加内容
bufferedwriter out = null; try { out = new bufferedwriter(new filewriter(”filename”, true)); out.write(”astring”); } catch (ioexception e) { // error processing code } finally { if (out != null) { out.close(); } }3. 得到当前方法的名字
string methodname = thread.currentthread().getstacktrace()[1].getmethodname();4. 转字符串到日期
java.util.date = java.text.dateformat.getdateinstance().parse(date string);//或者是:simpledateformat format = new simpledateformat( "dd.mm.yyyy" ); date date = format.parse( mystring );5. 使用jdbc链接oracle
public class oraclejdbctest { string driverclass = "oracle.jdbc.driver.oracledriver"; connection con; public void init(fileinputstream fs) throws classnotfoundexception, sqlexception, filenotfoundexception, ioexception { properties props = new properties(); props.load(fs); string url = props.getproperty("db.url"); string username = props.getproperty("db.user"); string password = props.getproperty("db.password"); class.forname(driverclass); con=drivermanager.getconnection(url, username, password); } public void fetch() throws sqlexception, ioexception { preparedstatement ps = con.preparestatement("select sysdate from dual"); resultset rs = ps.executequery(); while (rs.next()) { // do the thing you do } rs.close(); ps.close(); } public static void main(string[] args) { oraclejdbctest test = new oraclejdbctest(); test.init(); test.fetch(); } }6. 把 java util.date 转成 sql.date
java.util.date utildate = new java.util.date(); java.sql.date sqldate = new java.sql.date(utildate.gettime());7. 使用nio进行快速的文件拷贝
public static void filecopy( file in, file out ) throws ioexception { filechannel inchannel = new fileinputstream( in ).getchannel(); filechannel outchannel = new fileoutputstream( out ).getchannel(); try { // inchannel.transferto(0, inchannel.size(), outchannel); // original -- apparently has trouble copying large files on windows // magic number for windows, 64mb - 32kb) int maxcount = (64 * 1024 * 1024) - (32 * 1024); long size = inchannel.size(); long position = 0; while ( position < size ) { position = inchannel.transferto( position, maxcount, outchannel ); } } finally { if ( inchannel != null ) { inchannel.close(); } if ( outchannel != null ) { outchannel.close(); } } }8. 创建图片的缩略图
private void createthumbnail(string filename, int thumbwidth, int thumbheight, int quality, string outfilename) throws interruptedexception, filenotfoundexception, ioexception { // load image from filename image image = toolkit.getdefaulttoolkit().getimage(filename); mediatracker mediatracker = new mediatracker(new container()); mediatracker.addimage(image, 0); mediatracker.waitforid(0); // use this to test for errors at this point: system.out.println(mediatracker.iserrorany()); // determine thumbnail size from width and height double thumbratio = (double)thumbwidth / (double)thumbheight; int imagewidth = image.getwidth(null); int imageheight = image.getheight(null); double imageratio = (double)imagewidth / (double)imageheight; if (thumbratio < imageratio) { thumbheight = (int)(thumbwidth / imageratio); } else { thumbwidth = (int)(thumbheight * imageratio); } // draw original image to thumbnail image object and // scale it to the new size on-the-fly bufferedimage thumbimage = new bufferedimage(thumbwidth, thumbheight, bufferedimag
选购云主机服务器价格实惠ddos防护方法ofo小黄车是如何打造情感化设计的?来看总监的自述!网站突然打不开了没动过-虚拟主机/数据库问题如何将表格布满页面_word文档表格布满页面的方法图文步骤云服务器租用常见的负载均衡分类服务器托管安全吗3月阿里云开年采购云服务器秒杀