- 浏览: 194490 次
- 性别:
- 来自: 上海
文章分类
最新评论
-
hao3721:
dsfasd
ehcache 使用 -
nihaokid:
方法是不会存在在对象内存中的,它存在于方法区,建议看看jvm的 ...
Java 深层理解 父类引用指向子类对象 -
vissalan:
有一点没看明白Father f1 = (Father)s;这时 ...
Java 深层理解 父类引用指向子类对象 -
咖啡舞者:
非常感谢这种分享精神.
在BREW中实现自己的GUI(8)-IWEB的封装 -
咖啡舞者:
这是创建的代码。
在设备上调的。
界面在手机和模拟器上显示的差异
下面是20个非常有用的Java程序片段,希望能对你有用。
1. 字符串有整型的相互转换
view source
<embed id="highlighter_685098_clipboard" title="copy to clipboard" type="application/x-shockwave-flash" width="16" height="16" src="http://cocre.com/wp-content/plugins/syntaxhighlighter/syntaxhighlighter/scripts/clipboard.swf" lk_media="yes" lk_mediaid="lk_juiceapp_mediaPopup_1243315888969" allowscriptaccess="always" wmode="transparent" flashvars="highlighterId=highlighter_685098" menu="false"></embed>
print
?
1.
2.
String a = String.valueOf(
2
);
//integer to numeric string
3.
int
i = Integer.parseInt(a);
//numeric string to an int
2. 向文件末尾添加内容
view source
<embed id="highlighter_886302_clipboard" title="copy to clipboard" type="application/x-shockwave-flash" width="16" height="16" src="http://cocre.com/wp-content/plugins/syntaxhighlighter/syntaxhighlighter/scripts/clipboard.swf" lk_media="yes" lk_mediaid="lk_juiceapp_mediaPopup_1243315888976" allowscriptaccess="always" wmode="transparent" flashvars="highlighterId=highlighter_886302" menu="false"></embed>
print
?
01.
02.
BufferedWriter out =
null
;
03.
try
{
04.
out =
new
BufferedWriter(
new
FileWriter(”filename”,
true
));
05.
out.write(”aString”);
06.
}
catch
(IOException e) {
07.
// error processing code
08.
}
finally
{
09.
if
(out !=
null
) {
10.
out.close();
11.
}
12.
}
3. 得到当前方法的名字
view source
<embed id="highlighter_90532_clipboard" title="copy to clipboard" type="application/x-shockwave-flash" width="16" height="16" src="http://cocre.com/wp-content/plugins/syntaxhighlighter/syntaxhighlighter/scripts/clipboard.swf" lk_media="yes" lk_mediaid="lk_juiceapp_mediaPopup_1243315888980" allowscriptaccess="always" wmode="transparent" flashvars="highlighterId=highlighter_90532" menu="false"></embed>
print
?
1.
String methodName = Thread.currentThread().getStackTrace()[
1
].getMethodName();
4. 转字符串到日期
view source
<embed id="highlighter_366714_clipboard" title="copy to clipboard" type="application/x-shockwave-flash" width="16" height="16" src="http://cocre.com/wp-content/plugins/syntaxhighlighter/syntaxhighlighter/scripts/clipboard.swf" lk_media="yes" lk_mediaid="lk_juiceapp_mediaPopup_1243315888984" allowscriptaccess="always" wmode="transparent" flashvars="highlighterId=highlighter_366714" menu="false"></embed>
print
?
1.
2.
java.util.Date = java.text.DateFormat.getDateInstance().parse(date String);
或者是:
view source
<embed id="highlighter_544817_clipboard" title="copy to clipboard" type="application/x-shockwave-flash" width="16" height="16" src="http://cocre.com/wp-content/plugins/syntaxhighlighter/syntaxhighlighter/scripts/clipboard.swf" lk_media="yes" lk_mediaid="lk_juiceapp_mediaPopup_1243315888988" allowscriptaccess="always" wmode="transparent" flashvars="highlighterId=highlighter_544817" menu="false"></embed>
print
?
1.
2.
SimpleDateFormat format =
new
SimpleDateFormat(
"dd.MM.yyyy"
);
3.
Date date = format.parse( myString );
5. 使用JDBC链接Oracle
view source
<embed id="highlighter_694591_clipboard" title="copy to clipboard" type="application/x-shockwave-flash" width="16" height="16" src="http://cocre.com/wp-content/plugins/syntaxhighlighter/syntaxhighlighter/scripts/clipboard.swf" lk_media="yes" lk_mediaid="lk_juiceapp_mediaPopup_1243315888991" allowscriptaccess="always" wmode="transparent" flashvars="highlighterId=highlighter_694591" menu="false"></embed>
print
?
01.
public
class
OracleJdbcTest
02.
{
03.
String driverClass =
"oracle.jdbc.driver.OracleDriver"
;
04.
05.
Connection con;
06.
07.
public
void
init(FileInputStream fs)
throws
ClassNotFoundException, SQLException, FileNotFoundException, IOException
08.
{
09.
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);
15.
16.
con=DriverManager.getConnection(url, userName, password);
17.
}
18.
19.
public
void
fetch()
throws
SQLException, IOException
20.
{
21.
PreparedStatement ps = con.prepareStatement(
"select SYSDATE from dual"
);
22.
ResultSet rs = ps.executeQuery();
23.
24.
while
(rs.next())
25.
{
26.
// do the thing you do
27.
}
28.
rs.close();
29.
ps.close();
30.
}
31.
32.
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
view source
<embed id="highlighter_65942_clipboard" title="copy to clipboard" type="application/x-shockwave-flash" width="16" height="16" src="http://cocre.com/wp-content/plugins/syntaxhighlighter/syntaxhighlighter/scripts/clipboard.swf" lk_media="yes" lk_mediaid="lk_juiceapp_mediaPopup_1243315888995" allowscriptaccess="always" wmode="transparent" flashvars="highlighterId=highlighter_65942" menu="false"></embed>
print
?
1.
java.util.Date utilDate =
new
java.util.Date();
2.
java.sql.Date sqlDate =
new
java.sql.Date(utilDate.getTime());
7. 使用NIO进行快速的文件拷贝
view source
<embed id="highlighter_484357_clipboard" title="copy to clipboard" type="application/x-shockwave-flash" width="16" height="16" src="http://cocre.com/wp-content/plugins/syntaxhighlighter/syntaxhighlighter/scripts/clipboard.swf" lk_media="yes" lk_mediaid="lk_juiceapp_mediaPopup_1243315888999" allowscriptaccess="always" wmode="transparent" flashvars="highlighterId=highlighter_484357" menu="false"></embed>
print
?
01.
public
static
void
fileCopy( File in, File out )
02.
throws
IOException
03.
{
04.
FileChannel inChannel =
new
FileInputStream( in ).getChannel();
05.
FileChannel outChannel =
new
FileOutputStream( out ).getChannel();
06.
try
07.
{
08.
//
inChannel.transferTo(0, inChannel.size(), outChannel); // original
-- apparently has trouble copying large files on Windows
09.
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.
finally
20.
{
21.
if
( inChannel !=
null
)
22.
{
23.
inChannel.close();
24.
}
25.
if
( outChannel !=
null
)
26.
{
27.
outChannel.close();
28.
}
29.
}
30.
}
8. 创建图片的缩略图
view source
<embed id="highlighter_236268_clipboard" title="copy to clipboard" type="application/x-shockwave-flash" width="16" height="16" src="http://cocre.com/wp-content/plugins/syntaxhighlighter/syntaxhighlighter/scripts/clipboard.swf" lk_media="yes" lk_mediaid="lk_juiceapp_mediaPopup_1243315889003" allowscriptaccess="always" wmode="transparent" flashvars="highlighterId=highlighter_236268" menu="false"></embed>
print
?
01.
private
void
createThumbnail(String filename,
int
thumbWidth,
int
thumbHeight,
int
quality, String outFilename)
02.
throws
InterruptedException, FileNotFoundException, IOException
03.
{
04.
// load image from filename
05.
Image image = Toolkit.getDefaultToolkit().getImage(filename);
06.
MediaTracker mediaTracker =
new
MediaTracker(
new
Container());
07.
mediaTracker.addImage(image,
0
);
08.
mediaTracker.waitForID(
0
);
09.
// use this to test for errors at this point: System.out.println(mediaTracker.isErrorAny());
10.
11.
// determine thumbnail size from WIDTH and HEIGHT
12.
double
thumbRatio = (
double
)thumbWidth / (
double
)thumbHeight;
13.
int
imageWidth = image.getWidth(
null
);
14.
int
imageHeight = image.getHeight(
null
);
15.
double
imageRatio = (
double
)imageWidth / (
double
)imageHeight;
16.
if
(thumbRatio < imageRatio) {
17.
thumbHeight = (
int
)(thumbWidth / imageRatio);
18.
}
else
{
19.
thumbWidth = (
int
)(thumbHeight * imageRatio);
20.
}
21.
22.
// draw original image to thumbnail image object and
23.
// scale it to the new size on-the-fly
24.
BufferedImage thumbImage =
new
BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB);
25.
Graphics2D graphics2D = thumbImage.createGraphics();
26.
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
27.
graphics2D.drawImage(image,
0
,
0
, thumbWidth, thumbHeight,
null
);
28.
29.
// save thumbnail image to outFilename
30.
BufferedOutputStream out =
new
BufferedOutputStream(
new
FileOutputStream(outFilename));
31.
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
32.
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(thumbImage);
33.
quality = Math.max(
0
, Math.min(quality,
100
));
34.
param.setQuality((
float
)quality /
100
.0f,
false
);
35.
encoder.setJPEGEncodeParam(param);
36.
encoder.encode(thumbImage);
37.
out.close();
38.
}
9. 创建 JSON 格式的数据
请先阅读这篇文章
了解一些细节,
并下面这个JAR 文件:json-rpc-1.0.jar (75 kb)
view source
<embed id="highlighter_808034_clipboard" title="copy to clipboard" type="application/x-shockwave-flash" width="16" height="16" src="http://cocre.com/wp-content/plugins/syntaxhighlighter/syntaxhighlighter/scripts/clipboard.swf" lk_media="yes" lk_mediaid="lk_juiceapp_mediaPopup_1243315889007" allowscriptaccess="always" wmode="transparent" flashvars="highlighterId=highlighter_808034" menu="false"></embed>
print
?
01.
import
org.json.JSONObject;
02.
...
03.
...
04.
JSONObject json =
new
JSONObject();
05.
json.put(
"city"
,
"Mumbai"
);
06.
json.put(
"country"
,
"India"
);
07.
...
08.
String output = json.toString();
09.
...
10. 使用iText JAR生成PDF
阅读这篇文章 了解更多细节
view source
<embed id="highlighter_651063_clipboard" title="copy to clipboard" type="application/x-shockwave-flash" width="16" height="16" src="http://cocre.com/wp-content/plugins/syntaxhighlighter/syntaxhighlighter/scripts/clipboard.swf" lk_media="yes" lk_mediaid="lk_juiceapp_mediaPopup_1243315889011" allowscriptaccess="always" wmode="transparent" flashvars="highlighterId=highlighter_651063" menu="false"></embed>
print
?
01.
02.
import
java.io.File;
03.
import
java.io.FileOutputStream;
04.
import
java.io.OutputStream;
05.
import
java.util.Date;
06.
07.
import
com.lowagie.text.Document;
08.
import
com.lowagie.text.Paragraph;
09.
import
com.lowagie.text.pdf.PdfWriter;
10.
11.
public
class
GeneratePDF {
12.
13.
public
static
void
main(String[] args) {
14.
try
{
15.
OutputStream file =
new
FileOutputStream(
new
File(
"C:\\Test.pdf"
));
16.
17.
Document document =
new
Document();
18.
PdfWriter.getInstance(document, file);
19.
document.open();
20.
document.add(
new
Paragraph(
"Hello Kiran"
));
21.
document.add(
new
Paragraph(
new
Date().toString()));
22.
23.
document.close();
24.
file.close();
25.
26.
}
catch
(Exception e) {
27.
28.
e.printStackTrace();
29.
}
30.
}
31.
}
11. HTTP 代理设置
阅读这篇 文章 了解更多细节。
view source
<embed id="highlighter_44581_clipboard" title="copy to clipboard" type="application/x-shockwave-flash" width="16" height="16" src="http://cocre.com/wp-content/plugins/syntaxhighlighter/syntaxhighlighter/scripts/clipboard.swf" lk_media="yes" lk_mediaid="lk_juiceapp_mediaPopup_1243315889015" allowscriptaccess="always" wmode="transparent" flashvars="highlighterId=highlighter_44581" menu="false"></embed>
print
?
1.
2.
System.getProperties().put(
"http.proxyHost"
,
"someProxyURL"
);
3.
System.getProperties().put(
"http.proxyPort"
,
"someProxyPort"
);
4.
System.getProperties().put(
"http.proxyUser"
,
"someUserName"
);
5.
System.getProperties().put(
"http.proxyPassword"
,
"somePassword"
);
发表评论
-
Java实现通用线程池
2009-10-12 16:17 1032URL: http://blog.csdn.net/polar ... -
java 内存溢出分析
2009-10-09 15:44 1197内存溢出是指应用系统中存在无法回收的内存或使用的内存过多,最终 ... -
Java的内存泄漏
2009-10-09 13:27 800Java 的一个重要优点就 ... -
dom4j操作xml基础--Visitor访问模式解析XML
2009-07-03 17:15 1323http://www.blogjava.net/bulktre ... -
Dom4j的全面解析
2009-07-03 17:03 983作者:冰云 icecloud(AT)sin ... -
关于java使用javacomm20-win32实践总结
2009-06-21 23:34 723由于这几天要通过java调 ... -
20非常有用的Java程序片段 (下)
2009-05-26 14:29 107617. 把 Array 转换成 Map vi ... -
20非常有用的Java程序片段 (中)
2009-05-26 14:08 88012. 单实例Singleton 示例 请先阅读这篇文章 ... -
java实现 冒泡排序 插入排序 选择排序
2009-03-16 00:47 1064package test.sort; public clas ... -
红黑树的Java实现
2009-03-16 00:42 941红黑树可能是要考虑情况最多的BST树了,它有自己的规则(见代码 ... -
排序算法复习(Java实现)(二): 归并排序,堆排序,桶式排序,基数排序
2009-03-16 00:40 1141转自:http://www.blogjava.net/java ... -
排序算法复习(Java实现)(一): 插入,冒泡,选择,Shell,快速排序
2009-03-16 00:37 880转自:http://www.blogjava.net/java ... -
Java 深层理解 父类引用指向子类对象
2009-03-10 11:44 2637从对象的内存角度来理解试试. 假设现在有一个父类Father, ... -
java native method
2009-03-02 20:40 953一. 什么是Native Method 简单地讲,一个Na ... -
java 简介--学习笔记
2009-02-22 22:26 731一 java 特点 1 、简单 Java 设计 ... -
String理解
2009-02-21 00:44 836要理解 java中String的运作方式,必须明确一点:Str ... -
Java的时间处理
2009-02-21 00:42 8791. Java计算时间依靠1970 ...
相关推荐
20个非常有用的Java程序片段20个非常有用的Java程序片段
下面是20个非常有用的Java程序片段,希望能对你有用。
20个非常有用的Java程序片段
JAVA 学习必备10个有用的程序片段 JAVA 学习必备10个有用的程序片段 JAVA 学习必备10个有用的程序片段 JAVA 学习必备10个有用的程序片段
介绍20个常用到的代码片段,如得到当前方法的名字、列出文件和目录、解析/读取XML 文件等
《java程序设计》课程的题库资料,由贺州学院整理,可供学生期末课程复习使用,也可以供相关任课教师出卷使用。 内容示例为: 9. 阅读下列程序片段,写出运行结果。 public class Test { public static void main...
主要为大家分享了20个非常实用的Java程序片段,对java开发项目有所帮助,感兴趣的小伙伴们可以参考一下
JAVA 代码 经典 经验
实验二Java面向对象程序设计 (1)定义表示课程的类Course,课程的属性包括课程名、编号、先修课号;方法包括设置课程名、设置编号、设置先修课号,以及打印课程名、课程号、先修课号。编写主程序,创建Course类的对象...
下面是20个非常有用的Java程序片段,希望能对你有用。
完整的代码示例,Java开发人员必备,涵盖日常Java开发经常要用到的功能代码。
20个常用的Java程序块 详细 //1. 字符串有整型的相互转换 002 String a =... java常用设计模式 java常用类 java常用包 java常用方法 java常用api java常用的..
通过java的ToolProvider创建JavaCompile,用来执行class源文件 * 4.创建DiagnosticCollector用来执行获取执行失败的错误结果 * 5.添加动态执行的编译环境 options 是个集合,添加内容,字符集,classpath等 * 6....
基于功能代码片段的Java后门检测方法.pdf
javamid处理一些 java 代码片段使用 java 实用程序来读取/写入/播放 MIDI 文件。
JAVA时钟小应用程序 代码片段 { Thread 时针=null, 分针=null,秒针=null; //用来表示时针、分针和秒针的线程。 //表示时针、分针、秒针端点的整型变量: int hour_a,hour_b,munite_a,munite_b,second_a,...
1给定如下Java程序,编译运行时,将在()出现错误。(选一项) public class Test{ public static void main(String[]args){ int i= 0; //a语句 for(;i;i++){ //b语句 if(i>5){ String test = “hello”; /...
这是java程序设计基础片段的课文代码和课后习题的代码,大部分都拥有
Java 根据数字报语音的程序 一个用JAVA写的语音报号程序,不过报出的是英文,不过你只要掌握了原理就好办多了,替换一下语音文件就可以了。它可以根据你输入的数字报出相对应的英文发音,实际上程序还是很简单的。