20非常有用的Java程序片段(1)
下面是20个非常有用的Java程序片段,希望能对你有用。
1. 字符串有整型的相互转换
String a = String.valueOf(2); //integer to numeric string
int i = Integer.parseInt(a); //numeric string to an int
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
publicclass OracleJdbcTest
{
String driverClass = "oracle.jdbc.driver.OracleDriver";
Connection con;
publicvoid 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);
}
publicvoid 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();
}
publicstaticvoid 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进行快速的文件拷贝
publicstaticvoid 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. 创建图片的缩略图
privatevoid createThumbnail(String filename, int thumbWidth, int thumbHeight, int quality, String outFilename)
throws InterruptedException, FileNotFoundException, IOException
{
// load p_w_picpath from filename
Image p_w_picpath = Toolkit.getDefaultToolkit().getImage(filename);
MediaTracker mediaTracker = new MediaTracker(new Container());
mediaTracker.addImage(p_w_picpath, 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 p_w_picpathWidth = p_w_picpath.getWidth(null);
int p_w_picpathHeight = p_w_picpath.getHeight(null);
double p_w_picpathRatio = (double)p_w_picpathWidth / (double)p_w_picpathHeight;
if (thumbRatio < p_w_picpathRatio) {
thumbHeight = (int)(thumbWidth / p_w_picpathRatio);
} else {
thumbWidth = (int)(thumbHeight * p_w_picpathRatio);
}
// draw original p_w_picpath to thumbnail p_w_picpath 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(p_w_picpath, 0, 0, thumbWidth, thumbHeight, null);
// save thumbnail p_w_picpath 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();
}
转载于:https://blog.51cto.com/yangxianhong/1222086
20非常有用的Java程序片段(1)相关推荐
- 20非常有用的Java程序片段(11-15)
11. HTTP 代理设置 阅读这篇 文章 了解更多细节. 1 2 3 4 5 System.getProperties().put("http.proxyHost", " ...
- 20非常有用的Java程序片段(3)
15. 创建ZIP和JAR文件 import java.util.zip.*; import java.io.*; publicclass ZipIt { publicstaticvoid main( ...
- 20个非常有用的Java程序片段
20个非常有用的Java程序片段 来源:码农网 时间:2015-03-17 10:23:28 阅读数:1057 分享到:0 [导读] 下面是20个非常有用的Java程序片段,希望能对你有用.1 ...
- 20个非常有用的Java程序片段--转
原文地址:http://geek.csdn.net/news/detail/236591 下面是20个非常有用的Java程序片段,希望能对你有用. 1. 字符串有整型的相互转换 String a = ...
- java 程序片段_20个非常有用的Java程序片段
1. 字符串有整型的相互转换 Java代码 String a = String.valueOf(2); //integer to numeric string int i = Integer.pa ...
- 应用Java程序片段动态生成表格
通过Jsp页面动态来显示数据库中的数据,在根据条件进行查询时,将调查结果显示在jsp页面中,使用java程序片段(Scriptlet)动态生成表格 在jsp文件中,可以在"<%&quo ...
- 分享非常有用的Java程序(关键代码)(七)---抓屏程序
原文:分享非常有用的Java程序(关键代码)(七)---抓屏程序 import java.awt.Dimension; import java.awt.Rectangle; import java.a ...
- 一种基于DFA算法的敏感词检测JAVA程序片段
本文章提供一种基于DFA算法的敏感词检测JAVA程序片段,如下: 1.构造多叉树数据结构 import org.jetbrains.annotations.NotNull;/*** 多叉树* @aut ...
- 应用Java程序片段动态生成下拉列表
从数据库读取数据是,需要将某些数据动态添加到下拉列表中 在下拉列表的选项中,应用的是Java表达式标记输出的数组元素,Java表达式的标记为"<%="和"%> ...
最新文章
- Redis中持久化的两种方法详解
- python能做自动化吗-Python自动化 作为代码小白,我是这样成为自动化大神的!...
- BAPI_CUSTOMERRETURN_CREATE 创建退货订单
- StandardContext
- php ftp 创建文件夹失败,phpftp上传多个文件时失败
- ​如何成为一个更好的 React 开发者?
- Ubuntu16.04LTS修改开机动画
- 40 SD配置-销售凭证设置-分配计划行类别
- Python笔记(5) 变量类型
- (11)Zynq SPI控制器介绍
- Docker多主机安装Zookeeper集群
- 【Sharepoint】 备份与清理Audit log
- 读《scikiit-learn机器学习》支持向量机
- NVM:切换node版本后无法使用npm全局包
- 【LeetCode】【字符串】题号:*299. 猜数字游戏
- 用vb程序设计输出100以内素数和
- k8s 1.18.20 kubevirt v0.47.1 创建 windows10 虚拟机
- web测试方法总结 -- 非原创,存留一份
- 计算机中丢失safeengine.dll,safeengine.dll
- 【NLP】(task4)编写BERT模型