javaWeb学习之旅(一)------------java基础知识增强

一、java基础

1.文件的复制

debug调试
F5: step into 
F6:  step over 
F7:  step return

思路如下:


java实现的方法如下

public class Demo1 {

	@Test
	public static void main(String[] args) {
		String file1 = "F:\\test1.txt";
		String file2 = "F:\\test.txt";
		try {
			// 复制
			copy(file1, file2);
			System.out.println("文件2拷贝成功");
		} catch (Exception e) {
			// TODO: handle exception
		}

	}

	private static void copy(String file1, String file2) throws Exception {
		// 以流的形式读入文件1中的内容
		InputStream inputStream = readFile(file1);
		writetoFile(inputStream, file2);
	}

	private static void writetoFile(InputStream inputStream, String file2)
			throws IOException {
		// TODO Auto-generated method stub
		// 如果文件2不存在,则创建文件2
		if (!existFile(file2)) {
			File file = new File(file2);
			file.createNewFile();
		}
		// 打开文件2 的输出流
		FileOutputStream fileOutputStream = new FileOutputStream(file2);
		intout(inputStream, fileOutputStream);

	}

	// 从buffer中读入并写入文件2中
	private static void intout(InputStream inputStream,
			FileOutputStream fileOutputStream) {
		try {
			int len = 0;
			byte buffer[] = new byte[1024];
			while ((len = inputStream.read(buffer)) > 0) {

				fileOutputStream.write(buffer, 0, len);

			}

		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			if (inputStream != null) {
				try {
					inputStream.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (fileOutputStream != null) {
				try {
					fileOutputStream.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

		}

		// TODO Auto-generated method stub

	}

	// 读文件
	private static InputStream readFile(String file1)
			throws FileNotFoundException {
		// TODO Auto-generated method stub

		// 判断文件1存在
		if (!existFile(file1)) {
			throw new FileNotFoundException("指定的文件不存在");
		}

		// 向输入流中读文件
		FileInputStream inputStream = new FileInputStream(file1);

		return inputStream;
	}

	// 判断文件是否存在
	private static boolean existFile(String existfile) {
		// TODO Auto-generated method stub

		File file = new File(existfile);
		if (file.exists()) {
			return true;
		}
		return false;
	}

}


2.eclipse 快捷键

ctrl + shift + O         导包
ctrl + shift + F         格式化代码
ctrl + shift + X         更改为大写              
ctrl + shift + Y         更改为小写       
ctrl + shift + 向下键    复制行
ctrl + shift + T         查看类的源代码
Alt  + 方向键            向前向后
ctrl + /                 注释
F2                       查看方法说明
ctrl + T                 查看类的继承关系
ctrl + 1                 快速修复
Alt  +  /                   内容不全


3.junit测试类

在类的方法前添加@Test 可以用junit的测试类的输出
//每个测试方法运行之前、之后运行   @Before    @After
设置断言  
// 断言 测试返回值是否是期望的返回值
        Assert.assertEquals("1", person.run());

public class Person {

	public String run(){
		System.out.println("run");
		return "1";
	}
	public void eat(){
		System.out.println("eat");
	}
}

public class Demo4 {
	Person person;
//每个测试方法运行之前运行
	
	@Before
	public void before() {
		person = new Person();
		System.out.println("before");
	}

	@Test
	public void testrun() {

		person.run();
//		断言 测试返回值是否是期望的返回值
        Assert.assertEquals("1", person.run());
	}

	@Test
	public void testeat() {

		person.eat();

	}

	@After
	public void after() {
		System.out.println("after");

	}

}

4.装箱和拆箱

public class Demo5 {
	public static void main(String[] args) {

		Integer i = 1;// 装箱
		int j = i; // 拆箱

		// 典型应用
		ArrayList list = new ArrayList();
		list.add(1);
		list.add(2);
		list.add(3);
		Iterator iterator = list.iterator();
		while (iterator.hasNext()) {
			int k = (Integer) iterator.next(); // 拆箱
		}
	}

}

5.for循环


Map.Entry entry = (Entry) iterator.next();
具有如下两个方法

entry.getKey(); entry.getValue();
而map只有get();方法
String key = (String) obj;
String value = (String) map.get(key);



public class Demo6 {

	@Test
	public void test1() {

		int arr[] = { 1, 2, 3 };
		for (int num : arr) {
			System.out.println(num);
		}
	}

	@Test
	public void test2() {
		ArrayList list = new ArrayList();
		list.add(1);
		list.add(2);
		list.add(3);
		for (Object obj : list) {
			int i = (Integer) obj;
			System.out.println(i);
		}

	}

	@Test
	public void test3() {
		Map map = new LinkedHashMap();
		map.put("1", "aaa");
		map.put("2", "bbb");
		map.put("3", "ccc");

		// 传统方式1
		Set set = map.keySet();
		Iterator iterator = set.iterator();
		while (iterator.hasNext()) {

			String key = (String) iterator.next();
			String value = (String) map.get(key);
			System.out.println(key + "=" + value);

		}

	}

	@Test
	public void test4() {
		Map map = new LinkedHashMap();
		map.put("1", "aaa");
		map.put("2", "bbb");
		map.put("3", "ccc");

		// 传统方式 2
		Set set = map.entrySet();
		Iterator iterator = set.iterator();
		while (iterator.hasNext()) {
			Map.Entry entry = (Entry) iterator.next();

			String key = (String) entry.getKey();
			String value = (String) entry.getValue();
			System.out.println(key + "=" + value);

		}

	}

	@Test
	public void test5() {
		Map map = new LinkedHashMap();
		map.put("1", "aaa");
		map.put("2", "bbb");
		map.put("3", "ccc");

		// 增强for循环
		for (Object obj : map.keySet()) {
			String key = (String) obj;
			String value = (String) map.get(key);
			System.out.println(key + "=" + value);

		}

	}

	@Test
	public void test6() {
		Map map = new LinkedHashMap();
		map.put("1", "aaa");
		map.put("2", "bbb");
		map.put("3", "ccc");

		// 增强for循环
		for (Object obj : map.entrySet()) {
			Map.Entry entry = (Entry) obj;
			String key = (String) entry.getKey();
			String value = (String) entry.getValue();
			System.out.println(key + "=" + value);

		}

	}
6.可变参数

                //如果声明的是int 则不行,
		//会报错,Arrays.asList 接受传递的是一个对象
		Integer nums[]={1,2,3,4,5};
		list=Arrays.asList(nums);
		System.out.println(list);

public class Demo7 {
	@Test
	public void testsum() {
		sum(1, 2, 3, 4, 5);
	}

	public void sum(int... nums) {
		// 可以把可变参数看出数组
		// 可以前面是一个定参,后面是一个可变参数
		// public void sum(int x,int... nums)
		int sum = 0;
		for (int i : nums) {
			sum += i;
		}
		System.out.println(sum);
	}
	@Test
	public void canshu(){
		//asList 接受传递的是一个对象
		List list=Arrays.asList("1","2","3");
		System.out.println(list);
		
		
		String arr[]={"1","2","3","4"};
		list=Arrays.asList(arr);
		System.out.println(list);

		
		//如果声明的是int 则不行,
		//会报错,Arrays.asList 接受传递的是一个对象
		Integer nums[]={1,2,3,4,5};
		list=Arrays.asList(nums);
		System.out.println(list);
		
	}
	
	
}


7.反射

反射就是加载类,并解剖出类的各个组成部分,主要运用于框架中

反射 加载类 有3中方法如下

 //1,这个方法貌似有问题

Class class1 = Class.forName("/day01/src/com/nyist/wj/Person");
  // 2
  Class class2 = new Person().getClass();
  // 3
  Class class3 = Person.class;

 

// 暴力反射、
  constructor.setAccessible(true);

可以利用反射的机制实现访问私有的方法

 publicprivate
构造函数getConstructor()getDeclaredConstructor()
构造方法getMethod()getDeclaredMethod()
字段getField()getDeclaredField()

    

   /**
  * 反射类的public  构造方法
  * **/

// 、public Person(String name,int password)

	@Test
	public void test3() throws Exception {

		Class class2 = Person.class;

		Constructor constructor = class2
				.getConstructor(String.class, int.class);

		Person person = (Person) constructor.newInstance("dddddd", 12);
		System.out.println(person.name);
	}

/**
  * 反射类的private  构造方法
  * **/

	@Test
	// 暴力反射私有的构造函数 private Person(List list)
	public void test4() throws Exception {

		Class class4 = Person.class;
		Constructor constructor = class4
				.getDeclaredConstructor(ArrayList.class);
		// 暴力反射、
		constructor.setAccessible(true);
		Person person = (Person) constructor.newInstance(new ArrayList());
		System.out.println(person.name);

	}


/**
  * 反射类的public 构造函数
  * **/

@Test
	// public void test1(String name, int passward)
	public void fun1() throws Exception {
		Class class1 = Person.class;
		Method method = class1.getMethod("test1", String.class, int.class);
		Person person = new Person();
		method.invoke(person, "wangjie", 23);

	}

/**
  * 反射类的private  构造函数
  * **/

@Test
	// private void test1(InputStream inputStream)
	public void fun3() throws Exception {
		Class class1 = Person.class;
		Method method = class1.getDeclaredMethod("test1", InputStream.class);
		//暴力反射
		method.setAccessible(true);
		Person person = new Person();
		method.invoke(person, new FileInputStream("c:\\1.txt"));

	}



/**
  * 反射类的private   static 构造函数
  * **/

@Test
	// 静态的方法不需要对象
	// private static void test1(int num)
	public void fun4() throws Exception {
		Class class1 = Person.class;
		Method method = class1.getDeclaredMethod("test1", int.class);

		method.invoke(null, 23);

	}


/****
  *
  * 反射public 字段
  * *****/

//public String name="wangjie";
	public void ziduan() throws Exception {
		Class class1 = Person.class;
		Field field = class1.getField("name");
		Person person = new Person();
		String name = (String) field.get(person);
		// System.out.println(name);
		// 获取字段的值
		Object value = field.get(person);

		// 获取字段的类型
		Class typeClass = field.getType();
		if (typeClass.equals(String.class)) {

			String svalueString = (String) value;
			System.out.println(svalueString);
		}
		field.set(person, "ddddd");

		System.out.println(person.name);

	}


/****
  *
  * 反射private  字段
  * *****/

	@Test
	// private int password=123456
	public void ziduan1() throws Exception {

		Class class1 = Person.class;
		Field f = class1.getDeclaredField("password");
		Person person = new Person();
		// 暴力反射、
		f.setAccessible(true);
		System.out.println(f.get(person));

	}


 Demo如下:

 

package com.nyist.wj;

import java.awt.List;
import java.io.FileInputStream;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.jar.Attributes.Name;

import org.junit.Test;

public class Demo8 {

	/**
	 * 反射类的构造方法
	 * **/
	// 反射 无参的构造函数 public Person()
	@Test
	public void test1() throws Exception {
		Class class1 = new Person().getClass();
		Constructor constructor = class1.getConstructor(null);
		Person person = (Person) constructor.newInstance(null);
		System.out.println(person.name);

	}

	// public Person(String name)
	@Test
	public void test2() throws Exception {

		Class class2 = Person.class;

		Constructor constructor = class2.getConstructor(String.class);

		Person person = (Person) constructor.newInstance("dddddd");
		System.out.println(person.name);
	}

	// 、public Person(String name,int password)

	@Test
	public void test3() throws Exception {

		Class class2 = Person.class;

		Constructor constructor = class2
				.getConstructor(String.class, int.class);

		Person person = (Person) constructor.newInstance("dddddd\n", 12);
		System.out.println(person.name);
	}

	@Test
	// 暴力反射私有的构造函数 private Person(List list)
	public void test4() throws Exception {

		Class class4 = Person.class;
		Constructor constructor = class4
				.getDeclaredConstructor(ArrayList.class);
		// 暴力反射、
		constructor.setAccessible(true);
		Person person = (Person) constructor.newInstance(new ArrayList());
		System.out.println(person.name);

	}

	/**
	 * 反射类的构造函数
	 * **/

	@Test
	// public void test1()
	public void fun() throws Exception {
		Class class1 = Person.class;
		// method获得方法
		Method method = class1.getMethod("test1", null);
		Person person = new Person();
		method.invoke(person, null);

	}

	@Test
	// public void test1(String name, int passward)
	public void fun1() throws Exception {
		Class class1 = Person.class;
		Method method = class1.getMethod("test1", String.class, int.class);
		Person person = new Person();
		method.invoke(person, "wangjie", 23);

	}

	@Test
	// public Class[] test1(String name, int[] password)
	public void fun2() throws Exception {
		Class class1 = Person.class;
		Method method = class1.getMethod("test1", String.class, int[].class);
		Person person = new Person();
		Class array[] = (Class[]) method.invoke(person, "wangjie", new int[] {
				1, 2, 3 });
		System.out.println(array[1]);

	}

	@Test
	// private void test1(InputStream inputStream)
	public void fun3() throws Exception {
		Class class1 = Person.class;
		Method method = class1.getDeclaredMethod("test1", InputStream.class);
		//暴力反射
		method.setAccessible(true);
		Person person = new Person();
		method.invoke(person, new FileInputStream("c:\\1.txt"));

	}

	@Test
	// 静态的方法不需要对象
	// private static void test1(int num)
	public void fun4() throws Exception {
		Class class1 = Person.class;
		Method method = class1.getDeclaredMethod("test1", int.class);

		method.invoke(null, 23);

	}

	@Test
	// public static void main(String[] args)
	public void fun5() throws Exception {
		Class class1 = Person.class;
		Method method = class1.getDeclaredMethod("main", String[].class);

		// method.invoke(null, new Object[]{new String[]{"aa","ggg"}});
		// 或者
		method.invoke(null, (Object) new String[] { "ss", "ss" });

	}

	@Test
	/****
	 * 
	 * 反射字段
	 * *****/
	//public String name="wangjie";
	public void ziduan() throws Exception {
		Class class1 = Person.class;
		Field field = class1.getField("name");
		Person person = new Person();
		String name = (String) field.get(person);
		// System.out.println(name);
		// 获取字段的值
		Object value = field.get(person);

		// 获取字段的类型
		Class typeClass = field.getType();
		if (typeClass.equals(String.class)) {

			String svalueString = (String) value;
			System.out.println(svalueString);
		}
		field.set(person, "ddddd");

		System.out.println(person.name);

	}

	@Test
	// private int password=123456
	public void ziduan1() throws Exception {

		Class class1 = Person.class;
		Field f = class1.getDeclaredField("password");
		Person person = new Person();
		// 暴力反射、
		f.setAccessible(true);
		System.out.println(f.get(person));

	}

	@Test
	// private static int age=23;
	public void ziduan2() throws Exception {

		Class class1 = Person.class;
		Field f = class1.getDeclaredField("password");
		Person person = new Person();
		// 暴力反射、
		f.setAccessible(true);
		System.out.println(f.get(person));

	}
}


Person类如下:

package com.nyist.wj;

import java.io.InputStream;
import java.util.ArrayList;

public class Person {

	//反射字段
	public String name="wangjie";
	private int password=123456;
	private static int age = 23;
	

	/**
	 * 反射类的构造函数
	 * **/
	public Person() {
		System.out.println("person");
	}

	public Person(String name) {
		System.out.println(name);
	}

	public Person(String name, int password) {
		System.out.println(name + ":" + password);
	}

	private Person(ArrayList list) {
		System.out.println(list);
	}

	/**
	 * 反射类的构造方法
	 * **/
	public void test1() {
		System.out.println("test");
	}

	public void test1(String name, int passward) {

		System.out.println(name + "" + passward);
	}

	public Class[] test1(String name, int[] password) {

		return new Class[] { String.class };
	}

	private void test1(InputStream inputStream) {
		System.out.println(inputStream);
	}

	private static void test1(int num) {

		System.out.println(num);
	}

	
	public static void main(String[] args) {
		System.out.println("main.....");
	}
	// 以下代码用于Demo4中

	public String run() {
		System.out.println("run");
		return "1";
	}

	public void eat() {
		System.out.println("eat");
	}

}


8.内省操作javabean的属性

Demo如下

public class Demo9 {

	// 得到bean的所有属性
	@Test
	public void test1() throws IntrospectionException {
		// 得到bean的自己是属性
		BeanInfo info = Introspector.getBeanInfo(student.class, Object.class);
		PropertyDescriptor[] pds = info.getPropertyDescriptors();

		for (PropertyDescriptor pd : pds) {
			System.out.println(pd.getName());
		}

	}

	// 操作bean的制定属性
	@Test
	public void test2() throws Exception {
		PropertyDescriptor pd = new PropertyDescriptor("age", student.class);

		student stu = new student();
		// 得到属性的写方法,为属性赋值

		Method method = pd.getWriteMethod();
		method.invoke(stu, 23);

		// 获取属性的值
		method = pd.getReadMethod();
		System.out.println(method.invoke(stu, null));
	}

	// 高级属性,获取当前属性的类型
	@Test
	public void test3() throws Exception {
		PropertyDescriptor pd = new PropertyDescriptor("age", student.class);

		student stu = new student();
		// 得到属性的写方法,为属性赋值

		System.out.println(pd.getPropertyType());
	}
}

 

Studet.java如下

 

package com.nyist.wj;

public class student {
	private String name;
	private String passward;
	private int age;
	//本实例工有5个属性 隐藏的一个是object
	
	public String getab() {
		return null;
	}
	
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassward() {
		return passward;
	}
	public void setPassward(String passward) {
		this.passward = passward;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	

}

9.泛型的使用

测试的例子如下:

	@Test
	public void test1() {

		List<String> list = new ArrayList<String>();
		list.add("qq");
		list.add("ghg");
		list.add("eee");
		// 传统
		Iterator<String> iterator = list.iterator();
		while (iterator.hasNext()) {

			String value = iterator.next();
			System.out.println(value);

		}
		// 增强for循环

		for (String string : list) {
			System.out.println(string);
		}

	}


还有...

	@Test
	public void test2() {

		Map<Integer, String> map = new HashMap<Integer, String>();
		map.put(1, "hhhh");
		map.put(2, "eeee");
		map.put(3, "hhhh");
		// 传统keyset entryset

		Set<Map.Entry<Integer, String>> set = map.entrySet();
		Iterator<Map.Entry<Integer, String>> iterator = set.iterator();
		while (iterator.hasNext()) {
			Map.Entry<Integer, String> entry = (Map.Entry<Integer, String>) iterator
					.next();
			int key = entry.getKey();
			String value = entry.getValue();
			System.out.println(key + " " + value);

		}

		// 增强for循环的方法(重点)

		for (Map.Entry<Integer, String> entry : map.entrySet()) {

			int key = entry.getKey();
			String value = entry.getValue();
			System.out.println(key + " " + value);
		}
	}

 工程包下载地址:http://download.csdn.net/detail/wjky2014/5220360


 




  

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员Android

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值