一些java的常识:
1. 关于switch 语句
> > > java6中switch不支持字符串,java7中支持字符串,即 int, char, enum,string
2. 关于端口
> > > 0-1023之间的端口用于一些知名网络应用和服务,一边编写普通网络应用程序应用1024以上的端口,以免和已知系统服务冲突
3. 关于 内部类和外部类
> > > 从语义和编程来理解,内部类使用外部类的局部变量,是希望在内部类中更改该局部变量的值或引用,但在实际编译中,由于内部类只是一个普通类,所以外部类的局部变量是在内部类的构造函数里赋值给内部类的private变量的。即外部类的局部变量和内部类的同名变量只是两个指向同一个对象的引用。如果不用final限定局部变量,则内部类的该private变量也会是非final,那么在内部类中改变该引用对象,是无法影响外部类的该局部变量的,只会使人产生困惑。因此,必须用fina限定需要在内部类使用的外部类的局部变量。
4. 关于final
> > > final指不能再重新赋值或指向其他对象,eg:
final StringBuffer sb=new StringBuffer ("abc");
sb=new StringBuffer("e");//X,不能指向其他变量
sb.append("e"); //fine, sb指向的对象本身可以改变。
5. 关于 调用系统程序 : 使用Runtime函数
> > > Process process=Runtime.getRuntime().exec("ping "+ip+" -w 280 -n 1"); //调用cmd执行ping 命令,ps: -w 280 为设置等待超时时间, -n 1是发送的回显请求书,其中1 是指发送1个request 包
> > > Process process=Runtime.getRuntime().exec("notepad"); //调用notepad
> > > Runtime r=Runtime.getRuntime();
r.gc(); //调用垃圾回收器
r.totalMemory (); //内存
r.freeMemory(); //可用内存
6. server/client 通信使用SocketServer和Socket
> > > for server: SocketServer server=new SocketServer(int port);
Socket socket=server.accept(); //accept() will block current thread to execute until client responds
> > > for client: Socket socket= new Socket(String IP,int port);
> > > server &client 各占一个线程,server必须先启动;
> > > 输入输出流使用socket.getOutputSream()/socket.getInputSream();转化为BufferedReader type后,需要使用while(true)无限循环监听输入输出流以保证所有数据都有被接 收,eg: 服务器端保持监听port上的所有输入并打印:
while (true){
System.out.println("client msg is "+reader.readLine());
}
> > > 客户端socket获取服务器的IP和端口:
InetAddress inet= socket.getInetAddress();
String serverIP=inet.getHostAddress();
String serverPort=socket.getPort();
> > >客户端socket获取本地IP和端口
InetAddress inet=socket.getLocalAddress();
String localIP=inet.getHostAddress();
String localPort=socket.getLocalPort();
7. server 设置 timeout
> > > try { ServerSocket server=new ServerSocket(int port);
server.setSoTimeout(10000); //10秒内无客户端的连接即激活SocketTimeoutException
Socket socket;
while(true){ ...socket=server.accpet();...}
}catch(SocketTimeoutException e){ JOptionPane.showMessageDialog(null,"timeout");}
8. 获取IP或者主机信息用InetAddress类
> > > 本地IP和主机名: InetAddress inet=InetAddress.getLocalHost();
String localIP=inet.getHostAddress();
String localHost=inet.getHostName();
> > > 获取某个IP的InetAddress: InetAddress inet=InetAddress.getByName("192.168.1.1");
9. 设定组播使用MulticastSocket和DatagramPacket类
> > > 发送端:InetAddress inet=InetAddress.getByName("224.255.10.0"); // 设定组播地址,发送端和接收端都需要加入此地址
MulticastSocket socket=new MulticastSocket();
socket.setTimetoLive(1); //指定发送范围为本地网络
socket.joinGroup(inet); //加入组播地址
//建立发送DatagramPacket
DatagramPacket packet=new DatagramPacket(byte[] data, data.length, inet,port);
//发送数据
while(true){ socket.send(packet); sleep(3000);}
> > > 接收端:MulticastSocket socket=new MulticastSocket(port); //接收端要指定port
socket.joinGroup(inet);//加入组播地址
//建立DatagramPacket
DatagramPacket packet=new DatagramPacket(byte[] data,data.length,inet,port);
//接收数据
while (true) { socket.receive(packet);
//转化数据为String
String msg=new String(packet.getData(),0,packet.getLength());} //while循环结束
10. 获取日期和时间并格式化:
> > >当前日期: DateFormat df=DateFormat.getDateInstance();//java.text.DateFormat;
String currentDate=df.format(new Date()); //java.unil.Date;
> > >当前时间: DateFormat df=DateFomat.getTimeInstance(DateFormat.MEDIUM);
String currentTime=df.format(new Date());
> > >设置日期和时间样式: SimpleDateFormat format=new SimpleDateFormat("yyyy-mm-dd HH:mm:ss"); //设置格式,java.text.SimpleDateFormat;
File file=new File("d:/dir/abc.txt");
String modifyDate=format.format(new Date(file.lastModified())); //获取文件最后修改日期和时间
11. 获取屏幕坐标
> > >Dimension d=Toolkit.getDefaultToolkit().getScreenSize(); //屏幕右下角坐标
setBounds((d.with-300)/2,(d.height-200)/2,300,200); //将frame设置为屏幕中央
12. 生成jar: Eclipse->Export->Java->Runnable jar file-> select java file and destination folder click next->done
13. 一些常用的不会改变的Config参数可以放入interface,需要用到的类只要实现这个interface即可使用,如DB使用中
interface DBConfig { String Driver="com.mysql.jdbc.Driver"; String DBurl="..."; String user="..."; String psw="..."}
class DBConnection implements DBConfig {// static function to connection to DB and returns Connection type}
class DBFuction {//DB functions, will user DBConnection class to connect to DB}
class Fuction {//functions which will use DBFuctions}
PS: 试图在实现类的code中更改interface中的成员变量值会报错