- 浏览: 194501 次
- 性别:
- 来自: 上海
文章分类
最新评论
-
hao3721:
dsfasd
ehcache 使用 -
nihaokid:
方法是不会存在在对象内存中的,它存在于方法区,建议看看jvm的 ...
Java 深层理解 父类引用指向子类对象 -
vissalan:
有一点没看明白Father f1 = (Father)s;这时 ...
Java 深层理解 父类引用指向子类对象 -
咖啡舞者:
非常感谢这种分享精神.
在BREW中实现自己的GUI(8)-IWEB的封装 -
咖啡舞者:
这是创建的代码。
在设备上调的。
界面在手机和模拟器上显示的差异
12. 单实例Singleton 示例
请先阅读这篇文章 了解更多信息
view source
<embed id="highlighter_35199_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_1243315889019" allowscriptaccess="always" wmode="transparent" flashvars="highlighterId=highlighter_35199" menu="false"></embed>
print
?
01.
02.
public
class
SimpleSingleton {
03.
private
static
SimpleSingleton singleInstance =
new
SimpleSingleton();
04.
05.
//Marking default constructor private
06.
//to avoid direct instantiation.
07.
private
SimpleSingleton() {
08.
}
09.
10.
//Get instance for class SimpleSingleton
11.
public
static
SimpleSingleton getInstance() {
12.
13.
return
singleInstance;
14.
}
15.
}
另一种实现
view source
<embed id="highlighter_120550_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_1243315889023" allowscriptaccess="always" wmode="transparent" flashvars="highlighterId=highlighter_120550" menu="false"></embed>
print
?
1.
public
enum
SimpleSingleton {
2.
INSTANCE;
3.
public
void
doSomething() {
4.
}
5.
}
6.
7.
//Call the method from Singleton:
8.
SimpleSingleton.INSTANCE.doSomething();
13. 抓屏程序
阅读这篇文章 获得更多信息。
view source
<embed id="highlighter_817602_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_1243315889027" allowscriptaccess="always" wmode="transparent" flashvars="highlighterId=highlighter_817602" menu="false"></embed>
print
?
01.
import
java.awt.Dimension;
02.
import
java.awt.Rectangle;
03.
import
java.awt.Robot;
04.
import
java.awt.Toolkit;
05.
import
java.awt.image.BufferedImage;
06.
import
javax.imageio.ImageIO;
07.
import
java.io.File;
08.
09.
...
10.
11.
public
void
captureScreen(String fileName)
throws
Exception {
12.
13.
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
14.
Rectangle screenRectangle =
new
Rectangle(screenSize);
15.
Robot robot =
new
Robot();
16.
BufferedImage image = robot.createScreenCapture(screenRectangle);
17.
ImageIO.write(image,
"png"
,
new
File(fileName));
18.
19.
}
20.
...
14. 列出文件和目录
view source
<embed id="highlighter_1356_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_1243315889031" allowscriptaccess="always" wmode="transparent" flashvars="highlighterId=highlighter_1356" menu="false"></embed>
print
?
01.
File dir =
new
File(
"directoryName"
);
02.
String[] children = dir.list();
03.
if
(children ==
null
) {
04.
// Either dir does not exist or is not a directory
05.
}
else
{
06.
for
(
int
i=
0
; i < children.length; i++) {
07.
// Get filename of file or directory
08.
String filename = children[i];
09.
}
10.
}
11.
12.
// It is also possible to filter the list of returned files.
13.
// This example does not return any files that start with `.'.
14.
FilenameFilter filter =
new
FilenameFilter() {
15.
public
boolean
accept(File dir, String name) {
16.
return
!name.startsWith(
"."
);
17.
}
18.
};
19.
children = dir.list(filter);
20.
21.
// The list of files can also be retrieved as File objects
22.
File[] files = dir.listFiles();
23.
24.
// This filter only returns directories
25.
FileFilter fileFilter =
new
FileFilter() {
26.
public
boolean
accept(File file) {
27.
return
file.isDirectory();
28.
}
29.
};
30.
files = dir.listFiles(fileFilter);
15. 创建ZIP和JAR文件
view source
<embed id="highlighter_695137_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_1243315889035" allowscriptaccess="always" wmode="transparent" flashvars="highlighterId=highlighter_695137" menu="false"></embed>
print
?
01.
02.
import
java.util.zip.*;
03.
import
java.io.*;
04.
05.
public
class
ZipIt {
06.
public
static
void
main(String args[])
throws
IOException {
07.
if
(args.length <
2
) {
08.
System.err.println(
"usage: java ZipIt Zip.zip file1 file2 file3"
);
09.
System.exit(-
1
);
10.
}
11.
File zipFile =
new
File(args[
0
]);
12.
if
(zipFile.exists()) {
13.
System.err.println(
"Zip file already exists, please try another"
);
14.
System.exit(-
2
);
15.
}
16.
FileOutputStream fos =
new
FileOutputStream(zipFile);
17.
ZipOutputStream zos =
new
ZipOutputStream(fos);
18.
int
bytesRead;
19.
byte
[] buffer =
new
byte
[
1024
];
20.
CRC32 crc =
new
CRC32();
21.
for
(
int
i=
1
, n=args.length; i < n; i++) {
22.
String name = args[i];
23.
File file =
new
File(name);
24.
if
(!file.exists()) {
25.
System.err.println(
"Skipping: "
+ name);
26.
continue
;
27.
}
28.
BufferedInputStream bis =
new
BufferedInputStream(
29.
new
FileInputStream(file));
30.
crc.reset();
31.
while
((bytesRead = bis.read(buffer)) != -
1
) {
32.
crc.update(buffer,
0
, bytesRead);
33.
}
34.
bis.close();
35.
// Reset to beginning of input stream
36.
bis =
new
BufferedInputStream(
37.
new
FileInputStream(file));
38.
ZipEntry entry =
new
ZipEntry(name);
39.
entry.setMethod(ZipEntry.STORED);
40.
entry.setCompressedSize(file.length());
41.
entry.setSize(file.length());
42.
entry.setCrc(crc.getValue());
43.
zos.putNextEntry(entry);
44.
while
((bytesRead = bis.read(buffer)) != -
1
) {
45.
zos.write(buffer,
0
, bytesRead);
46.
}
47.
bis.close();
48.
}
49.
zos.close();
50.
}
51.
}
16. 解析/读取XML 文件
XML文件
view source
<embed id="highlighter_879090_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_1243315889039" allowscriptaccess="always" wmode="transparent" flashvars="highlighterId=highlighter_879090" menu="false"></embed>
print
?
01.
<?
xml
version
=
"1.0"
?>
02.
<
students
>
03.
<
student
>
04.
<
name
>John</
name
>
05.
<
grade
>B</
grade
>
06.
<
age
>12</
age
>
07.
</
student
>
08.
<
student
>
09.
<
name
>Mary</
name
>
10.
<
grade
>A</
grade
>
11.
<
age
>11</
age
>
12.
</
student
>
13.
<
student
>
14.
<
name
>Simon</
name
>
15.
<
grade
>A</
grade
>
16.
<
age
>18</
age
>
17.
</
student
>
18.
</
students
>
Java代码
view source
<embed id="highlighter_40476_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_1243315889043" allowscriptaccess="always" wmode="transparent" flashvars="highlighterId=highlighter_40476" menu="false"></embed>
print
?
01.
02.
package
net.viralpatel.java.xmlparser;
03.
04.
import
java.io.File;
05.
import
javax.xml.parsers.DocumentBuilder;
06.
import
javax.xml.parsers.DocumentBuilderFactory;
07.
08.
import
org.w3c.dom.Document;
09.
import
org.w3c.dom.Element;
10.
import
org.w3c.dom.Node;
11.
import
org.w3c.dom.NodeList;
12.
13.
public
class
XMLParser {
14.
15.
public
void
getAllUserNames(String fileName) {
16.
try
{
17.
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
18.
DocumentBuilder db = dbf.newDocumentBuilder();
19.
File file =
new
File(fileName);
20.
if
(file.exists()) {
21.
Document doc = db.parse(file);
22.
Element docEle = doc.getDocumentElement();
23.
24.
// Print root element of the document
25.
System.out.println(
"Root element of the document: "
26.
+ docEle.getNodeName());
27.
28.
NodeList studentList = docEle.getElementsByTagName(
"student"
);
29.
30.
// Print total student elements in document
31.
System.out
32.
.println(
"Total students: "
+ studentList.getLength());
33.
34.
if
(studentList !=
null
&& studentList.getLength() >
0
) {
35.
for
(
int
i =
0
; i < studentList.getLength(); i++) {
36.
37.
Node node = studentList.item(i);
38.
39.
if
(node.getNodeType() == Node.ELEMENT_NODE) {
40.
41.
System.out
42.
.println(
"====================="
);
43.
44.
Element e = (Element) node;
45.
NodeList nodeList = e.getElementsByTagName(
"name"
);
46.
System.out.println(
"Name: "
47.
+ nodeList.item(
0
).getChildNodes().item(
0
)
48.
.getNodeValue());
49.
50.
nodeList = e.getElementsByTagName(
"grade"
);
51.
System.out.println(
"Grade: "
52.
+ nodeList.item(
0
).getChildNodes().item(
0
)
53.
.getNodeValue());
54.
55.
nodeList = e.getElementsByTagName(
"age"
);
56.
System.out.println(
"Age: "
57.
+ nodeList.item(
0
).getChildNodes().item(
0
)
58.
.getNodeValue());
59.
}
60.
}
61.
}
else
{
62.
System.exit(
1
);
63.
}
64.
}
65.
}
catch
(Exception e) {
66.
System.out.println(e);
67.
}
68.
}
69.
public
static
void
main(String[] args) {
70.
71.
XMLParser parser =
new
XMLParser();
72.
parser.getAllUserNames(
"c:\\test.xml"
);
73.
}
74.
}
发表评论
-
Java实现通用线程池
2009-10-12 16:17 1033URL: http://blog.csdn.net/polar ... -
java 内存溢出分析
2009-10-09 15:44 1197内存溢出是指应用系统中存在无法回收的内存或使用的内存过多,最终 ... -
Java的内存泄漏
2009-10-09 13:27 801Java 的一个重要优点就 ... -
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 107717. 把 Array 转换成 Map vi ... -
20非常有用的Java程序片段 (上)
2009-05-26 14:02 941下面是20个非常有用的Java程序片段,希望能对你有用。 1 ... -
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 1142转自: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开发经常要用到的功能代码。
JAVA 代码 经典 经验
实验二Java面向对象程序设计 (1)定义表示课程的类Course,课程的属性包括课程名、编号、先修课号;方法包括设置课程名、设置编号、设置先修课号,以及打印课程名、课程号、先修课号。编写主程序,创建Course类的对象...
下面是20个非常有用的Java程序片段,希望能对你有用。
通过java的ToolProvider创建JavaCompile,用来执行class源文件 * 4.创建DiagnosticCollector用来执行获取执行失败的错误结果 * 5.添加动态执行的编译环境 options 是个集合,添加内容,字符集,classpath等 * 6....
20个常用的Java程序块 详细 //1. 字符串有整型的相互转换 002 String a =... java常用设计模式 java常用类 java常用包 java常用方法 java常用api java常用的..
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后门检测方法.pdf
javamid处理一些 java 代码片段使用 java 实用程序来读取/写入/播放 MIDI 文件。
JAVA时钟小应用程序 代码片段 { Thread 时针=null, 分针=null,秒针=null; //用来表示时针、分针和秒针的线程。 //表示时针、分针、秒针端点的整型变量: int hour_a,hour_b,munite_a,munite_b,second_a,...
Java片段 Java开发中,经典,高效的程序代码片段
这是java程序设计基础片段的课文代码和课后习题的代码,大部分都拥有