流对象
常见流
ByteArrayOutputStream 获取字节数组 toByteArray()
DataOutputStream 获取各种类型数据
ObjectOutputStream 对象序列化
FileOutputStream 文件读写
ZipOutputStream 压缩文件处理
流管道
目的为结果 FileOutputStream f = new FileOutputStream(“c://b.txt”);
目的为其他流 ObjectOutputStream o = new ObjectOutputStream(f);
对象序列化(反序列化)(对象类引用Serializable接口) 相当于自动序列化
FileOutputStream f = new FileOutputStream("c://b.txt");
ObjectOutputStream o = new ObjectOutputStream(f);
o.writeObject(new stu("32",121,213));
o.close();
f.close();
FileintputStream f = new FileintputStream ("c://b.txt");
ObjectintputStream o = new ObjectintputStream(f);
stu t = o.readObject();
o.close();
f.close();
版本序列号(号码相同可用)serialVersionUID = -8727451110083870139L;
序列化内容:
版本信息
类描述信息(类全名)
属性值(全部支持序列化)
自定义序列化(Externalizable接口)
public void writeExternal(ObjectOutput out)throws IOException{
out.writeInt(age);
};
public void readExternal(ObjectInput in)throws IOException,ClassNotFoundException{
age = in.readInt();
};
过滤流
对数据进行加工
public static void main(String[] args) {
try {
FileOutputStream f = new File0utputStream("c://b.zip");
Zip0utputStream z = new Zip0utputStream(f);
inputfile(z, new File("d://aaaa/"));
z.close();
f.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private static void inputfile( Zip0utputStream z, File f) throws Exception {
if(f.isDirectory()) { //检查一个对象是否是文件夹。返回值是boolean类型的。如果是则返回true,否则返回false。
File[] fs=f.listFiles( );//listFiles是获取该目录下所有文件和目录的绝对路径
for(File f1:fs)
inputfile(z, f1);
return;
}
z.putNextEntry(new ZipEntry(f.getName()));
System.out.println(f.getPath());
FileInputStream fi = new FileInputStream(f);
byte[] tempbytes = new byte[100];
int byteread = 0;
while ((byteread = fi.read(tempbytes))!=-1) {
z.write(tempbytes , 0, byteread);
fi.close();
}
blocking IO 同步阻塞(以上)(频率小,数据量小)
NON blocking IO 同步非阻塞(网络处理,频率高,数据量小)
Selector,Buffer,Channel
创建
FileInputStream fi = new FileInputStream(f);
FileChannel inChannel = fi.getChannel();
缓冲区
ByteBuffer buf = ByteBuffer.allocate(48);
读写
int bytesRead = inChannel.read(buf);
完成
Asynchronization NON blocking IO 异步非阻塞(超大并发,频率高,数据量大)
枚举(无法继承,不需构造方法)
sex s = sex.nan;
1、定义
enum sex{
nan,nv
}
2、switch 选择分支使用
switch (s) {
case nan:
break;
case nv:
break;
}
3、高级定义(项目有值,只读属性,私有构造方法(含参))
访问时可通过s.getVal()获取选择值
enum sex{
nan(100),nv(101);
private int val;
private sex(int val) {
this.val = val;
}
public int getVal() {
return val;
}
}
泛型-广泛
定义:
public class stu <T,...>{ } 类定义
interface b1<T>{ } 接口定义
public <C> void test(C c) { } 方法定义
public class stu <T> implements b1<T>{ } 继承引用
public class stu <T extends Number>{ } 泛型约束
使用:(容器使用,类型传递)
创建时确定类型,获取时同样明确类型,不需强制转换或判断
不适合对泛型数据进行业务处理(只适合取放)
stu<Integer> stu = new stu<Integer>();
反射
java源码文件,类文件,类加载器,字节码,创建对象
原理:对字节码直接操作,完成类的操作
1、找到字节码
Class 指向字节码的描述(类-字节码的描述类)
(1)Class cls = 类名.class;
(2)Class cls = 类实例.getClass();
(3)Class cls = Class.forName("类全名");
(4)URL[] urls = new URL(new URL("file:///C:/a/"));
URLClassLoader loader = new URLClassLoader(urls);
Class cls = loader.loadClass("zz.math");
2、创建对象(多参数,私有Declared,setAccessible(true))
Object o = cls.newInstance(); //创建实例,获取默认构造方法
Constructor c = cls.getConstructor(); //获取默认构造方法
Object o = c.newInstance();
Constructor c = cls.getConstructor(String.class); //获取指定参数构造方法
Object o = c.newInstance("ssss");
int a = c.getModifiers(); //获取方法修饰
if(a|Modifier.PUBLIC == 0){
多个修饰按位获取
}
Constructor[] c = cls.getConstructors(); //获取public的构造方法
Constructor[] c = cls.getDeclaredConstructors();
cs.setAccessible(true); //忽略访问权限
3、属性
Field f = cls.getField("count");
f.set(obj, value);
Object o = f.get(obj);
4、方法(static方法运行不需实例设为null)
Method m = cls.getMethod("add", int.class,int.class); //获取方法
Object v = m.invoke(实例对象, 203,205);//执行+返回值
5、其他处理
Method m = cls.getMethod("add", int.class,int.class);
Class<?> returncls = m.getReturnType();
System.out.println(returncls.getName());
核心:
1、Class对象获取
2、可以获取类里面的全部描述
获取public的一个 getConstructor()
获取定义的一个 getDeclaredConstructor()
获取public的全部 getConstructors()
获取定义的全部.getDeclaredConstructors()
设置安全检验 .setAccessible(true)
3、执行时需实例(static时为null)
4、参数对应(查询时和执行时)
控制反转:ioc