Tech Neo技巧沙龙 | 11月25号,九州云/ZStack与您一路商量云时代收集界线治理实践
下面是20个异常有效的Java法度榜样片段,欲望能对你有效。
1、字符串有整型的互相转换
1.String a = String.valueOf(2); //integer to numeric string 2.int i = Integer.parseInt(a); //numeric string to an int2.向文件末尾添加内容
1.BufferedWriter out = null; 2.try { 3 out = new BufferedWriter(new FileWriter(”filename”, true)); 4 out.write(”aString”); 5} catch (IOException e) { 6 // error processing code 7} finally { 8 if (out != null) { 9 out.close(); 10 } 11}3.获得当前办法的名字
1.String methodName = Thread.currentThread().getStackTrace()[1].getMethodName();4.转字符串到日期
另一种实现
1.java.util.Date = java.text.DateFormat.getDateInstance().parse(date String);或者是:
1.SimpleDateFormat format = new SimpleDateFormat( "dd.MM.yyyy" ); 2.Date date = format.parse( myString );5.应用JDBC链接Oracle
1.public class OracleJdbcTest 2.{ 3 String driverClass = "oracle.jdbc.driver.OracleDriver"; 45 Connection con; 67 public void init(FileInputStream fs) throws ClassNotFoundException, SQLException, FileNotFoundException, IOException 8 { 9 Properties props = new Properties(); 10 props.load(fs); 11 String url = props.getProperty("db.url"); 12 String userName = props.getProperty("db.user"); 13 String password = props.getProperty("db.password"); 14 Class.forName(driverClass); 1516 con=DriverManager.getConnection(url, userName, password); 17 } 1819 public void fetch() throws SQLException, IOException 20 { 21 PreparedStatement ps = con.prepareStatement("select SYSDATE from dual"); 22 ResultSet rs = ps.executeQuery(); 2324 while (rs.next()) 25 { 26 // do the thing you do 27 } 28 rs.close(); 29 ps.close(); 30 } 3132 public static void main(String[] args) 33 { 34 OracleJdbcTest test = new OracleJdbcTest(); 35 test.init(); 36 test.fetch(); 37 } 38}6.把 Java util.Date 转成 sql.Date
1.java.util.Date utilDate = new java.util.Date(); 2.java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());1.public static void fileCopy( File in, File out ) 2. throws IOException 3. { 4. FileChannel inChannel = new FileInputStream( in ).getChannel(); 5. FileChannel outChannel = new FileOutputStream( out ).getChannel(); 6. try7. { 8.// inChannel.transferTo(0, inChannel.size(), outChannel); // original -- apparently has trouble copying large files on Windows 9.10. // magic number for Windows, 64Mb - 32Kb) 11. int maxCount = (64 * 1024 * 1024) - (32 * 1024); 12. long size = inChannel.size(); 13. long position = 0; 14. while ( position < size ) 15. { 16. position += inChannel.transferTo( position, maxCount, outChannel ); 17. } 18. } 19. finally20. { 21. if ( inChannel != null ) 22. { 23. inChannel.close(); 24. } 25. if ( outChannel != null ) 26. { 27. outChannel.close(); 28. } 29. } 30. }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, BufferedImage.TYPE_INT_RGB); Graphics2D graphics2D = thumbImage.createGraphics(); graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null); // save thumbnail image to outFilename BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(outFilename)); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(thumbImage); quality = Math.max(0, Math.min(quality, 100)); param.setQuality((float)quality / 100.0f, false); encoder.setJPEGEncodeParam(param); encoder.encode(thumbImage); out.close(); }9.创建 JSON 格局的数据
import org.json.JSONObject; ... ... JSONObject json = new JSONObject(); json.put("city", "Mumbai"); json.put("country", "India"); ... String output = json.toString(); ...
推荐阅读
Tech Neo技巧沙龙 | 11月25号,九州云/ZStack与您一路商量云时代收集界线治理实践 图1 广东生益科技信息治理部CIO周嘉林 广东生益科技股份有限公司创建于1985年,拥有员工3800多名,是>>>详细阅读
本文标题:程序员花2小时总结:20个非常有用的Java程序片段
地址:http://www.17bianji.com/lsqh/38914.html
1/2 1