java学习笔记(十八)

一、Properties属性文件

java.util.Properties;

load()解析 *.properties-->Properties对象

store()保存Properties对象-->*.properties

list()在指定流上输出全部的属性信息

getProperties(String key)获取指定属性

setProperties(key,Value)修改指定属性

实例:

public static void main(String[] args) 
	throws IOException{
		//Properties是一个Map集合,相当于Map<String,String>
		Properties cfg=new Properties();
		cfg.load(new FileInputStream("src/day21/config.properties"));
		String url=cfg.getProperty("jdbc.url");
		System.out.println(url);
		//不支持中文的GBK编码,网上有那种现成的工具,如果要在属性文件中写中文的话,可以拿来用
		Properties sysCfg=System.getProperties();
		sysCfg.list(System.out);//打印出所有的属性信息
	}


二、网络编程

TCP编程

服务器

A.IP地址(域名)

B.不同的端(ServerSocket)1--65535,一般不用1000以下的端口,因为这些端口会被系统指定为特定功能的端口

C.每个端口可建立多个Socket实例,每个实例代表一个对客户的服务

客户端

A.使用域名(IP)找到指定的服务器

B.使用服务器的端口号找到服务端口

C.建立Socket对象

D.使用流与服务器通信

实例

public class ServerDemo {
	public static void main(String[] args) 
	throws Exception{
		ServerDemo server=new ServerDemo();
		server.listen(8903);						//server端口
	}
	public void listen(int port)throws Exception{
		ServerSocket ss=new ServerSocket(port);		//在食堂开了窗口,但是没开张
		while(true){
			Socket socket=ss.accept();				//开张,可以接受请求,socket就是那个客户,等待客户端请求
			new ClientAgent(socket).start();		//这里是一个线程
		}
	}
	class ClientAgent extends Thread{
		Socket socket;
		public ClientAgent(Socket socket){
			this.socket=socket;
		}
		public void run(){
			try{
			BufferedReader in=new BufferedReader(
					new InputStreamReader(
							socket.getInputStream()));
			PrintWriter out=new PrintWriter(socket.getOutputStream());
			out.println("今天来点啥?");
			out.flush();
			String str=in.readLine();					//读取客户端的内容
			if(str.equals("包子")){
				out.println("没有");
				out.flush();
			}
			socket.close();								//这是一个没礼貌的服务员
			}catch(Exception e){
				e.printStackTrace();
			}
		}
	}
}

public class ClientDemo {
	public static void main(String[] args) 
	throws Exception{
		Socket socket=new Socket("localhost",8903);	//这个socket代表服务器
		BufferedReader in=new BufferedReader(
				new InputStreamReader(
						socket.getInputStream()));
		PrintWriter out=new PrintWriter(socket.getOutputStream());
		String str=in.readLine();					//读取服务器来的内容
		System.out.println(str);
		out.println("包子");
		System.out.println("包子");
		out.flush();
		str=in.readLine();
		System.out.println(str);
		socket.close();
	}
}

可以类比食堂打饭来理解网络编程中服务器与客户端的关系

三、反射(自省)是自我管理的机制

java代码管理java的类和方法等

任何类都是Class的具体实例,类加载到内存中的结果是Class的实例,是一个对象

Student s=new Student();

Class cls=Student.class;

1.Class实例的获得方式

A.java类只按需加载一次,任何方式获得的实例都是

B.三种方法:  (1)类的.class属性

(2)对象的getClass()方法

(3)Class类的静态寻找方法:Class.forName()

实例:

public class ClassDemo {
	public static void main(String[] args)throws Exception {
		Class c1=Foo.class;
		System.out.println(c1.getName());
		Foo f=new Foo();
		Class c2=f.getClass();
		System.out.println(c1==c2);//true
		Class c3=Class.forName("day21.Foo");
		System.out.println(c3==c1);//true
	}
}
class Foo{
	static{
		System.out.println("Load Foo");
	}
}
public class MethodDemo {
	public static void main(String[] args) 
	throws Exception{
		Class cls=Class.forName("day21.Goo");
		Method[] ms=cls.getMethods();
	//	cls.getDeclaredMethods()
		for(Method method:ms){
			System.out.println(method.getName());
		}
	}
}
class Goo{
	public int getVal(){return 4;}
}



2.class实例的用途

反射应用:

测试工具

实例:

public class TestCaseDemo {
	public static void main(String[] args) throws Exception{
		while(true){
			System.out.println("输入类名");
			Scanner s=new Scanner(System.in);
			String name=s.nextLine();
			Class cls=Class.forName(name);
			Object obj=cls.newInstance();
			Method[] ms=cls.getMethods();
			for(Method m:ms){
				if(m.getName().startsWith("test")){
					m.invoke(obj, new Object[]{});//执行方法名以test开头的方法
				}
			}
		}
	}

}
class TestCase2{
	public void testVal(){
		System.out.println("testCase2");
	}
}
class TestCase{
	public void testGBK()throws IOException{
		System.out.println(IOUtils.toHexString("中国".getBytes("GBK")));
	}
	public void testUTF8()throws IOException{
		System.out.println(IOUtils.toHexString("中国".getBytes("utf-8")));
	}

	public void testUTF16BE()throws IOException{
		System.out.println(IOUtils.toHexString("中国".getBytes("utf-16be")));
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值