下面是20个非常有用的Java程序片段,希望能对你有用。

1. 字符串有整型的相互转换

  1. String a = String.valueOf(2);   //integer to numeric string

  2. int i = Integer.parseInt(a); //numeric string to an int

2. 向文件末尾添加内容

  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. publicclass OracleJdbcTest

  2. {

  3. String driverClass = "oracle.jdbc.driver.OracleDriver";

  4. Connection con;

  5. publicvoid init(FileInputStream fs) throws ClassNotFoundException, SQLException, FileNotFoundException, IOException

  6. {

  7. Properties props = new Properties();

  8. props.load(fs);

  9. String url = props.getProperty("db.url");

  10. String userName = props.getProperty("db.user");

  11. String password = props.getProperty("db.password");

  12. Class.forName(driverClass);

  13. con=DriverManager.getConnection(url, userName, password);

  14. }

  15. publicvoid fetch() throws SQLException, IOException

  16. {

  17. PreparedStatement ps = con.prepareStatement("select SYSDATE from dual");

  18. ResultSet rs = ps.executeQuery();

  19. while (rs.next())

  20. {

  21. // do the thing you do

  22. }

  23. rs.close();

  24. ps.close();

  25. }

  26. publicstaticvoid main(String[] args)

  27. {

  28. OracleJdbcTest test = new OracleJdbcTest();

  29. test.init();

  30. test.fetch();

  31. }

  32. }

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());

7. 使用NIO进行快速的文件拷贝

  1. publicstaticvoid fileCopy( File in, File out )

  2. throws IOException

  3. {

  4. FileChannel inChannel = new FileInputStream( in ).getChannel();

  5. FileChannel outChannel = new FileOutputStream( out ).getChannel();

  6. try

  7. {

  8. //          inChannel.transferTo(0, inChannel.size(), outChannel);      // original -- apparently has trouble copying large files on Windows

  9. // magic number for Windows, 64Mb - 32Kb)

  10. int maxCount = (64 * 1024 * 1024) - (32 * 1024);

  11. long size = inChannel.size();

  12. long position = 0;

  13. while ( position < size )

  14. {

  15. position += inChannel.transferTo( position, maxCount, outChannel );

  16. }

  17. }

  18. finally

  19. {

  20. if ( inChannel != null )

  21. {

  22. inChannel.close();

  23. }

  24. if ( outChannel != null )

  25. {

  26. outChannel.close();

  27. }

  28. }

  29. }

8. 创建图片的缩略图

  1. privatevoid createThumbnail(String filename, int thumbWidth, int thumbHeight, int quality, String outFilename)

  2. throws InterruptedException, FileNotFoundException, IOException

  3. {

  4. // load p_w_picpath from filename

  5. Image p_w_picpath = Toolkit.getDefaultToolkit().getImage(filename);

  6. MediaTracker mediaTracker = new MediaTracker(new Container());

  7. mediaTracker.addImage(p_w_picpath, 0);

  8. mediaTracker.waitForID(0);

  9. // use this to test for errors at this point: System.out.println(mediaTracker.isErrorAny());

  10. // determine thumbnail size from WIDTH and HEIGHT

  11. double thumbRatio = (double)thumbWidth / (double)thumbHeight;

  12. int p_w_picpathWidth = p_w_picpath.getWidth(null);

  13. int p_w_picpathHeight = p_w_picpath.getHeight(null);

  14. double p_w_picpathRatio = (double)p_w_picpathWidth / (double)p_w_picpathHeight;

  15. if (thumbRatio < p_w_picpathRatio) {

  16. thumbHeight = (int)(thumbWidth / p_w_picpathRatio);

  17. } else {

  18. thumbWidth = (int)(thumbHeight * p_w_picpathRatio);

  19. }

  20. // draw original p_w_picpath to thumbnail p_w_picpath object and

  21. // scale it to the new size on-the-fly

  22. BufferedImage thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB);

  23. Graphics2D graphics2D = thumbImage.createGraphics();

  24. graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);

  25. graphics2D.drawImage(p_w_picpath, 0, 0, thumbWidth, thumbHeight, null);

  26. // save thumbnail p_w_picpath to outFilename

  27. BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(outFilename));

  28. JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);

  29. JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(thumbImage);

  30. quality = Math.max(0, Math.min(quality, 100));

  31. param.setQuality((float)quality / 100.0f, false);

  32. encoder.setJPEGEncodeParam(param);

  33. encoder.encode(thumbImage);

  34. out.close();

  35. }

转载于:https://blog.51cto.com/yangxianhong/1222086

20非常有用的Java程序片段(1)相关推荐

  1. 20非常有用的Java程序片段(11-15)

    11. HTTP 代理设置 阅读这篇 文章 了解更多细节. 1 2 3 4 5 System.getProperties().put("http.proxyHost", " ...

  2. 20非常有用的Java程序片段(3)

    15. 创建ZIP和JAR文件 import java.util.zip.*; import java.io.*; publicclass ZipIt { publicstaticvoid main( ...

  3. 20个非常有用的Java程序片段

    20个非常有用的Java程序片段 来源:码农网   时间:2015-03-17 10:23:28   阅读数:1057 分享到:0 [导读] 下面是20个非常有用的Java程序片段,希望能对你有用.1 ...

  4. 20个非常有用的Java程序片段--转

    原文地址:http://geek.csdn.net/news/detail/236591 下面是20个非常有用的Java程序片段,希望能对你有用. 1. 字符串有整型的相互转换 String a = ...

  5. java 程序片段_20个非常有用的Java程序片段

    1. 字符串有整型的相互转换 Java代码 String a = String.valueOf(2);   //integer to numeric string int i = Integer.pa ...

  6. 应用Java程序片段动态生成表格

    通过Jsp页面动态来显示数据库中的数据,在根据条件进行查询时,将调查结果显示在jsp页面中,使用java程序片段(Scriptlet)动态生成表格 在jsp文件中,可以在"<%&quo ...

  7. 分享非常有用的Java程序(关键代码)(七)---抓屏程序

    原文:分享非常有用的Java程序(关键代码)(七)---抓屏程序 import java.awt.Dimension; import java.awt.Rectangle; import java.a ...

  8. 一种基于DFA算法的敏感词检测JAVA程序片段

    本文章提供一种基于DFA算法的敏感词检测JAVA程序片段,如下: 1.构造多叉树数据结构 import org.jetbrains.annotations.NotNull;/*** 多叉树* @aut ...

  9. 应用Java程序片段动态生成下拉列表

    从数据库读取数据是,需要将某些数据动态添加到下拉列表中 在下拉列表的选项中,应用的是Java表达式标记输出的数组元素,Java表达式的标记为"<%="和"%> ...

最新文章

  1. Redis中持久化的两种方法详解
  2. python能做自动化吗-Python自动化 作为代码小白,我是这样成为自动化大神的!...
  3. BAPI_CUSTOMERRETURN_CREATE 创建退货订单
  4. StandardContext
  5. php ftp 创建文件夹失败,phpftp上传多个文件时失败
  6. ​如何成为一个更好的 React 开发者?
  7. Ubuntu16.04LTS修改开机动画
  8. 40 SD配置-销售凭证设置-分配计划行类别
  9. Python笔记(5) 变量类型
  10. (11)Zynq SPI控制器介绍
  11. Docker多主机安装Zookeeper集群
  12. 【Sharepoint】 备份与清理Audit log
  13. 读《scikiit-learn机器学习》支持向量机
  14. NVM:切换node版本后无法使用npm全局包
  15. 【LeetCode】【字符串】题号:*299. 猜数字游戏
  16. 用vb程序设计输出100以内素数和
  17. k8s 1.18.20 kubevirt v0.47.1 创建 windows10 虚拟机
  18. web测试方法总结 -- 非原创,存留一份
  19. 计算机中丢失safeengine.dll,safeengine.dll
  20. 【NLP】(task4)编写BERT模型

热门文章

  1. 分清函数指针和指针函数
  2. Unity Android DLL热更
  3. IDEA开发WebService遇到的问题和SOAPUI工具的使用
  4. html中元素盒子垂直居中的实现方法
  5. Redis 常见的性能问题和解决方法
  6. Oracle中常用的语句
  7. [原]C++头文件的包含顺序研究
  8. 8-1 数据库分库分表的几种方式
  9. do matlab的 while循环_PHP while和do while循环
  10. SSH家政服务系统设计与实现答辩PPT免费下载