序列化:被序列化的类必须实现Serializable接口)
String path = "D:" + File.separator + "soft" + File.separator + "object.txt";
OutputStream ops = new FileOutputStream(path);
InputStream ips = new FileInputStream(path);
ObjectOutputStream oops = new ObjectOutputStream(ops);
oops.writeObject(new Person("haoren",12));
oops.close();
ObjectInputStream oips = new ObjectInputStream(ips);
Object o =oips.readObject();
System.out.print(o);
ransient关键字(这个属性就不会在被序列化,序列化主要是对属性的序列化)
//多个对象的存和读
Person[] p = {new Person("haoren",34),new Person("huairen",33)};
String path = "D:" + File.separator + "soft" + File.separator + "object.txt";
OutputStream ops = new FileOutputStream(path);
InputStream ips = new FileInputStream(path);
ObjectOutputStream oops = new ObjectOutputStream(ops);
oops.writeObject(p);
oops.close();
ObjectInputStream oips = new ObjectInputStream(ips);
Object[] b = (Object[]) oips.readObject();
for(int i=0;i<b.length;i++){
System.out.println(b[i]);
}
oips.close();
反射(reflect)
//通过Class类获得类名
Class c1 = null;
Class c2= null;
Class c3 = null;
c1 = Class.forName(“cn.test.Person”);//这种最常用
c2 = new Person().getClass();
c3 = Person.class;
System.out.println(c1.getName());
System.out.println(c2.getName());
System.out.println(c3.getName());//通过Class类获得类名
Class c1 = null;
Class c2= null;
Class c3 = null;
c1 = Class.forName(“cn.test.Person”);//这种最常用
c2 = new Person().getClass();
c3 = Person.class;
System.out.println(c1.getName());
System.out.println(c2.getName());
System.out.println(c3.getName());
//取得全部方法//通过Class类创建对象(这种创建对象的方式必须要无参构造方法) /* String className= "cn.java.reflect.Person"; Class ob = Class.forName(className); Person p = (Person)ob.newInstance(); p.setName("haoren"); p.setAge(20); System.out.println(p.getName()); System.out.println(p.getAge());*/ //通过有参数的构造方法来创建对象 /*String className= "cn.java.reflect.Person"; Class ob = Class.forName(className); Constructor[] cst = ob.getConstructors(); //Person p = (Person)cst[0].newInstance("huairen");//这里的构造函数的数组是有顺序的,按照参数的个数排序 Person p = (Person)cst[1].newInstance("huairen",20); System.out.println(p);*/ //取得类的结构 String className= "cn.java.reflect.Person"; Class ob = Class.forName(className); //取得其全部实现的接口 /*Class[] inter = ob.getInterfaces(); for(int i=0;i<inter.length;i++){ System.out.println(inter[i]); }*/ //取得其父类 Class superClass = ob.getSuperclass(); System.out.println(superClass); //取得全部的构造方法 /*Constructor[] cs = ob.getConstructors(); for(int i=0;i<cs.length;i++){ System.out.println(cs[i]); }
Method[] mthd = ob.getMethods();
/*for(int i=0;i<mthd.length;i++){
System.out.println(mthd[i]);
}*/
//获得方法的形参类型
/*for(int i=0;i<mthd.length;i++){
Class[] pa = mthd[i].getParameterTypes();
for(int j=0;j<pa.length;j++){
System.out.println(pa[j]);
}
}*/
//取出构造方法的形参
for(int i=0;i<cs.length;i++){
Class[] pr = cs[i].getParameterTypes();
for(int j=0;j<pr.length;j++){
System.out.println(pr[j]);
}
}
//取出构造方法的权限
for(int i=0;i<cs.length;i++){
int md = cs[i].getModifiers();
//System.out.println(md);
System.out.println(Modifier.toString(md));//将数字1还原为public
}
//取得构造方法名
System.out.println(cs[0].getName());*/网络编程
UDP
发送端:
接收端System.out.println("发送端"); DatagramSocket ds = new DatagramSocket(); String s = "美女你好"; byte[] b = s.getBytes(); DatagramPacket dp = new DatagramPacket(b, b.length,InetAddress.getByName("192.168.0.4"),5550); ds.send(dp); ds.close();
System.out.println("接收端"); DatagramSocket ds = new DatagramSocket(5550); byte[] b = new byte[1024]; DatagramPacket dp = new DatagramPacket(b,b.length); ds.receive(dp); String r = new String(dp.getData(),0,dp.getLength()); System.out.println(r); ds.close();
发送端:System.out.println("发送端"); BufferedReader buffr = new BufferedReader(new InputStreamReader(System.in)); while(true){ System.out.print("setIn:"); String s = buffr.readLine(); if("quit".equals(s)){ break; } DatagramSocket ds = new DatagramSocket(); byte[] b = s.getBytes(); DatagramPacket dp = new DatagramPacket(b, b.length,InetAddress.getByName("192.168.0.4"),5550); ds.send(dp); } ds.close();
接收端
System.out.println("接收端"); DatagramSocket ds = new DatagramSocket(5550); while(true){ byte[] b = new byte[1024]; DatagramPacket dp = new DatagramPacket(b,b.length); ds.receive(dp); String r = new String(dp.getData(),0,dp.getLength()); System.out.println(r); }
tcp
客户端
服务器端:System.out.println("客户端:"); Socket sck = new Socket("192.168.0.4",5552); //向服务器端发消息 OutputStream out = sck.getOutputStream(); out.write("woshihaoren".getBytes()); //接受服务器端发来的消息 InputStream in = sck.getInputStream(); byte[] b = new byte[1024]; int i = in.read(b); System.out.println(new String(b,0,i)); sck.close();
文件上传System.out.println("服务端"); ServerSocket ssckt = new ServerSocket(5552); //获得客户端对象 Socket sckt = ssckt.accept(); //接受客户端发来的消息 InputStream in = sckt.getInputStream(); byte[] b = new byte[1024]; int i = in.read(b); System.out.println(new String(b,0,i)); //向客户端发送消息 OutputStream ops = sckt.getOutputStream(); ops.write("我是服务器".getBytes()); ssckt.close();
客户端:
Socket s = new Socket("192.168.0.3",7777); File f = new File("D:"+File.separator+"1326945231.jpg"); InputStream ips = new FileInputStream(f); OutputStream ops = s.getOutputStream(); byte[] b = new byte[1024]; int len = 0; while((len=ips.read(b))!=-1){ ops.write(b,0,len); } s.close(); ips.close();
服务端:
ServerSocket ss = new ServerSocket(7777); Socket s = ss.accept(); InputStream ips = s.getInputStream(); File f = new File("G:"+File.separator+"007.jpg"); OutputStream ops = new FileOutputStream(f); byte[] b = new byte[1024]; int len = 0; while((len=ips.read(b))!=-1){ ops.write(b,0,len); } ss.close(); ops.close(); System.out.print("上传完毕");
同时接收多个客户端上传ServerSocket ss = new ServerSocket(7777); while(true){ Socket s = ss.accept(); new Thread(new Load(s)).start(); } public class Load implements Runnable { private Socket s; static int count = 0; public Load(Socket s2) { this.s =s2; } public void run() { InputStream ips = null; try { ips = s.getInputStream(); } catch (IOException e2) { e2.printStackTrace(); } File f = new File("G:"+File.separator+"("+(++count)+").jpg"); OutputStream ops = null; try { ops = new FileOutputStream(f); } catch (FileNotFoundException e1) { e1.printStackTrace(); } byte[] b = new byte[1024]; int len = 0; try { while((len=ips.read(b))!=-1){ ops.write(b,0,len); } } catch (IOException e) { e.printStackTrace(); } try { ops.close(); } catch (IOException e) { e.printStackTrace(); } System.out.print("上传完毕"); } }
枚举类枚举的原因:些方法在运行时,它需要的数据不能是任意的,而必须是一定范围内的值,比如成绩Grade只能为ABCDE。
枚举的特征:枚举类也是一种特殊形式的Java类。
枚举类中声明的每一个枚举值代表枚举类的一个实例对象。
与java中的普通类一样,在声明枚举类时,也可以声明属性、方法和构造函数,但枚举类的构造函数必须为私有的(这点不难理解)。
枚举类也可以实现接口、或继承抽象类。
JDK5中扩展了swith语句,它除了可以接收int, byte, char, short外,还可以接收一个枚举类型。
若枚举类只有一个枚举值,则可以当作单态设计模式使用。package cn.java; public class TestDemo1 { public static void main(String[] args) { //访问枚举中的元素 //System.out.println(Grade.A); //访问枚举中的属性 //System.out.println(Grade.A.getInfo()); //访问枚举中的方法 Grade.A.out(); } } enum Grade{ A("90-100"){ public void out() { System.out.println("优秀"); }},B("80-90"){ public void out() { System.out.println("良好"); }},C("60-80"){ public void out() { System.out.println("一般"); }},D("0-60"){ public void out() { System.out.println("不及格"); }}; private String info; //枚举类可以有构造函数,构造函数必须是私有的 private Grade(String info) { this.info = info; } public String getInfo() { return info; } public void setInfo(String info) { this.info = info; } //枚举类也可以有抽象方法 public abstract void out(); }
//枚举类中的常用函数
//枚举类型对应的字符串名称
Grade g = Grade.A;
System.out.println(g.name());
//拿到元素的编号
System.out.println(g.ordinal());
//通过元素的名字拿到元素的实例
Grade gg = Grade.valueOf("B");
System.out.println(gg.getInfo());
//特殊的属性values:返回所有的枚举值。API中看不到的
Grade[] ggg = Grade.values();
for(Grade x:ggg){
System.out.println(x.name());
}