非常有用的Java程序片段1.字符串有整型的相互转换Java代码Stringa=String.valueOf(2);//integertonumericstringinti=Integer.parseInt(a);//numericstringtoanint2.向文件末尾添加内容Java代码BufferedWriterout=null;try{out=newBufferedWriter(newFileWriter(”filename”,true));out.write(”aString”);}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(dateString);或者是:SimpleDateFormatformat=newSimpleDateFormat("dd.MM.yyyy");Datedate=format.parse(myString);5.使用JDBC链接OracleJava代码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();}}6.把Javautil.Date转成sql.DateJava代码java.util.DateutilDate=newjava.util.Date();java.sql.DatesqlDate=newjava.sql.Date(utilDate.getTime());7.使用NIO进行快速的文件拷贝Java代码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*1024*1024)-(32*1024);longsize=inChannel.size();longposition=0;while(position