开发中经常用到的一些例子(mark帖) 用commons-fileupload-1.2 实现文件上传 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 Eclipse3.2+Tomcat5.5.17+Oracle9配置 输入一个正整数: BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); boolean b = true; int re = 0; try { while (b) { System.out.print("请输入一个正整数:"); String s = (String) br.readLine(); System.out.println("s="+s); for (int j = 0; j < s.length(); j++) { if (!(s.charAt(j) >= 48 && s.charAt(j) <= 57)) { System.out.println("您输入的不是正整数!请重新输入."); b = true; break; } else { b = false; } } if (!b) { int i = Integer.parseInt(s); if (i == 0) { System.out.println("您输入的不是正整数!请重新输入."); break; } System.out.println("您输入的是:" + i); } } } catch (Exception e) { e.printStackTrace(); } jsp:include java中调用exe文件(如果调用批处理文件bat,先转换成exe文件) try { String command = "d:/t.exe"; Process child = Runtime.getRuntime().exec(command); System.out.println("child="+child); } catch (IOException e) { e.printStackTrace(); } 按照行读取文本文件: int nLineCount = 0;//行数 File file = new File("d:/language.txt"); BufferedReader in = new BufferedReader(new FileReader(file)); String strLine = ""; StringBuffer strBuffer = new StringBuffer(1000); while ((strLine = in.readLine()) != null) { strBuffer.append(strLine); strBuffer.append("^^"); ++nLineCount; } //最终结果保存在strResult中,第一行在strResult[0],第一行第一列在strResult[0][0] String[][] strResult = new String[nLineCount][]; String[] strTemp = (strBuffer.toString()).split("\\^\\^"); for (int i = 0; i < strTemp.length; ++i) for (int j = 0; j < strTemp[i].length(); ++j) { strResult[i] = strTemp[i].split(","); } for (int i = 0; i < strResult.length; ++i) for (int j = 0; j < strResult[i].length; ++j) { strResult[i][j] = strResult[i][j].replaceAll("\"", ""); System.out.println(strResult[i][j]); } MD5加密算法: public final static String MD5(String s) { char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; try { byte[] btInput = s.getBytes(); MessageDigest mdInst = MessageDigest.getInstance("MD5"); mdInst.update(btInput); byte[] md = mdInst.digest(); int j = md.length; char str[] = new char[j * 2]; int k = 0; for (int i = 0; i < j; i++) { byte byte0 = md[i]; str[k++] = hexDigits[byte0 >>> 4 & 0xf]; str[k++] = hexDigits[byte0 & 0xf]; } return new String(str); } catch (Exception e) { e.printStackTrace(); return null; } } 金额转换/** * 金额转换处理类,a.小写转大写;(b.大写转小写) * <ul> * <li>处理能力:</li> * <li>a.整数部分:9999999999999999(16位长,仟万亿级)</li> * <li>b.小数部分:3位(可扩展,单位?),多于3位舍去(不四舍五入)</li> * <li>可无限扩展,方法:IUNIT后依次递加合适的单位,并处理0出现在关键位置</li> * </ul> * @author JaDo * @version 2.1 */public class CurrencyUtil { /** 大写数字 */ private static final String[] NUMBERS = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" }; /** 整数部分的单位 */ private static final String[] IUNIT = { "元", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "万", "拾", "佰", "仟" }; /** 小数部分的单位 */ private static final String[] DUNIT = { "角", "分", "厘" }; /** 整数部分长度限制,仟万亿级 */ private static int LIMIT = 16; /** * 得到中文金额。 * @return 中文金额 */ public static String toChinese(String str) { // 去掉"," String number = getFormatNum(str); // 不是金额,直接返回 if (!isMoney(number)) { return number; } // 整数部分数字 String integerStr; // 小数部分数字 String decimalStr; // 初始化:格式数字;分离整数部分和小数部分;确定长度 if (number.indexOf(".") > 0) { integerStr = number.substring(0, number.indexOf(".")); decimalStr = number.substring(number.indexOf(".") + 1); } else if (number.indexOf(".") == 0) { integerStr = ""; decimalStr = number.substring(1); } else { integerStr = number; decimalStr = ""; } // integerStr去掉首0,不必去掉decimalStr的尾0(超出部分舍去) if (!integerStr.equals("")) { integerStr = Long.toString(Long.parseLong(integerStr)); if (integerStr.equals("0")) { integerStr = ""; } } // overflow超出处理能力,直接返回 if (integerStr.length() > LIMIT) { System.out.println(number + ":超出处理能力"); return number; } // 整数部分数字 int[] integers = toArray(integerStr); // 设置万单位 boolean isMust5 = isMust5(integerStr); // 小数部分数字 int[] decimals = toArray(decimalStr); // 返回大写金额 return getChineseInteger(integers, isMust5) + getChineseDecimal(decimals); } /** * 整数部分和小数部分转换为数组,从高位至低位 */ private static int[] toArray(String number) { int[] array = new int[number.length()]; for (int i = 0; i < number.length(); i++) { array[i] = Integer.parseInt(number.substring(i, i + 1)); } return array; } /** * 得到中文金额的整数部分。 * @return 整数部分 */ private static String getChineseInteger(int[] integers, boolean isMust5) { StringBuffer chineseInteger = new StringBuffer(""); String key = ""; int length = integers.length; // int lastNum = 1; // int lengthOfZero = 0; for (int ii = 0; ii < length; ii++) { // 1234(万)5678(亿)9012(万)3456(元) // 0出现在关键位置:元、万、亿、万亿、兆、万兆、亿兆、万亿兆 // 处理顺序:不限,只跟具体位置有关,不必从高位到低位 // 特殊情况:10(拾元、壹拾元、壹拾万元、拾万元) if (integers[ii] == 0) { // 万(亿)(必填) if ((length - ii) == 13) key = IUNIT[4]; // 亿(必填) else if ((length - ii) == 9) key = IUNIT[8]; // 万(不必填) else if ((length - ii) == 5 && isMust5) key = IUNIT[4]; // 元(必填) else if ((length - ii) == 1) key = IUNIT[0]; // 0遇非0时补零,不包含最后一位 if ((length - ii) > 1 && integers[ii + 1] != 0) key += NUMBERS[0]; // 其余情况 else key += ""; // if(ii==5) break; } chineseInteger.append(integers[ii] == 0 ? key : (NUMBERS[integers[ii]] + IUNIT[length - ii - 1])); key = ""; } return chineseInteger.toString(); } /** * 得到中文金额的小数部分。 * @return 小数部分 */ private static String getChineseDecimal(int[] decimals) { StringBuffer chineseDecimal = new StringBuffer(""); for (int ii = 0; ii < decimals.length; ii++) { // 舍去3位小数之后的 if (ii == 3) break; chineseDecimal.append(decimals[ii] == 0 ? "" : (NUMBERS[decimals[ii]] + DUNIT[ii])); } return chineseDecimal.toString(); } /** * 格式化数字:去掉","。 * @param num 数字 * @return 格式化后的数字 */ private static String getFormatNum(String number) { return number.replace( ",", ""); } /** * 判断第5位数字的单位"万"是否应加。 * @return true是false否 */ private static boolean isMust5(String integerStr) { int length = integerStr.length(); if (length > 4) { String subInteger = ""; if (length > 8) { // 取得从低位数,第5到第8位的字串 subInteger = integerStr.substring(length - 8, length - 4); } else { subInteger = integerStr.substring(0, length - 4); } return Integer.parseInt(subInteger) > 0; } else { return false; } } /** * 判断金额字符串是否合法 不合法:a.包含多于一个".";b.包含"."、","和数字以外的字符;c.¥/$?。 * @return true是false否 */ private static boolean isMoney(String number) { try { Double.parseDouble(number); } catch (Exception ex) { System.out.println(number + "不是合法金额"); return false; } return true; }//以下为测试代码,可以忽略 public static void main(String[] args) { String num = "1234567890123456.43"; System.out.println(num + "-" + CurrencyUtil.toChinese(num)); num = "0.979"; System.out.println(num + "-" + CurrencyUtil.toChinese(num)); num = "09999999999999999"; System.out.println(num + "-" + CurrencyUtil.toChinese(num)); num = "10,001,000.09"; System.out.println(num + "-" + CurrencyUtil.toChinese(num)); num = "01.107000"; System.out.println(num + "-" + CurrencyUtil.toChinese(num)); num = "1.0778879"; System.out.println(num + "-" + CurrencyUtil.toChinese(num)); num = "1,002,002,020,000,201.897675434"; System.out.println(num + "-" + CurrencyUtil.toChinese(num)); num = "201000000"; System.out.println(num + "-" + CurrencyUtil.toChinese(num)); }} Hibernate Annotation中的关系定义 SSH整合经常遇到的错误解决办法 弹出窗口的命令格式:“window.open(‘URLStr’, ‘WindowName’, ‘Property’);”,其中: 1、window.open命令用于在网页上弹出一个新窗口。 2、URLStr:弹出窗口所显示的页面 3、WindowName:弹出窗口的名称,可以任意指定,也可以用’’来代替 4、Property:用于控制弹出窗口显示的属性,具体可控制的参数有: 4.1、Toolbar:是否显示浏览器工具栏,yes为显示,no为不显示 4.2、Location:是否显示浏览器地址栏,yes为显示,no为不显示 4.3、Directories:是否显示目录按钮,yes为显示,no为不显示 4.4、Status:是否显示状态栏,yes为显示,no为不显示 4.5、Menubar:是否显示菜单条,yes为显示,no为不显示 4.6、Scrollbars:是否激活水平和垂直流动条,yes为显示,no为不显示 4.7、Resizable:是否可以改变窗口大小,yes为显示,no为不显示 4.8、Width:指定窗口的宽度,以像素为单位 4.9、Height:指定窗口的高度,以像素为单位 4.10、Left:指定窗口距屏幕左端的距离,以像素为单位 4.11、Top:指定窗口距屏幕顶端的距离,以像素为单位 4.12、screenX:等同于Left属性 4.13、screenY:等同于Top属性 4.14、fullscreen:全屏 4.15、channelmode:F11化 4.16、directories:带有收藏链接工具栏 想学习java,学过C++ ,C#,怎么入门啊 单选按纽Checkbox的问题 请教 System.out.println("test " + s1=="Java"); Is this a bug? jode反编译工具的问题 JAVA中是否有象C++那种函数的默认参数的用法 Vector可以作为二维数组吗? 高手进 不是高手也进来(谢谢啊!!) 字节和字符的概念,晕菜了 送分问题!!!!!!!!!分必给!!!!!!!!!!! 怎么传递数组里的元素给另一个数组? 学习的JAVA的困惑 请大家帮我看看一个javadoc的问题
boolean b = true;
int re = 0;
try {
while (b) {
System.out.print("请输入一个正整数:");
String s = (String) br.readLine();
System.out.println("s="+s);
for (int j = 0; j < s.length(); j++) {
if (!(s.charAt(j) >= 48 && s.charAt(j) <= 57)) {
System.out.println("您输入的不是正整数!请重新输入.");
b = true;
break;
} else {
b = false;
}
}
if (!b) {
int i = Integer.parseInt(s);
if (i == 0) {
System.out.println("您输入的不是正整数!请重新输入.");
break;
}
System.out.println("您输入的是:" + i);
}
}
}
catch (Exception e) {
e.printStackTrace();
}
String command = "d:/t.exe";
Process child = Runtime.getRuntime().exec(command);
System.out.println("child="+child);
} catch (IOException e)
{
e.printStackTrace();
}
File file = new File("d:/language.txt");
BufferedReader in = new BufferedReader(new FileReader(file));
String strLine = "";
StringBuffer strBuffer = new StringBuffer(1000);
while ((strLine = in.readLine()) != null) {
strBuffer.append(strLine);
strBuffer.append("^^");
++nLineCount;
}
//最终结果保存在strResult中,第一行在strResult[0],第一行第一列在strResult[0][0]
String[][] strResult = new String[nLineCount][];
String[] strTemp = (strBuffer.toString()).split("\\^\\^");
for (int i = 0; i < strTemp.length; ++i)
for (int j = 0; j < strTemp[i].length(); ++j) {
strResult[i] = strTemp[i].split(",");
}
for (int i = 0; i < strResult.length; ++i)
for (int j = 0; j < strResult[i].length; ++j) {
strResult[i][j] = strResult[i][j].replaceAll("\"", "");
System.out.println(strResult[i][j]); }
{
char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F' };
try {
byte[] btInput = s.getBytes();
MessageDigest mdInst = MessageDigest.getInstance("MD5");
mdInst.update(btInput);
byte[] md = mdInst.digest();
int j = md.length;
char str[] = new char[j * 2];
int k = 0;
for (int i = 0; i < j; i++) {
byte byte0 = md[i];
str[k++] = hexDigits[byte0 >>> 4 & 0xf];
str[k++] = hexDigits[byte0 & 0xf];
}
return new String(str);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
* 金额转换处理类,a.小写转大写;(b.大写转小写)
* <ul>
* <li>处理能力:</li>
* <li>a.整数部分:9999999999999999(16位长,仟万亿级)</li>
* <li>b.小数部分:3位(可扩展,单位?),多于3位舍去(不四舍五入)</li>
* <li>可无限扩展,方法:IUNIT后依次递加合适的单位,并处理0出现在关键位置</li>
* </ul>
* @author JaDo
* @version 2.1
*/
public class CurrencyUtil {
/** 大写数字 */
private static final String[] NUMBERS = { "零", "壹", "贰", "叁", "肆", "伍", "陆",
"柒", "捌", "玖" }; /** 整数部分的单位 */
private static final String[] IUNIT = { "元", "拾", "佰", "仟", "万", "拾", "佰",
"仟", "亿", "拾", "佰", "仟", "万", "拾", "佰", "仟" }; /** 小数部分的单位 */
private static final String[] DUNIT = { "角", "分", "厘" }; /** 整数部分长度限制,仟万亿级 */
private static int LIMIT = 16;
/**
* 得到中文金额。
* @return 中文金额
*/
public static String toChinese(String str) {
// 去掉","
String number = getFormatNum(str);
// 不是金额,直接返回
if (!isMoney(number)) {
return number;
}
// 整数部分数字
String integerStr;
// 小数部分数字
String decimalStr; // 初始化:格式数字;分离整数部分和小数部分;确定长度
if (number.indexOf(".") > 0) {
integerStr = number.substring(0, number.indexOf("."));
decimalStr = number.substring(number.indexOf(".") + 1);
} else if (number.indexOf(".") == 0) {
integerStr = "";
decimalStr = number.substring(1);
} else {
integerStr = number;
decimalStr = "";
} // integerStr去掉首0,不必去掉decimalStr的尾0(超出部分舍去)
if (!integerStr.equals("")) {
integerStr = Long.toString(Long.parseLong(integerStr));
if (integerStr.equals("0")) {
integerStr = "";
}
} // overflow超出处理能力,直接返回
if (integerStr.length() > LIMIT) {
System.out.println(number + ":超出处理能力");
return number;
}
// 整数部分数字
int[] integers = toArray(integerStr);
// 设置万单位
boolean isMust5 = isMust5(integerStr);
// 小数部分数字
int[] decimals = toArray(decimalStr); // 返回大写金额
return getChineseInteger(integers, isMust5) + getChineseDecimal(decimals);
} /**
* 整数部分和小数部分转换为数组,从高位至低位
*/
private static int[] toArray(String number) {
int[] array = new int[number.length()];
for (int i = 0; i < number.length(); i++) {
array[i] = Integer.parseInt(number.substring(i, i + 1));
}
return array;
} /**
* 得到中文金额的整数部分。
* @return 整数部分
*/
private static String getChineseInteger(int[] integers, boolean isMust5) {
StringBuffer chineseInteger = new StringBuffer("");
String key = "";
int length = integers.length;
// int lastNum = 1;
// int lengthOfZero = 0;
for (int ii = 0; ii < length; ii++) {
// 1234(万)5678(亿)9012(万)3456(元)
// 0出现在关键位置:元、万、亿、万亿、兆、万兆、亿兆、万亿兆
// 处理顺序:不限,只跟具体位置有关,不必从高位到低位
// 特殊情况:10(拾元、壹拾元、壹拾万元、拾万元)
if (integers[ii] == 0) {
// 万(亿)(必填)
if ((length - ii) == 13)
key = IUNIT[4];
// 亿(必填)
else if ((length - ii) == 9)
key = IUNIT[8];
// 万(不必填)
else if ((length - ii) == 5 && isMust5)
key = IUNIT[4];
// 元(必填)
else if ((length - ii) == 1)
key = IUNIT[0]; // 0遇非0时补零,不包含最后一位
if ((length - ii) > 1 && integers[ii + 1] != 0)
key += NUMBERS[0];
// 其余情况
else
key += "";
// if(ii==5) break;
}
chineseInteger.append(integers[ii] == 0 ? key
: (NUMBERS[integers[ii]] + IUNIT[length - ii - 1]));
key = "";
}
return chineseInteger.toString();
} /**
* 得到中文金额的小数部分。
* @return 小数部分
*/
private static String getChineseDecimal(int[] decimals) {
StringBuffer chineseDecimal = new StringBuffer("");
for (int ii = 0; ii < decimals.length; ii++) {
// 舍去3位小数之后的
if (ii == 3)
break;
chineseDecimal.append(decimals[ii] == 0 ? ""
: (NUMBERS[decimals[ii]] + DUNIT[ii]));
}
return chineseDecimal.toString();
} /**
* 格式化数字:去掉","。
* @param num 数字
* @return 格式化后的数字
*/
private static String getFormatNum(String number) {
return number.replace( ",", "");
} /**
* 判断第5位数字的单位"万"是否应加。
* @return true是false否
*/
private static boolean isMust5(String integerStr) {
int length = integerStr.length();
if (length > 4) {
String subInteger = "";
if (length > 8) {
// 取得从低位数,第5到第8位的字串
subInteger = integerStr.substring(length - 8, length - 4);
} else {
subInteger = integerStr.substring(0, length - 4);
}
return Integer.parseInt(subInteger) > 0;
} else {
return false;
}
} /**
* 判断金额字符串是否合法 不合法:a.包含多于一个".";b.包含"."、","和数字以外的字符;c.¥/$?。
* @return true是false否
*/
private static boolean isMoney(String number) {
try {
Double.parseDouble(number);
} catch (Exception ex) {
System.out.println(number + "不是合法金额");
return false;
}
return true;
}//以下为测试代码,可以忽略
public static void main(String[] args) {
String num = "1234567890123456.43";
System.out.println(num + "-" + CurrencyUtil.toChinese(num));
num = "0.979";
System.out.println(num + "-" + CurrencyUtil.toChinese(num));
num = "09999999999999999";
System.out.println(num + "-" + CurrencyUtil.toChinese(num));
num = "10,001,000.09";
System.out.println(num + "-" + CurrencyUtil.toChinese(num));
num = "01.107000";
System.out.println(num + "-" + CurrencyUtil.toChinese(num));
num = "1.0778879";
System.out.println(num + "-" + CurrencyUtil.toChinese(num));
num = "1,002,002,020,000,201.897675434";
System.out.println(num + "-" + CurrencyUtil.toChinese(num));
num = "201000000";
System.out.println(num + "-" + CurrencyUtil.toChinese(num));
}
}
1、window.open命令用于在网页上弹出一个新窗口。
2、URLStr:弹出窗口所显示的页面
3、WindowName:弹出窗口的名称,可以任意指定,也可以用’’来代替
4、Property:用于控制弹出窗口显示的属性,具体可控制的参数有:
4.1、Toolbar:是否显示浏览器工具栏,yes为显示,no为不显示
4.2、Location:是否显示浏览器地址栏,yes为显示,no为不显示
4.3、Directories:是否显示目录按钮,yes为显示,no为不显示
4.4、Status:是否显示状态栏,yes为显示,no为不显示
4.5、Menubar:是否显示菜单条,yes为显示,no为不显示
4.6、Scrollbars:是否激活水平和垂直流动条,yes为显示,no为不显示
4.7、Resizable:是否可以改变窗口大小,yes为显示,no为不显示
4.8、Width:指定窗口的宽度,以像素为单位
4.9、Height:指定窗口的高度,以像素为单位
4.10、Left:指定窗口距屏幕左端的距离,以像素为单位
4.11、Top:指定窗口距屏幕顶端的距离,以像素为单位
4.12、screenX:等同于Left属性
4.13、screenY:等同于Top属性
4.14、fullscreen:全屏
4.15、channelmode:F11化
4.16、directories:带有收藏链接工具栏