记得第一次接触闭包的时候,觉得很奇怪,但从字面上很那理解闭包什么玩意,和闭包有的一比的当属控制反转,真正理解了后觉得就平常了。闭包二字其实是很经典的,闭包代码所表现的东西可不就是一个封闭的、自成一体的功能块吗?简单的说,闭包就是在内部定义的方法,拿到外面去使用。经典的javascript闭包形式如下:
Java代码
Js代码
function f1(){
var i = 20;
function closef(x){
alert(i+x);
}
return closef
}
var s = f1();
s(20);
function f1(){
var i = 20;
function closef(x){
alert(i+x);
}
return closef
}
var s = f1();
s(20);
javascript的这种闭包形式的确相当简洁,类似于c语言的函数指针
......
需要dom4j.jar文件 ,自行下载。 test.xml 1: <?xml version="1.0" encoding="gbk"?>
2:
3: <students>
4: <person sex="男" age="21">
5: <id>1</id>
6: <name>章治鹏</name>
7: <homepage>http://blog.csdn.net/tonyzzp</homepage>
8: </person>
9: <person age="20">
10: <id>2</id>
11: <name>徐雄皓</name>
12: <homepage boolean="false">http://www.xxh.com</homepage>
13: </person>
14: </students>
XMLStudentsParam.java
1: package org.zzp.common.xml.dom4j;
2:
3: public enum XMLStudentsParam {
4: id,name,homepage,sex,age
5: }
Dom4jReadDemo.java
1: package org.zzp.common.xml.dom4j;
2:
3: import java.util.Iterator;
4: import java.util.List;
5:
6: import org.dom4j.Attribute;
7: import org.dom4j.Document;
8: imp ......
需要dom4j.jar文件 ,自行下载。 test.xml 1: <?xml version="1.0" encoding="gbk"?>
2:
3: <students>
4: <person sex="男" age="21">
5: <id>1</id>
6: <name>章治鹏</name>
7: <homepage>http://blog.csdn.net/tonyzzp</homepage>
8: </person>
9: <person age="20">
10: <id>2</id>
11: <name>徐雄皓</name>
12: <homepage boolean="false">http://www.xxh.com</homepage>
13: </person>
14: </students>
XMLStudentsParam.java
1: package org.zzp.common.xml.dom4j;
2:
3: public enum XMLStudentsParam {
4: id,name,homepage,sex,age
5: }
Dom4jReadDemo.java
1: package org.zzp.common.xml.dom4j;
2:
3: import java.util.Iterator;
4: import java.util.List;
5:
6: import org.dom4j.Attribute;
7: import org.dom4j.Document;
8: imp ......
Introduction to XML and XML With Java
If you are looking for sample programs to parse a XML file using DOM/SAX parser or looking for a program to generate a XML file please proceed directly to programs.
This small tutorial introduces you to the basic concepts of XML and using Xerces parser for Java to generate and parse XML.
The intended audience are XML beginners with knowledge of Java.
Last updated:
03 Oct 2005.Modified programs runs in JDK 1.3, JDK 1.4 and JDK 1.5. Xerces parser is used with JDK 1.4 and 1.3.
Contents
1 Introduction
2 What is XML
3 Advantages of XML
3.1 Readability
3.2 Hierarchical
3.3 Language Independent
3.4 OS independent
4 Uses of XML
4.1 MetaContent
4.2 Messaging
4.3 Database
5 Parsers
5.1 DOM
5.2 SAX
5.3 When to use DOM Parser
5.4 When to use SAX Parser
5.5 Validating And Non Validating
5.6 Well Formedness
6 Programs
6.1 Parsing and Printing
6.1.1 Using DOM
6.1.2 Using SAX
6.2 Generating X ......
Introduction to XML and XML With Java
If you are looking for sample programs to parse a XML file using DOM/SAX parser or looking for a program to generate a XML file please proceed directly to programs.
This small tutorial introduces you to the basic concepts of XML and using Xerces parser for Java to generate and parse XML.
The intended audience are XML beginners with knowledge of Java.
Last updated:
03 Oct 2005.Modified programs runs in JDK 1.3, JDK 1.4 and JDK 1.5. Xerces parser is used with JDK 1.4 and 1.3.
Contents
1 Introduction
2 What is XML
3 Advantages of XML
3.1 Readability
3.2 Hierarchical
3.3 Language Independent
3.4 OS independent
4 Uses of XML
4.1 MetaContent
4.2 Messaging
4.3 Database
5 Parsers
5.1 DOM
5.2 SAX
5.3 When to use DOM Parser
5.4 When to use SAX Parser
5.5 Validating And Non Validating
5.6 Well Formedness
6 Programs
6.1 Parsing and Printing
6.1.1 Using DOM
6.1.2 Using SAX
6.2 Generating X ......
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
*
* @description 本程序实现了读取注册表分支:HKEY_CURRENT_USER\Software\ODBC\ODBC.INI\ODBC Data Sources到内存的操作
* @author:narsh
* @time 2010-2-8
*/
public class getRegToMemery {
public static void main(String []args){
try {
Process ps = null;
ps = Runtime
.getRuntime()
.exec(
"reg query \"HKEY_CURRENT_USER\\Software\\ODBC\\ODBC.INI\\ODBC Data Sources\"");
ps.getOutputStream().close();
InputStreamReader i = new InputStreamReader(ps.getInputStream());
String line;
BufferedReader ir = new BufferedReader(i);
while ((line = ir.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
......
/**
* 下载文件
* @param filePath --文件完整路径
* @param response --HttpServletResponse对象
*/
public static void downloadFile(
String filePath,
javax.servlet.http.HttpServletResponse response) {
String fileName = ""; //文件名,输出到用户的下载对话框
//从文件完整路径中提取文件名,并进行编码转换,防止不能正确显示中文名
try {
if(filePath.lastIndexOf("/") > 0) {
fileName = new String(filePath.substring(filePath.lastIndexOf("/")+1, filePath.length()).getBytes("GB2312"), "ISO8859_1");
}else if(filePath.lastIndexOf("\\") > 0) {
fileName = new String(filePath.substring(filePath.lastIndexOf("\\")+1, filePath.length()).getBytes("GB2312"), "ISO8859_1");
}
}catch(Exception e) {}
//打开指定文件的流信息
FileInputStream fs = null;
try {
fs = new FileInputStream(new File(filePath));
}catch(FileNotFoundException e) {
e.printStackTrace();
return;
}
//设置响应头和保存文件名
response.setContentType("APPLICATION/OCTET-STREAM");
response.setHeader("Content-Disposition", ......
首先需要知道的是,MP3文件的文件信息都放在文件最后的128个字节里面,这128个字节分别存储的信息如下:
char Header[3]; /* 标签头必须是"TAG"否则认为没有标签 */
char Title[30]; /* 标题 */
char Artist[30]; /* 作者 */
char Album[30]; /* 专集 */
char Year[4]; /* 出品年代 */
char Comment[28]; /* 备注 */
char reserve; /* 保留 */
char track;; /* 音轨 */
char Genre; /* 类型 */
代码:
public class ReadMP3 {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
String path = System.getProperty("user.dir")+"/images/wenbie.mp3";
readMp3ID3V1(path); ......