package com.qiku.day25; import java.io.*; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; public class Zy1 { public static void main(String[] args)throws Exception{ URL url=null; HttpURLConnection urlConnection = null; InputStream inputStream = null; OutputStream outputStream = null; BufferedInputStream bis = null; BufferedOutputStream bos = null; try { url = new URL("https://www.test.com/temp/tupian.png"); urlConnection = (HttpURLConnection) url.openConnection(); inputStream = urlConnection.getInputStream(); bis = new BufferedInputStream(inputStream,1024); outputStream = new FileOutputStream("tupian.png"); bos = new BufferedOutputStream(outputStream); int read; while ((read= bis.read())!=-1){ bos.write(read); } } catch (Exception e) { e.printStackTrace(); }finally { if(bis!=null){ try { bis.close(); } catch (IOException e) { e.printStackTrace(); } } if(bos!=null){ try { bos.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
package com.qiku.day25; public class Zy2 { public static void main(String[] args) { String str = "字符串类"; Integer num = 24; //.class创建类对象 Class c1 = Zy2.class; System.out.println(".class方式创建类对象:" + c1); //引用/对象.getClass()创建类对象 Class c2 = str.getClass(); System.out.println("引用/对象.getClass()方式创建类对象:" + c2); //包装类.TYPE创建类对象 Class c3 = Integer.TYPE; System.out.println("包装类.TYPE方式创建类对象:" + c3); //Class.forName()创建类对象 try { Class c4 = Class.forName((str.getClass() + "").substring(6)); System.out.println("Class.forName()方式创建类对象:" + c4); //类加载器ClassLoader创建类对象 ClassLoader classLoader =Zy2.class.getClassLoader(); Class c5 = classLoader.loadClass("com.HomeWork.day4_6.URLConnectionHW01"); System.out.println(c5); } catch (ClassNotFoundException e) { e.printStackTrace(); } } }
package com.qiku.day25; import java.lang.reflect.Constructor; public class Zy3 { public static void main(String[] args) { //原始方式 无参构造 Person person = new Person(); System.out.println(person); //反射机制 无参构造 try { Class c1 = Class.forName("com.HomeWork.day4_6.Person"); if (c1.newInstance() instanceof Person) { person = (Person) c1.newInstance(); System.out.println(person); } //原始方式 有参构造 person = new Person("张三", 22, "男"); System.out.println(person); //反射机制 有参构造 Class personClass = Person.class; Constructor constructor = personClass.getConstructor(String.class, int.class, String.class); Object o = constructor.newInstance("王五", 58, "女"); if (o instanceof Person) { person = (Person) o; System.out.println(person); } } catch (Exception e) { e.printStackTrace(); } } } //人类 class Person { private String name; private int age; private String sex; public Person() { } public Person(String name, int age, String sex) { this.name = name; this.age = age; this.sex = sex; } @Override public String toString() { return "Preson{" + "name='" + name + '\'' + ", age=" + age + ", sex='" + sex + '\'' + '}'; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } }