这20各个代码段是我在之前学习Java时,在某个网站看到的,当时直接剪到了
有道云笔记中,好久不写Java,最近整理笔记,觉得这些小知识作为Reference还是很方便的,所以搬到自己的博客中,向原作者表示感谢!之后在Reference中会随时记录有用的代码片段,以提高开发效率。
下面是20个非常有用的Java程序片段,希望能对你有用。
本篇记录1-10
1 . 字符串有整型的相互转换
1 | String a = String.valueOf(2); //integer to numeric string int i = Integer.parseInt(a); //numeric string to an int |
2 . 向文件末尾添加内容
1 | BufferedWriter out = null; |
3 . 得到当前方法的名字
1 | String methodName = Thread.currentThread().getStackTrace()[1].getMethodName(); |
4 . 转字符串到日期
1 | java.util.Date = java.text.DateFormat.getDateInstance().parse(date String); |
5 . 使用JDBC链接Oracle
1 | public class OracleJdbcTest { |
6 . 把 Java util.Date 转成 sql.Date
1 | java.util.Date utilDate = new java.util.Date(); |
7 . 使用NIO进行快速的文件拷贝
1 | public static void fileCopy( File in, File out )throws IOException{ |
8 . 创建图片的缩略图
1 | private void createThumbnail(String filename, int thumbWidth, int thumbHeight, int quality, String outFilename) |
9 . 创建 JSON 格式的数据
请先阅读这篇文章 了解一些细节,并下面这个JAR 文件:json-rpc-1.0.jar (75 kb)
1 | import org.json.JSONObject; |
10 . 使用iText JAR生成PDFd
阅读这篇文章 了解更多细节
1 | import java.io.File; |