JDK命令行

JDKVars.bat

@echo off

set JAVA_HOME=D:\Java\jdk1.6.0_22
set MVN_HOME=D:\Java\apache-maven-3.0.3
set ANT_HOME=D:\Java\apache-ant-1.8.1
set PATH=%JAVA_HOME%\bin;%MVN_HOME%\bin;%ANT_HOME%\bin;%PATH%
set CLASSPATH=.;%JAVA_HOME%\lib\tools.jar

color 02
title Java_1.6_u22
echo Java_Enviroment_OK!

echo on

cmd快捷启动方式

%ComSpec% /K D:\Java\jdk1.6.0_22\bin\JDKVars.bat

1和0

下面语句会出错吗?
你感觉哪一句不会出错呢?

public class Test01
{
    public static void main(String[] atgs)
    {
        System.out.println(1.0/0.0);
        System.out.println(1.0/0);
        System.out.println(1/0.0);
        System.out.println(1/0);
        System.out.println(0/0);
    }
}

你对java中的无穷大和非数了解吗?

动态创建div

function creatediv(objname)
{
    var objdiv = document.createElement("div");
    objdiv.id =objname;
    objdiv.innerHTML=objname;
    objdiv.style.top = 100;
    objdiv.style.left = 100;
    objdiv.style.background = '#FFFF00';
    objdiv.style.visibility = 'visible';
    objdiv.style.width = 500;
    objdiv.style.height = 500;
    
    document.body.appendChild(objdiv);
    document.getElementById(objname).onmouseover = function()
    {
        alert(this.id);
    };
}

上面的代码是没什么问题的,但有一种情况,就是FF提交表单时,
这样创建控件,会受安全限制,不一定好用的。

VC判断UTF-8与ANSI

大家知道,如果只有英文的话,UTF-8与ANSI是一样的
但有了中文以后,情况就很不一样了,
在ANSI中,比如GBK,中文占两字节,
在UTF-8中,中文占三字节,
当中英文混合时,情况就更复杂一些了。
下面一段是在以前项目中,先判断是UTF-8还是GBK然后转为UNICODE的代码

//要判断内容
char *s1="....";
//字符编码
UINT CodePage=0;
//字符串长度
int nLen=strlen(s1);

//判断是否为UTF-8
//至少要3字节
if(nLen>=3)
{
    unsigned char U1,U2,U3;
    int nNow=0;
    while(nNow<nLen)
    {
        U1=(unsigned)s1&#91;nNow&#93;;
        if((U1&0x80)==0x80)
        {
            //中文字符,则要三个字符
            if(nLen>nNow+2)
            {
                U2=(unsigned)s1[nNow+1];
                U3=(unsigned)s1[nNow+2];
                //中文三字节为0xE0 0xC0 0xC0
                if(((U1&0xE0)==0XE0) && ((U2&0xC0)==0x80) && ((U3&0xC0)==0x80))
                {
                    //有可能是UTF-8
                    CodePage=65001;
                    nNow=nNow+3;
                }
                else
                {
                    //不是UTF-8
                    CodePage=0;
                    break;
                }
            }
            else
            {
                //不是UTF-8
                CodePage=0;
                break;
            }
        }
        else
        {
            //非中文字符
            nNow++;
        }
    }
}

DWORD dwNum;
dwNum=MultiByteToWideChar(CodePage,0,s1,-1,NULL,0);
if(dwNum)
{
    wchar_t *pwText;
    pwText=new TCHAR[dwNum];
    if(pwText)
    {
        MultiByteToWideChar(CodePage,0,s1,-1,pwText,dwNum);
    }
    szPatientName=pwText;
    delete []pwText;
}

Jboss4.2.3GA与Asix2通讯失败

两个项目,分别使用Jboss和Asix2开发了webservice及其client,通讯走的是soap1.2

最后用客户端访问服务端时,却发现Jboss的客户端,可以分别访问Jboss及Asix2的服务端
但Asix2的服务端却只能访问Asix2的服务端,访问Jboss的服务端时,会报下面的错误:
Transport level information does not match with SOAP Message namespace URI

经过跟踪Asix2的源码,并截取双方的通信内容,发现,
原来JBoss4.2.3GA返回消息时,没有按照soap1.2标准,返回正确的HTTP消息头
在Content-Type中,start-info无论如何只会填写text/xml,这和SOAP1.2要求的application/soap+xml是不一致的。

在JBoss网站发现,JBoss4.3以上的版本修复了这个问题,但4.3以上是收费的。
咋办啊,只好自己打补丁咯
下载jbossws-core源码包,按照下面的补丁内容,改好文件
https://issues.jboss.org/secure/attachment/12323928/JBWS-2419_patch.txt
重新打好jar包,替换过去
搞定:)

cpp中删除一个静态局部变量?

大家都知道,static变量,内容在堆中分配的,new来的对象也是堆中分配的,
那么,如果在cpp中删除一个静态局部变量,后果是什么?
编译错误?还是运行错误?
比如下面的aTest函数,能运行吗?

#include <iostream>
using namespace std;

int aTest()
{
	static int a=0;
	int *b=&a;
	delete b;
	return a++;
}

int main(int argc,char** argv)
{
	for(int i=0;i<100;i++)
	{
		cout<<aTest()<<endl;
	}
	return 0;
}

事实是,
在gcc下,无论debug还是release,都没有问题,
在vc下,debug会报assert错误,release没有问题。
那cpp下,内存管理是谁做的?编译器为什么让它过去了呢?
其实,个人感觉,也就是上面的内存操作并不多,复杂情况下,早就挂了。

c语言中,用函数实现sizeof操作

大家都知道,c语言中sizeof是个操作符,在编译阶段已经变成了数值
要用函数实现sizeof的话,和sizeof操作符是会有一定区别的
这里,我只给出了一个很简单的例子,
用指针操作实现sizeof(double)的功能

double a=0.0;
int sz=(int)&((&a)[1])-(int)&a;

Java Cheat List

1.控制台读入字符串

String pair ="";
try
{
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    pair = br.readLine(); 
}
catch(IOException ex)
{
    ex.printStackTrace();
}

2.控制台读入整数

int a=0;
try{
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    a=Integer.parseInt(br.readLine());
    b=Integer.parseInt(br.readLine());
    c=Integer.parseInt(br.readLine());
}catch(IOException ex){
    ex.printStackTrace();
}

3.计算某年某月为星期几

int y=2000,m=1,d=1;
Calendar aCalendar=Calendar.getInstance();
aCalendar.set(Calendar.YEAR,y);
aCalendar.set(Calendar.MONTH, m-1);
aCalendar.set(Calendar.DAY_OF_MONTH, d);
//注意,x为1-7,默认1为星期日
int x=aCalendar.get(Calendar.DAY_OF_WEEK);

IOS Cheat List

1.页面切换

//页面切换
MyViewController *controller = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];
controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical;

[self presentModalViewController:controller animated:YES];
//页面返回
[self dismissModalViewControllerAnimated:YES];

2.获取AppDelegate

MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];

3.获取配置信息

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *MyTextValue = [defaults objectForKey:TEXT_VALUE_KEY];

BOOL MyBoolValue = [defaults boolForKey:BOOL_VALUE_KEY];

4.读取plist

//本地
NSString *path = [[NSBundle mainBundle] pathForResource:@"filename" ofType:@"plist"];
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
//远程
NSStrng * strURL = [[NSString alloc] initWithFormat:@"FORMAT",.....];
NSLog(@"%@",strURL);
NSURL *plistURL = [NSURL URLWithString:strURL];
NSDictionary *myDict = [[NSDictionary alloc] initWithContentsOfURL:plistURL];

5.关闭键盘

[myControl resignFirstResponder];

6.内置功能调用

//网站
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://网址"]];
//打电话
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://电话号码"]];
//发送邮件
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto://邮箱地址"]];
//发送短信
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms://电话号码"]];

7.导航

//左按钮
UIBarButtonItem *lBtn = [[UIBarButtonItem alloc] initWithTitle:@"名称" style:UIBarButtonItemStylePlain target:self action:@selector(lBtnPressed:)];
self.navigationItem.leftBarButtonItem = lBtn;
 [lBtn release];

//入栈
MyViewController *nextController = [[MyViewController alloc] init];
nextController.title = @"MyViewControllerName";
[self.navigationController pushViewController:nextController animated:YES];

//出栈
[self.navigationController popViewControllerAnimated:YES];

8.返回一个TableViewCell

static NSString *tableViewCellIdentifier = @"MyCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableViewCellIdentifier];
if(cell==nil)
{
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableViewCellIdentifier] autorelease];
}
cell.textLabel.text = @"TEXT"
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;

9.UIAlertView异步的哟

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"HI" message:@"Hello" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
[alert show];
[alert release];

10.注册推送通知

[[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert |UIRemoteNotificationTypeSound];

11.注册网络状态

[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(reachabilityChanged:) name: KEY_ATS_NETWORK_CHANGE_NOTIFICATION object: nil];

NSString* token = [deviceToken description];
deviceTokenId = [[[token stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]] stringByReplacingOccurrencesOfString:@" "withString:@""] retain];

PushedMsg *newMsg = [[PushedMsg alloc] init];
newMsg.msgContent = [[[userInfo objectForKey:@"aps"] objectForKey:@"alert"] copy];

12.本地通知

//增加
UILocalNotification *notification=[[UILocalNotification alloc] init];
NSDate *now1=[NSDate date];
notification.timeZone=[NSTimeZone defaultTimeZone];
notification.repeatInterval=NSDayCalendarUnit;
notification.applicationIconBadgeNumber = 1;
notification.alertAction = NSLocalizedString(@"显示", nil);

notification.fireDate=[now1 dateByAddingTimeInterval:10];
notification.alertBody=@"通知";

[notification setSoundName:UILocalNotificationDefaultSoundName];
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
[NSString stringWithFormat:@"%d",myswitch.tag],KEY_ATS_LOCAL_NOTIFICATION, nil];
[notification setUserInfo:dict];
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
//取消
UILocalNotification *myUILocalNotification=[myArray objectAtIndex:i];
if ([[[myUILocalNotification userInfo] objectForKey:KEY_ATS_LOCAL_NOTIFICATION] intValue]==myswitch.tag)
{
[[UIApplication sharedApplication] cancelLocalNotification:myUILocalNotification];
}

13.计算MD5

#import <CommonCrypto/CommonDigest.h>

//生成指定字符串的MD5
+ (NSString*)CalcMD5:(NSString *)InString
{
    //生成MD5
    const char *ptr = [InString UTF8String];
    unsigned char md5Buffer[CC_MD5_DIGEST_LENGTH];
    CC_MD5(ptr, strlen(ptr), md5Buffer);
   
    //转为NSString
    NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
    {
        &#91;output appendFormat:@"%02x",md5Buffer&#91;i&#93;&#93;;
    }
    return output;
}
&#91;/code&#93;

<strong>14.获取位置</strong>
[code lang="objc"]
CLLocationManager* locationManager = [[CLLocationManager alloc]init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];

15.本地化

NSLocalizedString(@"KEY",@"DEFAULT");

16.获取图片

UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:picker animated:YES];
[picker release];

17.加速器

UIAccelerometer *accel = [UIAccelerometer sharedAccelerometer];
accel.delegate =self;
accel.updateInterval = UPDATE_INTERVAL;

18.声音

NSString *path=[[NSBundle mainBundle] pathForResource:@"文件名" ofType:@"wav"];
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path], &soundID);

AudioServicesPlaySystemSound(soundID);