- 浏览: 63965 次
- 来自: ...
文章分类
最新评论
-
wait10000y:
简单~!!!简单的不可以解析大的数据了~!
java解析xml字符串 -
zhouzhichu:
呵呵,我感觉写的也一般
mysql存储过程实例 -
haidage:
写的就一个垃圾,上火
mysql存储过程实例 -
hawk315:
你这个上面是不是有步骤地遗失?
b.创建Portlets Ap ...
Eclipse下Portlet应用开发(JSR168)
1.得到当前月的第一天和最后一天,注意3是4月份,从零开始
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, 2007);
c.set(Calendar.MONTH, 3);
System.out.println(c.getActualMaximum(c.DAY_OF_MONTH));
System.out.println(c.getActualMinimum(c.DAY_OF_MONTH));
2.十六进制的转换
String st = "this is roger";
// hex
for(int i = 0; i<st.length(); i++){
System.out.println();
int ch=(int)st.charAt( i );
String s4="00"+Integer.toHexString( ch );
System.out.println(i + "output->"+s4); // String to Hex
//back String
int intVal = Integer.parseInt(s4, 16);
char charVal = (char) intVal;
System.out.println("The character is: "+charVal);
}
int temp = 1234567;
String hexString = Integer.toHexString(temp);
System.out.println( "hexString = " + hexString );
String str = Integer.toString(Integer.parseInt(hexString, 16));
System.out.println( "str = " + str );
/ 转化十六进制编码为字符串
public static String toStringHex(String s)
{
byte[] baKeyword = new byte[s.length()/2];
for(int i = 0; i < baKeyword.length; i++)
{
try
{
baKeyword[i] = (byte)(0xff & Integer.parseInt(s.substring(i*2, i*2+2),16));
}
catch(Exception e)
{
e.printStackTrace();
}
}
try
{
s = new String(baKeyword, "utf-8");//UTF-16le:Not
}
catch (Exception e1)
{
e1.printStackTrace();
}
return s;
}
public static void main(String[] args) {
System.out.println(encode("中文"));
System.out.println(decode(encode("中文")));
}
/*
* 16进制数字字符集
*/
private static String hexString="0123456789ABCDEF";
/*
* 将字符串编码成16进制数字,适用于所有字符(包括中文)
*/
public static String encode(String str)
{
//根据默认编码获取字节数组
byte[] bytes=str.getBytes();
StringBuilder sb=new StringBuilder(bytes.length*2);
//将字节数组中每个字节拆解成2位16进制整数
for(int i=0;i<bytes.length;i++)
{
sb.append(hexString.charAt((bytes[i]&0xf0)>>4));
sb.append(hexString.charAt((bytes[i]&0x0f)>>0));
}
return sb.toString();
}
/*
* 将16进制数字解码成字符串,适用于所有字符(包括中文)
*/
public static String decode(String bytes)
{
ByteArrayOutputStream baos=new ByteArrayOutputStream(bytes.length()/2);
//将每2位16进制整数组装成一个字节
for(int i=0;i<bytes.length();i+=2)
baos.write((hexString.indexOf(bytes.charAt(i))<<4 |hexString.indexOf(bytes.charAt(i+1))));
return new String(baos.toByteArray());
}
或
String str="中";
char c=(char)str.charAt(0);
System.out.println(c+"转换为16进制为:"+Integer.toString(c,16));
System.out.println("16进制转换为字符串为:"+(char)Integer.valueOf(Integer.toString(c, 16),16).intValue());
public class testcolor {
private static byte uniteBytes(byte src0, byte src1) {
byte _b0 = Byte.decode("0x" + new String(new byte[] {src0})).byteValue();
_b0 = (byte) (_b0 << 4);
byte _b1 = Byte.decode("0x" + new String(new byte[] {src1})).byteValue();
byte ret = (byte) (_b0 ^ _b1);
// System.out.println(ret<<8);
return ret;
}
/**
* @param src
* @return
*/
/*private static byte[] HexString2Bytes(String src) {
byte[] ret = new byte[6];
byte[] tmp = src.getBytes();
for (int i = 0; i < 3; i++) {
ret[i] = uniteBytes(tmp[i * 2], tmp[i * 2 + 1]);
}
return ret;
}
;*/
public static byte[] HexString2Bytes(String src)
{ byte[] ret = new byte[6];
byte[] tmp = src.getBytes(); for(int i=0; i<6; ++i ){ ret[i] = uniteBytes(tmp[i*2], tmp[i*2+1]); } return ret; }
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
String s="ff22ee";
Number ss;
byte[] ret = new byte[6];
Integer i=new Integer(ret[0]);
ret = HexString2Bytes(s) ;
int c=((ret[2]<<8|ret[1]<<8|ret[0])|0xff000000);
//Color cc=new Color(c);
// System.out.println(cc.getRed()+":::"+cc.getBlue()+":::"+cc.getGreen());
}
}
将IP地址转为10进制数,使用小端模式
public static int parseIPNUmber(String ipNumberStr)
throws IllegalArgumentException {
int result = 0;
if (ipNumberStr == null) {
throw new IllegalArgumentException();
}
StringTokenizer tokenizer = new StringTokenizer(ipNumberStr, ".");
try {
String tmp = ipNumberStr;
// get the 3 first numbers
int offset = 0;
for (int i = 0; i < 3; i++) {
// get the position of the first dot
int index = tmp.indexOf(".");
// if there is not a dot then the ip string representation is
// not compliant to the decimal-dotted notation.
if (index != -1) {
// get the number before the dot and convert it into
// an integer.
String numberStr = tmp.substring(0, index);
int number = Integer.parseInt(numberStr);
if ((number < 0) || (number > 255)) {
throw new IllegalArgumentException(ipNumberStr);
}
result += number << offset;
offset += 8;
tmp = tmp.substring(index + 1);
} else {
throw new IllegalArgumentException(ipNumberStr);
}
}
// the remaining part of the string should be the last number.
if (tmp.length() > 0) {
int number = Integer.parseInt(tmp);
if ((number < 0) || (number > 255)) {
throw new IllegalArgumentException(ipNumberStr);
}
result += number << offset;
} else {
throw new IllegalArgumentException(ipNumberStr);
}
}
catch (NoSuchElementException ex) {
throw new IllegalArgumentException(ipNumberStr);
}
catch (NumberFormatException ex) {
throw new IllegalArgumentException(ipNumberStr);
}
return result;
}
将10进制数字转为IP地址
public static String toip(int mIPNumber) {
StringBuffer result = new StringBuffer();
int temp;
temp = mIPNumber & 0x000000FF;
result.append(temp);
result.append(".");
temp = (mIPNumber >> & 0x000000FF;
result.append(temp);
result.append(".");
temp = (mIPNumber >> 16) & 0x000000FF;
result.append(temp);
result.append(".");
temp = (mIPNumber >> 24) & 0x000000FF;
result.append(temp);
return result.toString();
}
掩码和网络地址的转换 例如 /24就是255.255.255.0
public static int computeNetworkPrefixFromMask(int mask) {
int result = 0;
int tmp = mask;
while ((tmp & 0x00000001) == 0x00000001) {
result++;
tmp = tmp >>> 1;
}
if (tmp != 0) {
return -1;
}
return result;
}
public static int computeMaskFromNetworkPrefix(int prefix)
{
int subnet = 0;
for (int i = 0; i < prefix; i++) {
subnet = subnet << 1;
subnet += 1;
}
return subnet;
}
例如 System.out.println(toip(computeMaskFromNetworkPrefix(20)));
是255.255.15.0
判断两个IP是不是在一个子网内
public static boolean isIPAddressInRange (int address1,int adress2,int subnet)
{
int result1 = address1 & subnet;
int result2 =adress2 & subnet;
return result1 == result2;
}
读取classes的下面的文件
TestDOMParsing.class.getResourceAsStream("/MSG-response.xml");
返回输入流
web工程不必这样做
使用ServletContext.getResourceAsStream()就可以.
sun的文档这样说得
Returns a URL to the resource that is mapped to a specified path. The path must begin with a "/" and is interpreted as relative to the current context root.
This method allows the servlet container to make a resource available to servlets from any source. Resources can be located on a local or remote file system, in a #base, or in a .war file.
简单类型的排序
简单类型不外是byte, char, short, int, long, float, double等数据类型, 这些类型不能放在聚集中,只能使用数组。java.util.Arrays方法提供了对这些类型的sort方法(实际上还有很多其他有用的方法),下面是对一个简单的int数组排序:
int[] arr = {2, 3, 1,10,7,4};
System.out.print("before sort: ");
for (int i = 0; i< arr.length; i++)
System.out.print(arr[i] + " ");
System.out.println();
Arrays.sort(arr);
System.out.print("after sort: ");
for (int i = 0; i< arr.length; i++)
System.out.print(arr[i] + " ");
System.out.println();
输出结果:
before sort: 2 3 1 10 7 4
after sort: 1 2 3 4 7 10
对象的排序
对象可以放在数组里,同样调用Arrays.sort(Object[] arr)即可;也可以放到聚集里,用java.util.Collections的sort(List list)。注意不是list必须实现List接口而不仅仅是Collection接口。
但是这个类必须实现了java.lang.Comparable接口。这个接口只有一个方法:int compartTo(Object o),当本对象比传入的对象大时,返回一个正整数。 以类Programmer为例:
class Programmer implements Comparable{
private String name;
private String language;
private double pay;
public Programmer(String name, String language, double pay) {
this.name = name;
this.language = language;
this.pay = pay;
}
public int compareTo(Object o) {
Programmer other = (Programmer)o;
return (int)pay - (int)other.pay;
}
public String toString(){
return "{name: " + name + ", language: " + language + ", money: " + pay + "}";
}
}
对其进行排序:
ArrayList list = new ArrayList();
list.add(new Programmer("张三", "C", 12000));
list.add(new Programmer("李四", "Java", 200));
list.add(new Programmer("王五", "C++", 5000));
list.add(new Programmer("钱六", "VB", 3000));
System.out.println("before sort: " + list);
Collections.sort(list);
System.out.println("after sort: " + list);
输出:
before sort: [{name: 张三, language: C, money: 12000.0}, {name: 李四, language: Java, money: 200.0}, {name: 王五, language: C++, money: 5000.0}, {name: 钱六, language: VB, money: 3000.0}]
after sort: [{name: 李四, language: Java, money: 200.0}, {name: 钱六, language: VB, money: 3000.0}, {name: 王五, language: C++, money: 5000.0}, {name: 张三, language: C, money: 12000.0}]
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, 2007);
c.set(Calendar.MONTH, 3);
System.out.println(c.getActualMaximum(c.DAY_OF_MONTH));
System.out.println(c.getActualMinimum(c.DAY_OF_MONTH));
2.十六进制的转换
String st = "this is roger";
// hex
for(int i = 0; i<st.length(); i++){
System.out.println();
int ch=(int)st.charAt( i );
String s4="00"+Integer.toHexString( ch );
System.out.println(i + "output->"+s4); // String to Hex
//back String
int intVal = Integer.parseInt(s4, 16);
char charVal = (char) intVal;
System.out.println("The character is: "+charVal);
}
int temp = 1234567;
String hexString = Integer.toHexString(temp);
System.out.println( "hexString = " + hexString );
String str = Integer.toString(Integer.parseInt(hexString, 16));
System.out.println( "str = " + str );
/ 转化十六进制编码为字符串
public static String toStringHex(String s)
{
byte[] baKeyword = new byte[s.length()/2];
for(int i = 0; i < baKeyword.length; i++)
{
try
{
baKeyword[i] = (byte)(0xff & Integer.parseInt(s.substring(i*2, i*2+2),16));
}
catch(Exception e)
{
e.printStackTrace();
}
}
try
{
s = new String(baKeyword, "utf-8");//UTF-16le:Not
}
catch (Exception e1)
{
e1.printStackTrace();
}
return s;
}
public static void main(String[] args) {
System.out.println(encode("中文"));
System.out.println(decode(encode("中文")));
}
/*
* 16进制数字字符集
*/
private static String hexString="0123456789ABCDEF";
/*
* 将字符串编码成16进制数字,适用于所有字符(包括中文)
*/
public static String encode(String str)
{
//根据默认编码获取字节数组
byte[] bytes=str.getBytes();
StringBuilder sb=new StringBuilder(bytes.length*2);
//将字节数组中每个字节拆解成2位16进制整数
for(int i=0;i<bytes.length;i++)
{
sb.append(hexString.charAt((bytes[i]&0xf0)>>4));
sb.append(hexString.charAt((bytes[i]&0x0f)>>0));
}
return sb.toString();
}
/*
* 将16进制数字解码成字符串,适用于所有字符(包括中文)
*/
public static String decode(String bytes)
{
ByteArrayOutputStream baos=new ByteArrayOutputStream(bytes.length()/2);
//将每2位16进制整数组装成一个字节
for(int i=0;i<bytes.length();i+=2)
baos.write((hexString.indexOf(bytes.charAt(i))<<4 |hexString.indexOf(bytes.charAt(i+1))));
return new String(baos.toByteArray());
}
或
String str="中";
char c=(char)str.charAt(0);
System.out.println(c+"转换为16进制为:"+Integer.toString(c,16));
System.out.println("16进制转换为字符串为:"+(char)Integer.valueOf(Integer.toString(c, 16),16).intValue());
public class testcolor {
private static byte uniteBytes(byte src0, byte src1) {
byte _b0 = Byte.decode("0x" + new String(new byte[] {src0})).byteValue();
_b0 = (byte) (_b0 << 4);
byte _b1 = Byte.decode("0x" + new String(new byte[] {src1})).byteValue();
byte ret = (byte) (_b0 ^ _b1);
// System.out.println(ret<<8);
return ret;
}
/**
* @param src
* @return
*/
/*private static byte[] HexString2Bytes(String src) {
byte[] ret = new byte[6];
byte[] tmp = src.getBytes();
for (int i = 0; i < 3; i++) {
ret[i] = uniteBytes(tmp[i * 2], tmp[i * 2 + 1]);
}
return ret;
}
;*/
public static byte[] HexString2Bytes(String src)
{ byte[] ret = new byte[6];
byte[] tmp = src.getBytes(); for(int i=0; i<6; ++i ){ ret[i] = uniteBytes(tmp[i*2], tmp[i*2+1]); } return ret; }
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
String s="ff22ee";
Number ss;
byte[] ret = new byte[6];
Integer i=new Integer(ret[0]);
ret = HexString2Bytes(s) ;
int c=((ret[2]<<8|ret[1]<<8|ret[0])|0xff000000);
//Color cc=new Color(c);
// System.out.println(cc.getRed()+":::"+cc.getBlue()+":::"+cc.getGreen());
}
}
将IP地址转为10进制数,使用小端模式
public static int parseIPNUmber(String ipNumberStr)
throws IllegalArgumentException {
int result = 0;
if (ipNumberStr == null) {
throw new IllegalArgumentException();
}
StringTokenizer tokenizer = new StringTokenizer(ipNumberStr, ".");
try {
String tmp = ipNumberStr;
// get the 3 first numbers
int offset = 0;
for (int i = 0; i < 3; i++) {
// get the position of the first dot
int index = tmp.indexOf(".");
// if there is not a dot then the ip string representation is
// not compliant to the decimal-dotted notation.
if (index != -1) {
// get the number before the dot and convert it into
// an integer.
String numberStr = tmp.substring(0, index);
int number = Integer.parseInt(numberStr);
if ((number < 0) || (number > 255)) {
throw new IllegalArgumentException(ipNumberStr);
}
result += number << offset;
offset += 8;
tmp = tmp.substring(index + 1);
} else {
throw new IllegalArgumentException(ipNumberStr);
}
}
// the remaining part of the string should be the last number.
if (tmp.length() > 0) {
int number = Integer.parseInt(tmp);
if ((number < 0) || (number > 255)) {
throw new IllegalArgumentException(ipNumberStr);
}
result += number << offset;
} else {
throw new IllegalArgumentException(ipNumberStr);
}
}
catch (NoSuchElementException ex) {
throw new IllegalArgumentException(ipNumberStr);
}
catch (NumberFormatException ex) {
throw new IllegalArgumentException(ipNumberStr);
}
return result;
}
将10进制数字转为IP地址
public static String toip(int mIPNumber) {
StringBuffer result = new StringBuffer();
int temp;
temp = mIPNumber & 0x000000FF;
result.append(temp);
result.append(".");
temp = (mIPNumber >> & 0x000000FF;
result.append(temp);
result.append(".");
temp = (mIPNumber >> 16) & 0x000000FF;
result.append(temp);
result.append(".");
temp = (mIPNumber >> 24) & 0x000000FF;
result.append(temp);
return result.toString();
}
掩码和网络地址的转换 例如 /24就是255.255.255.0
public static int computeNetworkPrefixFromMask(int mask) {
int result = 0;
int tmp = mask;
while ((tmp & 0x00000001) == 0x00000001) {
result++;
tmp = tmp >>> 1;
}
if (tmp != 0) {
return -1;
}
return result;
}
public static int computeMaskFromNetworkPrefix(int prefix)
{
int subnet = 0;
for (int i = 0; i < prefix; i++) {
subnet = subnet << 1;
subnet += 1;
}
return subnet;
}
例如 System.out.println(toip(computeMaskFromNetworkPrefix(20)));
是255.255.15.0
判断两个IP是不是在一个子网内
public static boolean isIPAddressInRange (int address1,int adress2,int subnet)
{
int result1 = address1 & subnet;
int result2 =adress2 & subnet;
return result1 == result2;
}
读取classes的下面的文件
TestDOMParsing.class.getResourceAsStream("/MSG-response.xml");
返回输入流
web工程不必这样做
使用ServletContext.getResourceAsStream()就可以.
sun的文档这样说得
Returns a URL to the resource that is mapped to a specified path. The path must begin with a "/" and is interpreted as relative to the current context root.
This method allows the servlet container to make a resource available to servlets from any source. Resources can be located on a local or remote file system, in a #base, or in a .war file.
简单类型的排序
简单类型不外是byte, char, short, int, long, float, double等数据类型, 这些类型不能放在聚集中,只能使用数组。java.util.Arrays方法提供了对这些类型的sort方法(实际上还有很多其他有用的方法),下面是对一个简单的int数组排序:
int[] arr = {2, 3, 1,10,7,4};
System.out.print("before sort: ");
for (int i = 0; i< arr.length; i++)
System.out.print(arr[i] + " ");
System.out.println();
Arrays.sort(arr);
System.out.print("after sort: ");
for (int i = 0; i< arr.length; i++)
System.out.print(arr[i] + " ");
System.out.println();
输出结果:
before sort: 2 3 1 10 7 4
after sort: 1 2 3 4 7 10
对象的排序
对象可以放在数组里,同样调用Arrays.sort(Object[] arr)即可;也可以放到聚集里,用java.util.Collections的sort(List list)。注意不是list必须实现List接口而不仅仅是Collection接口。
但是这个类必须实现了java.lang.Comparable接口。这个接口只有一个方法:int compartTo(Object o),当本对象比传入的对象大时,返回一个正整数。 以类Programmer为例:
class Programmer implements Comparable{
private String name;
private String language;
private double pay;
public Programmer(String name, String language, double pay) {
this.name = name;
this.language = language;
this.pay = pay;
}
public int compareTo(Object o) {
Programmer other = (Programmer)o;
return (int)pay - (int)other.pay;
}
public String toString(){
return "{name: " + name + ", language: " + language + ", money: " + pay + "}";
}
}
对其进行排序:
ArrayList list = new ArrayList();
list.add(new Programmer("张三", "C", 12000));
list.add(new Programmer("李四", "Java", 200));
list.add(new Programmer("王五", "C++", 5000));
list.add(new Programmer("钱六", "VB", 3000));
System.out.println("before sort: " + list);
Collections.sort(list);
System.out.println("after sort: " + list);
输出:
before sort: [{name: 张三, language: C, money: 12000.0}, {name: 李四, language: Java, money: 200.0}, {name: 王五, language: C++, money: 5000.0}, {name: 钱六, language: VB, money: 3000.0}]
after sort: [{name: 李四, language: Java, money: 200.0}, {name: 钱六, language: VB, money: 3000.0}, {name: 王五, language: C++, money: 5000.0}, {name: 张三, language: C, money: 12000.0}]
发表评论
-
jdom增删改查
2009-07-01 11:36 927package bean; import java.io. ... -
JDOM读写XML文件
2009-07-01 11:34 1020package com.test; import org.jd ... -
启动java程序时需要很多jar包的解决办法
2008-06-02 16:25 1947以下方法在 WINDOWS XP 和2003SERVER上测试 ... -
java解析xml字符串
2008-05-28 18:18 5652public static String[] pars ... -
java处理中文加密解密
2008-05-26 10:58 3668public class EscapeUnescapeUtil ... -
设置eclipse自动生成的author等注释
2008-05-13 15:55 4069设置eclipse自动生成的author等注释2007-08- ... -
java移动文件
2008-05-12 16:32 1822下面这个方法是文件移动方法,其中如果去掉红色部分代码,则是文件 ... -
jdbc addbatch
2008-05-04 11:15 2951Statememt PreparedStatement 都有a ...
相关推荐
本压缩包“java常用代码”集合了一系列基础到进阶的Java代码示例,涵盖了多个关键领域,有助于初学者快速掌握Java编程的核心概念。 1. **遗产算法**:在Java中,继承是面向对象特性之一,它允许一个类(子类)继承...
JAVA常用代码块 JAVA常用代码块 JAVA常用代码块 JAVA常用代码块 JAVA常用代码块
java常用代码方法很适合初学者和刚刚参加工作的程序员,里面包含了常用正则表达式、公共日期类、串口驱动、各种数据库连接、公交换乘算法、 列出某文件夹及其子文件夹下面的文件,并可根据扩展名过滤等等很多有用的...
以下是对标题“java常用代码的集合”和描述中提及的知识点的详细解释,以及与标签相关的具体代码示例。 1. ISBN验证: ISBN(国际标准书号)是用于唯一标识书籍的编码。在Java中,我们可以编写一个方法来验证一个...
在"Java常用代码整理"这个主题中,我们可以探讨多个Java编程中的关键知识点,包括基础语法、面向对象特性、异常处理、集合框架、IO流、多线程、网络编程以及实用工具类等。 1. **基础语法**:Java的基础语法包括...
在Java中,"常用代码"通常涵盖了许多核心概念和技术,包括但不限于数据类型、控制结构、类与对象、异常处理、集合框架、输入/输出(I/O)、多线程以及正则表达式等。本篇将主要探讨正则表达式这一主题,它是Java中处理...
"Java常用代码大全.zip"这个压缩包很可能包含了各种常见的Java编程示例和实用代码片段,帮助开发者快速解决日常开发中遇到的问题。这个文档,"Java常用代码大全.doc",可能是一个详细的指南,涵盖了Java开发中的各种...
此外,文件可能还会包含异常处理(try-catch-finally语句)、接口和抽象类的使用示例,这些都是Java中重要的错误管理和设计模式的基础。 在集合框架方面,文件可能涵盖了ArrayList、LinkedList、HashMap、HashSet等...
总之,"Java常用代码全集.zip"文档是一个宝贵的资源,它可以帮助开发者巩固基础,学习新技巧,并解决实际开发中遇到的问题。通过深入研究并实践这些代码,开发者可以提升自己的编程技能,更有效地编写出高质量的Java...
这个"java常用代码工具包"中的"新建文件夹"可能包含了上述功能的实现代码或配置文件。使用时,开发者需要将这些代码导入到项目中,通过引用和调用来利用这些工具类。同时,阅读和理解这些工具类的源代码也是一种学习...
1. **输出语句**:`System.out.println()` 是Java中最基本的输出语句,用于在控制台打印信息。 2. **变量声明**:`int num = 0;` 用来声明并初始化一个整型变量。 3. **条件判断**:`if (num > 0) {...}` 用于进行...
4. **数据库连接**:在Java中,我们通常使用JDBC(Java Database Connectivity)来与数据库进行交互。JDBC提供了一组接口和类,使得Java程序能够执行SQL语句、处理结果集等。首先需要加载驱动(如Class.forName())...