欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

java,

来源: javaer 分享于  点击 23191 次 点评:21

java,


文章目录

  • 1.变量
    • 1.局部变量
    • 2.成员变量
    • 3.静态变量
  • 2.常量
  • 3.数据类型
    • 1.基本数据类型
      • 1.数值型
        • 1.整数类型
        • 2.浮点类型
      • 2.字符类型(2byte)
      • 3.布尔型(1bit)
    • 2.引用数据类型(4byte)
      • 1.类
      • 2.接口
      • 3.数组
  • 4.运算符
    • 1.算数运算符
      • 1.一元运算符
      • 2.二元运算符
    • 2.运算及扩展运算符
    • 3.逻辑运算符
    • 4.位运算符
    • 5.字符串连接符
    • 6.条件运算符(三元运算符)
  • 5.类型转换
    • 1.自动类型转换
    • 2.强制类型转换
  • 6.获取键盘输入
  • 7.面向对象
    • 1.类的格式
    • 2.构造方法
    • 3.this关键字
    • 4.static关键字
    • 5.静态初始化块
    • 6.参数传值机制
    • 7.继承
      • 1.概述
      • 2.子类重写父类的方法
      • 3.super
        • 1.概述
        • 2.通过super调用被子类覆盖掉的父类方法
        • 3.构造方法里的super
    • 8.判断某个对象是否属于某个类的(包括这个类的祖先)
    • 9.封装
      • 1.封装的有点
      • 2.封装的实现,使用访问控制符
      • 3.封装的使用细节
    • 10.多态
      • 1.概述
      • 2.多态的要点
    • 11.对象的转型
    • 12.final关键字
    • 13.抽象方法和抽象类
      • 1.概述
      • 2.特点
    • 14.接口
      • 1.声明格式
      • 2.定义接口的详细说明
      • 3.要点
      • 4.一个类可以实现多个接口
      • 5.接口可以继承多个接口
    • 15.内部类
      • 1.成员内部类
        • 1.静态内部类
        • 2.非静态内部类
      • 2.匿名内部类
      • 3.局部内部类
  • 8.jvm内存分析
    • 1.栈stack
    • 2.堆heap
    • 3.方法区method area
  • 9.java的包机制
  • 10.修饰符
  • 11.Object类
    • 1.概述
    • 2.toString
      • 1.概述
      • 2.object类里的toString
    • 3.equals
      • 1.equals和==
      • 2.object类里的equals方法
      • 3.原始的equals方法
      • 4.重写equals方法
      • 5.String类型的equals方法
  • 12.数组
    • 1.概述
    • 2.特点
    • 3.数组的声明
    • 4.创建数组
    • 5.数组的长度
    • 6.数组的遍历
    • 7.数组的拷贝
    • 8.数组中删除指定索引的元素(封装成了方法)
    • 9.数组中指定索引里增加元素(数组的扩容,封装成了方法)
    • 10.数组工具类
      • 1.打印数组
      • 2.数组元素的排序
        • 1.数组元素是基本数据类型
        • 2.数组元素是引用数据类型
      • 3.查寻数组中某个元素的索引
        • 1.二分查找法
    • 11.多维数组
      • 1.概述
      • 2.二维数组的声明
  • 13.String类
    • 1.String基础
    • 2.String类常用方法
    • 3.可变字符序列
      • 1.StringBuilder
      • 2.StringBuffer
      • 3.使用方法
      • 4.可变字符序列和不可变字符序列效率对比
  • 14.算法
    • 1.冒泡排序
      • 1.概述
      • 2.使用java实现
    • 2.二分法查找
      • 1.概述
      • 2.使用java实现
  • 15.包装类
    • 1.概述
    • 2.作用
    • 3.基本使用方法
    • 4.自动装箱和拆箱
      • 1.概述
      • 2.基本用法
      • 3.注意点
    • 5.包装类的缓存问题
  • 16.枚举
  • 17.容器
    • 1.数组的优劣性
    • 2.容器成员图
    • 3.泛型
      • 1.概述
      • 2.自定义泛型
      • 3.容器中使用泛型
    • 4.collection接口
      • 1.概述
      • 2.方法
      • 3.List
        • 1.概述
        • 2.List接口的方法
        • 2.ArrayList
          • 1.特点
        • 3.LinkedList
          • 1.特点
        • 4.Vector
      • 4.Set
        • 1.概述
        • 2.HashSet
        • 3.TreeSet
      • 5.Collections工具类
        • 1.概述
        • 2.对于List
    • 5.Map接口
      • 0.5 map种类
      • 1.概述
      • 2.方法
      • 3.HashMap
        • 1.概述
      • 4.HashTable
      • 5.TreeMap
        • 1.概述
    • 6.使用Iterator迭代器遍历容器元素(List,Set,Map)
      • 1.迭代器遍历List
      • 2.迭代器遍历Set
      • 3.迭代器遍历Map
    • 7.遍历集合的方法总结
  • 18.处理时间的类
    • 1.概述
    • 2.Date时间类
    • 3.DateFormat类和SimpleDateFormat类
    • 4.Calendar日历类
  • 19.Math类
    • 1.概述
    • 2.常用方法
  • 20.Random类
  • 21.File类
    • 1.文件的创建
    • 2.访问文件的属性
    • 3.创建空文件或目录(在该对象所指的文件或目录不存在的情况下)
    • 4.综合应用
    • 5.API
    • 6.字符集
      • 1.编码
      • 2.解码
  • 22.异常机制
    • 1.概述
    • 2.异常的分类
      • 1.RuntimeException运行时异常
      • 2.CheckedException已检查异常
        • 1.捕获异常
        • 2.声明异常(throw子句)
      • 3.自定义异常
  • 23.IO流
    • 1.字节流
      • 1.InputStream
        • 1.方法
        • 2.源头为硬盘上的文件
        • 3.字节数组输入流
        • 4.缓冲输入流
      • 2.OutputStream
        • 1.方法
        • 2.源头为硬盘上的文件
        • 3.字节数组输出流
        • 4.缓冲输出流
      • 3.文件的拷贝
      • 4.硬盘上的文件==>字节数组,字节数组==>硬盘上的文件
      • 5.自己做一个工具
    • 2.字符流
      • 1.Reader
        • 1.方法
        • 2.源头为硬盘上的文件
        • 3.缓冲输入流
      • 2.Writer
        • 1.方法
        • 2.源头为硬盘上的文件
        • 3.缓冲输出流
    • 3.字节流==>字符流
    • 4.数据流
    • 5.对象流
      • 1.对象流==>字节数组
      • 2.对象流==>文件
    • 6.打印流
      • 1.PrintStream
      • 2.PrintWriter
    • 7.文件的分割和合并
    • 8.CommonsIO
      • 1.文件大小和目录大小
      • 2.列出子孙级
      • 3.读取内容
      • 4.写出内容
      • 5.拷贝
  • 24.多线程
    • 1.概念
    • 2.创建线程的3种方式
      • 1.继承Thread类创建线程
      • 2.实现Runnable接口创建线程(推荐)
      • 3.实现callable接口创建线程
    • 3.静态代理
    • 4.lambda
    • 5.线程状态
      • 1.生命周期
      • 2.线程的方法
      • 3.终止线程
      • 4.暂停线程
      • 5.礼让线程
      • 6.插队线程
    • 6.线程的优先级
    • 7.守护线程
    • 8.常用的其他方法
    • 9.线程同步
  • 25.网络编程
    • 1.ip
    • 2.端口
    • 3.URL
    • 4.UDP
  • 26.反射技术
    • 1.注解
      • 1.概述
      • 2.内置注解
      • 3.自定义注解
        • 1.概述
        • 2.元注解
          • 1.target
          • 2.retention
    • 2.java动态性
      • 1.反射机制
        • 1.获取某个class的详细信息
      • 2.动态编译
      • 3.动态执行JavaScript代码
      • 4.动态字节码操作
  • 27.正则表达式
  • 28.JDBC
  • 29.gradle
    • 1.项目结构
    • 2.groovy语言
      • 1.基本语法
      • 2.闭包
    • 3.
  • 30.spring

1.变量

1.局部变量

方法或语句块内部定义的变量。生命周期是从声明位置开始到方法或语句块执行完毕为止。
局部变量在使用前必须先声明、赋储值,再使用。

2.成员变量

方法外部,类的内部定义的变量。从属于对象,生命周期伴随对象始终。

3.静态变量

使用static定义。从属于类,生命周期伴随类始终,从类加载到卸载。

2.常量

一个固定的值。

final String name="jiongwen"

3.数据类型

1.基本数据类型

1.数值型

1.整数类型

类型 占用存储空间 表述范围
byte 1byte -128~127
short 2byte -32768~32767
int 4byte -231~231-1
long 8byte -263~263-1 (long类型写完数字之后最后面要加L,比如7400000000L)
进制类型 特点 例子
十进制 99,500,0
八进制 0(零)开头 015
十六进制 0(零)x或0(零)X开头 0x15
二进制 0(零)b或0(零)B开头 0b01110011

2.浮点类型

类型 占用存储空间 表述范围
float 4byte -3.403E38~3.403E38(float类型写完之后最后面要加F,比如3.14F)
double 8byte -1.798E308~1.798E308

2.字符类型(2byte)

用单引号来表示字符常量。
字符类型用来表示在Unicode编码表中的字符,Unicode编码被设计用来处理各种语言的文字,它占2个字节,可允许有65536个字节。他们也可以从’\u0000’到’\uFFFF’之间的十六进制值来表示。
字符和数字可以进行数学运算,字符会变成编码表中的序号。

char test1='a';
char test2='额';
char test3='\u0061';

3.布尔型(1bit)

布尔类型只有2个常量值:true和false。

2.引用数据类型(4byte)

1.类

2.接口

3.数组

4.运算符

1.算数运算符

1.一元运算符

++,–

2.二元运算符

+,-,*,/,%属于二元运算符
运算准则:
整数运算:
1.如果有2个操作数中有一个是Long,则结果也是Long
2.没有Long时,结果为int。即使操作数全为short,byte,结果也是int。
浮点运算:
1.如果两个操作数中有一个为double,则结果为double。
2.只有两个操作数都是float,则结果才为float。
取模运算:
1.其操作数可以为浮点数,一般使用整数,结果是“余数”,“余数”符号和左边操作数相同,
如:7%3=1,-7%3=-1,7%-3=1。

2.运算及扩展运算符

运算符 用法举例 等效的表达式
+= a+=b a=a+b
-= a-=b a=a-b
*= a*=b a=a*b
/= a/=b a=a/b
%= a%=b a=a%b

3.逻辑运算符

运算符 说明
逻辑与(&) 两个操作数为true,结果才是true,否则是false
逻辑或(丨) 两个操作数有一个是true,结果就是true
短路与(&&) 只要有一个为false,则直接返回false
短路或(丨丨)只要有一个为true,则直接返回true
逻辑非(!) 取反
逻辑异或(^) 相同为false,不同为true

4.位运算符

位运算符 说明
- 取反
& 按位与(两个都是1的时候结果才是1,其他都是0)
按位或(只要有一个是1,结果就是1)
^ 按位异或(比较每个位,位相同则0,位不相同则1)
<< 左移运算符,左移一位相当于乘2
>> 右移运算符,右移一位相当于除2取商

5.字符串连接符

“+”运算符两侧的操作数只要有一个是字符串(String)类型,系统会自动将另一个操作数转换为字符串然后再进行连接。

6.条件运算符(三元运算符)

x ? y:z
//如果x为真,那么表达式结果为y,否则结果为z
int score=80;
String type=score<60?"不及格":"及格";

5.类型转换

1.自动类型转换

自动类型转换指的是容量小的数据类型可以自动转换为容量大的数据类型。
下图的红色线表示无数据丢失的自动类型转换,蓝色虚线表示在转换时可能会有精度的损失。

byte a=1;
short b=2;
int c=a+b;
//隐式转换的时候自动将a和b转换成int之后再相加,所以c必须是int

int a1=324;
long b1=a1;
double d1=b1;
float f=234324L;

2.强制类型转换

语法格式:
(type)var

6.获取键盘输入

https://blog.csdn.net/qq_40164190/article/details/81917208

import java.util.Scanner;
class test {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);//创建键盘录入对象
        System.out.println("请输入第一个整数:");
        int x=sc.nextInt();//将键盘录入的数据存储在x中
        System.out.println(x);
    }
}

7.面向对象

1.类的格式

//每一个源文件必须有且只有一个public class,并且类名和文件名保持一致!
public class Main {
//一个java文件可以同时定义多个class

    int id; //成员变量
    String name;//成员变量
    int selery;
    Computer comp;

    void study(){//成员方法
        System.out.println("s"+comp.brand);
    }

    public Main(int _x,String _y){//构造方法,用于创建这个类的对象
        id=_x;
        name=_y;
    }



    //程序执行的入口,必须要有
    public static void main(String[] args){
        Main mm=new Main(1001,"jiongwen");//通过构造方法创建Main类的对象
        mm.selery=10000;


        Computer c1=new Computer();
        c1.brand="acer";
        mm.comp=c1;


        mm.study();//调用对象的方法
    }
}

class Computer{
    String brand;
}

2.构造方法



3.this关键字



4.static关键字

静态成员方法里面只能使用静态成员变量或静态成员方法。

5.静态初始化块


6.参数传值机制

7.继承

1.概述

2.子类重写父类的方法


3.super

1.概述

2.通过super调用被子类覆盖掉的父类方法

3.构造方法里的super

所有的构造方法里的第一句是super();意思是调用父类里的构造方法。

8.判断某个对象是否属于某个类的(包括这个类的祖先)

9.封装

1.封装的有点

2.封装的实现,使用访问控制符

3.封装的使用细节


10.多态

1.概述

2.多态的要点


11.对象的转型

12.final关键字

13.抽象方法和抽象类

1.概述

2.特点


14.接口

1.声明格式

2.定义接口的详细说明

3.要点

4.一个类可以实现多个接口

5.接口可以继承多个接口

15.内部类

1.成员内部类

1.静态内部类


2.非静态内部类


2.匿名内部类




3.局部内部类


8.jvm内存分析

1.栈stack

特点:
1.栈描述的是方法执行的内存模型。每个方法被调用都会创建一个栈帧(存储局部变量、操作数、方法出口等)
2.jvm是每个线程创建一个栈,用于存放该线程执行方法的信息(实际参数、局部变量等)
3.栈属于线程私有,不能实现线程的共享
4.栈的存储特性是“先进后出,后进先出”
5.栈是由系统自动分配,速度快!栈是一个连续的内存空间

2.堆heap

特点:
1.堆用于存储创建好的对象和数组(数组也是对象)
2.jvm只有一个堆,被所有线程共享
3.堆是一个不连续的内存空间,分配灵活,速度慢

3.方法区method area

特点:
1.jvm只有一个方法区,被所有线程共享
2.方法区实际也是堆,只是用于存储类、常量相关的信息
3.用来存放程序中永远是不变或唯一的内容。(类信息【class对象】,静态变量, 字符串常量等)

9.java的包机制


10.修饰符

作用域 当前类 同一package 子孙类 其他package
public
protected ×
friendly × ×
private × × ×

11.Object类

1.概述

2.toString

1.概述

2.object类里的toString

3.equals

1.equals和==


2.object类里的equals方法

3.原始的equals方法

跟==用法完全一样

4.重写equals方法

5.String类型的equals方法

String.class里重写了equals方法,重写成只要字符串内容相等即两个对象就相等
所以比较两个字符串是否相等的话

12.数组

1.概述

2.特点


3.数组的声明

4.创建数组

5.数组的长度

array.length

6.数组的遍历

public class test {
    public static void bbb(int[] arr){
        for (int i=0;i<arr.length;i++){
            System.out.println(arr[i]);
        }
    }
    public static void main(String[] args) {
        int[] arr2={4,5,6};
        bbb(arr2);
    }

}

7.数组的拷贝

8.数组中删除指定索引的元素(封装成了方法)


9.数组中指定索引里增加元素(数组的扩容,封装成了方法)

    public static String[] addArray(String[] s,String[] d,int index){
        String[] ns=new String[s.length+d.length];
        System.arraycopy(s,0,ns,0,index);
        System.arraycopy(d,0,ns,index,d.length);
        System.arraycopy(s,index,ns,index+d.length,s.length-index);
        return ns;
    }


    public static void main(String[] args){
        String[] s={"腾讯","阿里","百度","京东"};
        String[] d={"三星数据","国民银行"};
        s=addArray(s,d,2);
        System.out.println(Arrays.toString(s));
        //[腾讯, 阿里, 三星数据, 国民银行, 百度, 京东]
    }

10.数组工具类

1.打印数组


2.数组元素的排序

1.数组元素是基本数据类型

2.数组元素是引用数据类型

3.查寻数组中某个元素的索引

1.二分查找法

如果查找的元素不存在的话返回-1

11.多维数组

1.概述

2.二维数组的声明

13.String类

1.String基础


2.String类常用方法



3.可变字符序列

1.StringBuilder

线程不安全、效率高(一般使用它)。

2.StringBuffer

线程安全、效率低。

3.使用方法

.setCharAt(index,char)//替换指定索引的字符


4.可变字符序列和不可变字符序列效率对比

图2比图1更加效率

14.算法

1.冒泡排序

1.概述

2.使用java实现

2.二分法查找

1.概述

2.使用java实现

15.包装类

1.概述

2.作用

3.基本使用方法

4.自动装箱和拆箱

1.概述

2.基本用法

3.注意点

要留意是否是空指针

5.包装类的缓存问题

16.枚举


17.容器

1.数组的优劣性


2.容器成员图

3.泛型

1.概述


2.自定义泛型

3.容器中使用泛型

4.collection接口

1.概述

2.方法

import java.util.*;
public class Main {
    public static void main(String[] args){
        Collection<String> c=new ArrayList<>();
        System.out.println(c.isEmpty());
        c.add("朴炯文");
        c.add("炯文");
        c.add("朴炯文");
        Object[] objs=c.toArray();//转化成obj数组
        System.out.println(Arrays.toString(objs));
        System.out.println(c.contains("炯文"));
        System.out.println(c.size());
        System.out.println(c);
        c.remove("朴炯文");//只移除第一个
        System.out.println(c);
        c.clear();//移除所有的元素
    }
}

3.List

1.概述

2.List接口的方法


2.ArrayList

1.特点

3.LinkedList

1.特点

4.Vector

4.Set

1.概述

2.HashSet

3.TreeSet

5.Collections工具类

1.概述

2.对于List


5.Map接口

0.5 map种类

HashMap,TreeMap,Hashtable,LinkedHashMap

1.概述

2.方法



3.HashMap

1.概述


4.HashTable

5.TreeMap

1.概述

6.使用Iterator迭代器遍历容器元素(List,Set,Map)

1.迭代器遍历List

2.迭代器遍历Set

3.迭代器遍历Map


7.遍历集合的方法总结


18.处理时间的类

1.概述

2.Date时间类

import java.util.Date;
public class jiongwen {
	public static void main(String[] args) {
		Date d1=new Date();//不传参数的话输出当前时间
		System.out.println(d1);//Mon Nov 26 13:04:49 CST 2018
		Date d2=new Date(2000);//传参数的话输出从1970年1月1号0点开始的时间(毫秒),时区不同,结果也不同
		System.out.println(d2);//Thu Jan 01 08:00:02 CST 1970
	}
}

3.DateFormat类和SimpleDateFormat类




4.Calendar日历类





19.Math类

1.概述

2.常用方法


20.Random类


21.File类

1.文件的创建

2.访问文件的属性

3.创建空文件或目录(在该对象所指的文件或目录不存在的情况下)


4.综合应用

5.API


6.字符集

1.编码


2.解码

22.异常机制

1.概述


2.异常的分类

1.RuntimeException运行时异常

2.CheckedException已检查异常

1.捕获异常


2.声明异常(throw子句)


3.自定义异常



23.IO流

1.字节流

1.InputStream

1.方法

2.源头为硬盘上的文件

	//每次读一个字节
        //1.创建流
        File src=new File("abc.txt");
        //2.选择流
        InputStream is=null;
        try {
            is=new FileInputStream(src);
            //3.读取
            int temp;	//一个一个字节去读
            while((temp=is.read())!=-1){	
                System.out.println((char)temp);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try{
                if(is!=null){
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
	//每次读好几个字节
        //1.创建流
        File src=new File("abc.txt");
        //2.选择流
        InputStream is=null;
        try {
            is=new FileInputStream(src);
            //3.读取
            byte[] car=new byte[3]; //每次读3个字节
            int len=-1; //接受长度
            while((len=is.read(car))!=-1){
                //字节数组==>字符串
                String str=new String(car,0,len);
                System.out.print(str);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try{
                if(is!=null){
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

3.字节数组输入流

public static void main(String[] args) {
		//1、创建源
		byte[] src = "talk is cheap show me the code".getBytes();
		//2、选择流
		InputStream  is =null;
		try {
			is =new ByteArrayInputStream(src);
			//3、操作 (分段读取)
			byte[] flush = new byte[5]; //缓冲容器
			int len = -1; //接收长度
			while((len=is.read(flush))!=-1) {
				//字节数组-->字符串 (解码)
				String str = new String(flush,0,len);
				System.out.println(str);
			}		
		
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			//4、释放资源
			try {
				if(null!=is) {
					is.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

4.缓冲输入流

public class BufferedTest01 {

	public static void main(String[] args) {
		File src = new File("abc.txt");
		//2、选择流
		InputStream  is =null;
		try {
			is =new BufferedInputStream(new FileInputStream(src));
			//3、操作 (分段读取)
			byte[] flush = new byte[1024]; //缓冲容器
			int len = -1; //接收长度
			while((len=is.read(flush))!=-1) {
				//字节数组-->字符串 (解码)
				String str = new String(flush,0,len);
				System.out.println(str);
			}		
		
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			//4、释放资源
			try {
				if(null!=is) {
					is.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}			
		}
	}
	
	public static void test1() {
		//1、创建源
		File src = new File("abc.txt");
		//2、选择流
		InputStream  is =null;
		BufferedInputStream bis =null;
		try {
			is =new FileInputStream(src);
			bis = new BufferedInputStream(is);
			//3、操作 (分段读取)
			byte[] flush = new byte[1024]; //缓冲容器
			int len = -1; //接收长度
			while((len=is.read(flush))!=-1) {
				//字节数组-->字符串 (解码)
				String str = new String(flush,0,len);
				System.out.println(str);
			}		
		
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			//4、释放资源
			try {
				if(null!=is) {
					is.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				if(null!=bis) {
					bis.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}

2.OutputStream

1.方法

2.源头为硬盘上的文件

        //1.创建流
        File src=new File("abc.txt");
        //2.选择流
        OutputStream os=null;
        try {
            os=new FileOutputStream(src,false);//true代表开启插入模式,false表示开启重写模式(默认是false)
            //3.操作
            String msg="Must success";
            byte[] datas=msg.getBytes();
            os.write(datas,0,datas.length);
            os.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try{
                if(os!=null){
                    //4.释放资源
                    os.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

3.字节数组输出流

public static void main(String[] args) {
		//1、创建源
		byte[] dest =null;
		//2、选择流 (新增方法)
		ByteArrayOutputStream baos =null;
		try {
			baos = new ByteArrayOutputStream();
			//3、操作(写出)
			String msg ="show me the code";
			byte[] datas =msg.getBytes(); // 字符串-->字节数组(编码)
			baos.write(datas,0,datas.length);
			baos.flush();
			//获取数据
			dest = baos.toByteArray();
			System.out.println(dest.length +"-->"+new String(dest,0,baos.size()));
		}catch(FileNotFoundException e) {		
			e.printStackTrace();
		}catch (IOException e) {
			e.printStackTrace();
		}finally{
			//4、释放资源
			try {
				if (null != baos) {
					baos.close();
				} 
			} catch (Exception e) {
			}
		}
	}

4.缓冲输出流

public class BufferedTest02 {

	public static void main(String[] args) {
		//1、创建源
		File dest = new File("dest.txt");
		//2、选择流
		OutputStream os =null;
		try {
			os =new BufferedOutputStream( new FileOutputStream(dest));
			//3、操作(写出)
			String msg ="IO is so easy\r\n";
			byte[] datas =msg.getBytes(); // 字符串-->字节数组(编码)
			os.write(datas,0,datas.length);
			os.flush();
		}catch(FileNotFoundException e) {		
			e.printStackTrace();
		}catch (IOException e) {
			e.printStackTrace();
		}finally{
			//4、释放资源
			try {
				if (null != os) {
					os.close();
				} 
			} catch (Exception e) {
			}
		}
	}

}

3.文件的拷贝

public static void copy(String srcPath,String destPath) {
		//1、创建源
			File src = new File(srcPath); //源头
			File dest = new File(destPath);//目的地
			//2、选择流
			InputStream  is =null;
			OutputStream os =null;
			try {
				is =new FileInputStream(src);
				os = new FileOutputStream(dest);		
				//3、操作 (分段读取)
				byte[] flush = new byte[1024]; //缓冲容器
				int len = -1; //接收长度
				while((len=is.read(flush))!=-1) {
					os.write(flush,0,len); //分段写出
				}			
				os.flush();
			}catch(FileNotFoundException e) {		
				e.printStackTrace();
			}catch (IOException e) {
				e.printStackTrace();
			}finally{
				//4、释放资源 分别关闭 先打开的后关闭
				try {
					if (null != os) {
						os.close();
					} 
				} catch (IOException e) {
					e.printStackTrace();
				}
				
				try {
					if(null!=is) {
						is.close();
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
	}
public static void copy2(String srcPath,String destPath) {
		//1、创建源
			File src = new File(srcPath); //源头
			File dest = new File(destPath);//目的地
			//2、选择流		
			try(InputStream  is=new FileInputStream(src);
					OutputStream os = new FileOutputStream(dest);	) {				
				//3、操作 (分段读取)
				byte[] flush = new byte[1024]; //缓冲容器
				int len = -1; //接收长度
				while((len=is.read(flush))!=-1) {
					os.write(flush,0,len); //分段写出
				}			
				os.flush();
			}catch(FileNotFoundException e) {		
				e.printStackTrace();
			}catch (IOException e) {
				e.printStackTrace();
			}
	}

4.硬盘上的文件==>字节数组,字节数组==>硬盘上的文件

public class IOTest09 {

	public static void main(String[] args) {
		//图片转成字节数组
		byte[] datas = fileToByteArray("p.png");
		System.out.println(datas.length);
		byteArrayToFile(datas,"p-byte.png");		
	}
	/**
	 * 1、图片读取到字节数组
	 * 1)、图片到程序  FileInputStream
	 * 2)、程序到字节数组	ByteArrayOutputStream
	 */
	public static byte[] fileToByteArray(String filePath) {
		//1、创建源与目的地
		File src = new File(filePath);
		byte[] dest =null;
		//2、选择流
		InputStream  is =null;
		ByteArrayOutputStream baos =null;
		try {
			is =new FileInputStream(src);
			baos = new ByteArrayOutputStream();
			//3、操作 (分段读取)
			byte[] flush = new byte[1024*10]; //缓冲容器
			int len = -1; //接收长度
			while((len=is.read(flush))!=-1) {
				baos.write(flush,0,len);		 //写出到字节数组中			
			}		
			baos.flush();
			return baos.toByteArray();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			//4、释放资源
			try {
				if(null!=is) {
					is.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;		
	}
	/**
	 * 2、字节数组写出到图片
	 * 1)、字节数组到程序 ByteArrayInputStream
	 * 2)、程序到文件 FileOutputStream
	 */
	public static void byteArrayToFile(byte[] src,String filePath) {
		//1、创建源
		File dest = new File(filePath);
		//2、选择流
		InputStream  is =null;
		OutputStream os =null;
		try {
			is =new ByteArrayInputStream(src);
			os = new FileOutputStream(dest);
			//3、操作 (分段读取)
			byte[] flush = new byte[5]; //缓冲容器
			int len = -1; //接收长度
			while((len=is.read(flush))!=-1) {
				os.write(flush,0,len);			//写出到文件	
			}		
			os.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			//4、释放资源
			try {
				if (null != os) {
					os.close();
				} 
			} catch (Exception e) {
			}
		}
	}
}

5.自己做一个工具

public class FileUtils {

	public static void main(String[] args) {
		//文件到文件
		try {
			InputStream is = new FileInputStream("abc.txt");
			OutputStream os = new FileOutputStream("abc-copy.txt");
			copy(is,os);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		//文件到字节数组
		byte[] datas = null;
		try {
			InputStream is = new FileInputStream("p.png");
			ByteArrayOutputStream os = new ByteArrayOutputStream();
			copy(is,os);
			datas = os.toByteArray();
			System.out.println(datas.length);
		} catch (IOException e) {
			e.printStackTrace();
		}
		//字节数组到文件
		try {
			InputStream is = new ByteArrayInputStream(datas);
			OutputStream os = new FileOutputStream("p-copy.png");
			copy(is,os);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 对接输入输出流
	 * @param is
	 * @param os
	 */
	public static void copy(InputStream is,OutputStream os) {		
			try {			
				//3、操作 (分段读取)
				byte[] flush = new byte[1024]; //缓冲容器
				int len = -1; //接收长度
				while((len=is.read(flush))!=-1) {
					os.write(flush,0,len); //分段写出
				}			
				os.flush();
			}catch(FileNotFoundException e) {		
				e.printStackTrace();
			}catch (IOException e) {
				e.printStackTrace();
			}finally{
				//4、释放资源 分别关闭 先打开的后关闭
				close(is,os);
			}
	}
	/**
	 * 释放资源
	 * @param is
	 * @param os
	 */
	public static void close(InputStream is ,OutputStream os) {
		try {
			if (null != os) {
				os.close();
			} 
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		try {
			if(null!=is) {
				is.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}	
	/**
	 * 释放资源
	 * @param ios
	 */
	public static void close(Closeable... ios) {
		for(Closeable io:ios) {
			try {
				if(null!=io) {
					io.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

public class FileUtils2 {

	public static void main(String[] args) {
		//文件到文件
		try {
			InputStream is = new FileInputStream("abc.txt");
			OutputStream os = new FileOutputStream("abc-copy.txt");
			copy(is,os);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		//文件到字节数组
		byte[] datas = null;
		try {
			InputStream is = new FileInputStream("p.png");
			ByteArrayOutputStream os = new ByteArrayOutputStream();
			copy(is,os);
			datas = os.toByteArray();
			System.out.println(datas.length);
		} catch (IOException e) {
			e.printStackTrace();
		}
		//字节数组到文件
		try {
			InputStream is = new ByteArrayInputStream(datas);
			OutputStream os = new FileOutputStream("p-copy.png");
			copy(is,os);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 对接输入输出流
	 * try ...with...resource
	 * @param is
	 * @param os
	 */
	public static void copy(InputStream is,OutputStream os) {		
			try(is;os) {			
				//3、操作 (分段读取)
				byte[] flush = new byte[1024]; //缓冲容器
				int len = -1; //接收长度
				while((len=is.read(flush))!=-1) {
					os.write(flush,0,len); //分段写出
				}			
				os.flush();
			}catch(FileNotFoundException e) {		
				e.printStackTrace();
			}catch (IOException e) {
				e.printStackTrace();
			}
	}	
}

2.字符流

1.Reader

1.方法

2.源头为硬盘上的文件

public static void main(String[] args) {
		//1、创建源
		File src = new File("abc.txt");
		//2、选择流
		Reader  reader =null;
		try {
			reader =new FileReader(src);
			//3、操作 (分段读取)
			char[] flush = new char[1024]; //缓冲容器
			int len = -1; //接收长度
			while((len=reader.read(flush))!=-1) {
				//字符数组-->字符串
				String str = new String(flush,0,len);
				System.out.println(str);
			}		
		
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			//4、释放资源
			try {
				if(null!=reader) {
					reader.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

3.缓冲输入流

public static void main(String[] args) {
		//1、创建源
		File src = new File("abc.txt");
		//2、选择流
		BufferedReader  reader =null;
		try {
			reader =new BufferedReader(new FileReader(src));
			//3、操作 (分段读取)
			String line =null;
			while((line=reader.readLine())!=null) {
				//字符数组-->字符串
				System.out.println(line);
			}		
		
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			//4、释放资源
			try {
				if(null!=reader) {
					reader.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

2.Writer

1.方法

2.源头为硬盘上的文件

public static void main(String[] args) {
		//1、创建源
		File dest = new File("dest.txt");
		//2、选择流
		Writer writer =null;
		try {
			writer = new FileWriter(dest);
			//3、操作(写出)
			//写法一
//			String msg ="IO is so easy\r\n尚学堂欢迎你";
//			char[] datas =msg.toCharArray(); // 字符串-->字符数组
//			writer.write(datas,0,datas.length);
			//写法二
			/*String msg ="IO is so easy\r\n尚学堂欢迎你";
			writer.write(msg);	
			writer.write("add");		
			writer.flush();*/
			
			//写法三
			writer.append("IO is so easy\r\n").append("尚学堂欢迎你");
			writer.flush();
		}catch(FileNotFoundException e) {		
			e.printStackTrace();
		}catch (IOException e) {
			e.printStackTrace();
		}finally{
			//4、释放资源
			try {
				if (null != writer) {
					writer.close();
				} 
			} catch (Exception e) {
			}
		}
	}

3.缓冲输出流

public static void main(String[] args) {
		//1、创建源
		File dest = new File("dest.txt");
		//2、选择流
		BufferedWriter writer =null;
		try {
			writer = new BufferedWriter(new FileWriter(dest));
			//3、操作(写出)			
			writer.append("IO is so easy");
			writer.newLine();
			writer.append("尚学堂欢迎你");
			writer.flush();
		}catch(FileNotFoundException e) {		
			e.printStackTrace();
		}catch (IOException e) {
			e.printStackTrace();
		}finally{
			//4、释放资源
			try {
				if (null != writer) {
					writer.close();
				} 
			} catch (Exception e) {
			}
		}
	}

3.字节流==>字符流

/**
 * 转换流: InputStreamReader OutputStreamWriter
 * 1、以字符流的形式操作字节流(纯文本的)
 * 2、指定字符集
 * @author TW
 *
 */
public class ConvertTest02 {
	public static void main(String[] args) {
		try(BufferedReader reader =
				new BufferedReader(
						new InputStreamReader(
								new URL("http://www.baidu.com").openStream(),"UTF-8"));
				BufferedWriter writer =
						new BufferedWriter(
								new OutputStreamWriter(
										new FileOutputStream("baidu.html"),"UTF-8"));){
			//3、操作 (读取)
			String msg ;
			while((msg=reader.readLine())!=null) {
				//System.out.println(msg);
				writer.write(msg); //字符集不统一不够出现乱码
				writer.newLine();
			}					
			writer.flush();
		}catch(IOException e) {
			System.out.println("操作异常");
		}
		
		
	}
	public static void test2() {
		//操作网络流  下载百度的源代码
		try(InputStreamReader is =
				new InputStreamReader(new URL("http://www.baidu.com").openStream(),"UTF-8");){
			//3、操作 (读取)
			int temp ;
			while((temp=is.read())!=-1) {
				System.out.print((char)temp);
			}		
			
		}catch(IOException e) {
			System.out.println("操作异常");
		}
	}
	public static void test1() {
		//操作网络流  下载百度的源代码
		try(InputStream is =new URL("http://www.baidu.com").openStream();){
			//3、操作 (读取)
			int temp ;
			while((temp=is.read())!=-1) {
				System.out.print((char)temp); //字节数不够出现乱码
			}		
			
		}catch(IOException e) {
			System.out.println("操作异常");
		}
		
		
	}
}

4.数据流

/**
 * 数据流:
 * 1、写出后读取
 * 2、读取的顺序与写出保持一致
 * 
 * DataOutputStream
 * DataInputStream
 * @author TW
 *
 */
public class DataTest {

	public static void main(String[] args) throws IOException {
		//写出
		ByteArrayOutputStream baos =new ByteArrayOutputStream();
		DataOutputStream dos =new DataOutputStream(new BufferedOutputStream(baos));
		//操作数据类型 +数据
		dos.writeUTF("编码辛酸泪");
		dos.writeInt(18);
		dos.writeBoolean(false);
		dos.writeChar('a');
		dos.flush();
		byte[] datas =baos.toByteArray();
		System.out.println(datas.length);
		//读取
		DataInputStream dis =new DataInputStream(new BufferedInputStream(new ByteArrayInputStream(datas)));
		//顺序与写出一致
		String msg = dis.readUTF(); 
		int age = dis.readInt();
		boolean flag = dis.readBoolean();
		char ch = dis.readChar();
		System.out.println(flag);
	}

}

5.对象流

1.对象流==>字节数组

/**
 * 对象流:
 * 1、写出后读取
 * 2、读取的顺序与写出保持一致
 * 3、不是所有的对象都可以序列化Serializable
 * 
 * ObjectOutputStream
 * ObjectInputStream
 * @author TW
 *
 */
public class ObjectTest {

	public static void main(String[] args) throws IOException, ClassNotFoundException {
		//写出 -->序列化
		ByteArrayOutputStream baos =new ByteArrayOutputStream();
		ObjectOutputStream oos =new ObjectOutputStream(new BufferedOutputStream(baos));
		//操作数据类型 +数据
		oos.writeUTF("编码辛酸泪");
		oos.writeInt(18);
		oos.writeBoolean(false);
		oos.writeChar('a');
		//对象
		oos.writeObject("谁解其中味");
		oos.writeObject(new Date());
		Employee emp =new Employee("马云",400);
		oos.writeObject(emp);
		oos.flush();
		byte[] datas =baos.toByteArray();
		System.out.println(datas.length);
		//读取 -->反序列化
		ObjectInputStream ois =new ObjectInputStream(new BufferedInputStream(new ByteArrayInputStream(datas)));
		//顺序与写出一致
		String msg = ois.readUTF(); 
		int age = ois.readInt();
		boolean flag = ois.readBoolean();
		char ch = ois.readChar();
		System.out.println(flag);
		//对象的数据还原  
		Object str = ois.readObject();
		Object date = ois.readObject();
		Object employee = ois.readObject();
		
		if(str instanceof String) {
			String strObj = (String) str;
			System.out.println(strObj);
		}
		if(date instanceof Date) {
			Date dateObj = (Date) date;
			System.out.println(dateObj);
		}
		if(employee instanceof Employee) {
			Employee empObj = (Employee) employee;
			System.out.println(empObj.getName()+"-->"+empObj.getSalary());
		}
		
	}

}
//javabean 封装数据
class Employee implements java.io.Serializable{
	private transient String name; //该数据不需要序列化
	private double salary;
	public Employee() {
	}
	public Employee(String name, double salary) {
		this.name = name;
		this.salary = salary;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getSalary() {
		return salary;
	}
	public void setSalary(double salary) {
		this.salary = salary;
	}
	
}

2.对象流==>文件

/**
 * 对象流: 1、写出后读取 2、读取的顺序与写出保持一致 3、不是所有的对象都可以序列化Serializable
 * 
 * ObjectOutputStream ObjectInputStream
 * 
 * @author TW
 *
 */
public class ObjectTest02 {

	public static void main(String[] args) throws IOException, ClassNotFoundException {
		// 写出 -->序列化
		ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("obj.ser")));
		// 操作数据类型 +数据
		oos.writeUTF("编码辛酸泪");
		oos.writeInt(18);
		oos.writeBoolean(false);
		oos.writeChar('a');
		// 对象
		oos.writeObject("谁解其中味");
		oos.writeObject(new Date());
		Employee emp = new Employee("马云", 400);
		oos.writeObject(emp);
		oos.flush();
		oos.close();
		// 读取 -->反序列化
		ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream("obj.ser")));
		// 顺序与写出一致
		String msg = ois.readUTF();
		int age = ois.readInt();
		boolean flag = ois.readBoolean();
		char ch = ois.readChar();
		System.out.println(flag);
		// 对象的数据还原
		Object str = ois.readObject();
		Object date = ois.readObject();
		Object employee = ois.readObject();

		if (str instanceof String) {
			String strObj = (String) str;
			System.out.println(strObj);
		}
		if (date instanceof Date) {
			Date dateObj = (Date) date;
			System.out.println(dateObj);
		}
		if (employee instanceof Employee) {
			Employee empObj = (Employee) employee;
			System.out.println(empObj.getName() + "-->" + empObj.getSalary());
		}
		ois.close();
	}
}

6.打印流

1.PrintStream

import java.io.*;

//打印流PrintStream
public class tt {


   public static void main(String[] args) throws FileNotFoundException {
        //打印流System.out
       PrintStream ps=System.out;
       ps.println("打印流");
       ps.println(true);

       ps=new PrintStream(new BufferedOutputStream(new FileOutputStream("print.txt")),true);
       ps.println("打印流");
       ps.println(true);
       ps.close();

       //重定向输出端
       System.setOut(ps);
       System.out.println("change");

       //重定向到控制台
       System.setOut(new PrintStream(new BufferedOutputStream(new FileOutputStream(FileDescriptor.out)),true));
       System.out.println("i am back");
   }
}

2.PrintWriter

import java.io.*;

//打印流PrintWriter
public class tt {


   public static void main(String[] args) throws FileNotFoundException {

       PrintWriter pw=new PrintWriter(new BufferedOutputStream(new FileOutputStream("print.txt")),true);
       pw.println("打印流");
       pw.println(true);
       pw.close();

   }
}

7.文件的分割和合并

/**
 * 面向对象思想封装 分割
 * @author 裴新
 *
 */
public class SplitFile {
	//源头
	private File src;
	//目的地(文件夹)
	private String destDir;
	//所有分割后的文件存储路径
	private List<String> destPaths;
	//每块大小
	private int blockSize;
	//块数: 多少块
	private int size;
	
	public SplitFile(String srcPath,String destDir) {
		this(srcPath,destDir,1024);
	}
	public SplitFile(String srcPath,String destDir,int blockSize) {
		this.src =new File(srcPath);
		this.destDir =destDir;
		this.blockSize =blockSize;
		this.destPaths =new ArrayList<String>();
		
		//初始化
		 init();
	}
	//初始化
	private void init() {
		//总长度
		long len = this.src.length();		
		//块数: 多少块
		this.size =(int) Math.ceil(len*1.0/blockSize);
		//路径
		for(int i=0;i<size;i++) {
			this.destPaths.add(this.destDir +"/"+i+"-"+this.src.getName());
		}
	}
	/**
	 * 分割
	 * 1、计算每一块的起始位置及大小
	 * 2、分割
	 * @throws IOException 
	 */
	public void split() throws IOException {
		//总长度
		long len = src.length();		
		//起始位置和实际大小
		int beginPos = 0;
		int actualSize = (int)(blockSize>len?len:blockSize); 
		for(int i=0;i<size;i++) {
			beginPos = i*blockSize;
			if(i==size-1) { //最后一块
				actualSize = (int)len;
			}else {
				actualSize = blockSize;
				len -=actualSize; //剩余量
			}
			splitDetail(i,beginPos,actualSize);
		}
	}	
	/**
	 * 指定第i块的起始位置 和实际长度
	 * @param i
	 * @param beginPos
	 * @param actualSize
	 * @throws IOException
	 */
	private  void splitDetail(int i,int beginPos,int actualSize ) throws IOException {
		RandomAccessFile raf =new RandomAccessFile(this.src,"r");
		RandomAccessFile raf2 =new RandomAccessFile(this.destPaths.get(i),"rw");
		//随机读取 
		raf.seek(beginPos);
		//读取
		//3、操作 (分段读取)
		byte[] flush = new byte[1024]; //缓冲容器
		int len = -1; //接收长度
		while((len=raf.read(flush))!=-1) {			
			if(actualSize>len) { //获取本次读取的所有内容
				raf2.write(flush, 0, len);
				actualSize -=len;
			}else { 
				raf2.write(flush, 0, actualSize);
				break;
			}
		}			
		raf2.close();
		raf.close();
	}	
	/**
	 * 文件的合并
	 * @throws IOException 
	 */
	public void merge(String destPath) throws IOException {
		//输出流
		OutputStream os =new BufferedOutputStream( new FileOutputStream(destPath,true));	
		Vector<InputStream> vi=new Vector<InputStream>();
		SequenceInputStream sis =null;
		//输入流
		for(int i=0;i<destPaths.size();i++) {
			vi.add(new BufferedInputStream(new FileInputStream(destPaths.get(i))));											
		}
		sis =new SequenceInputStream(vi.elements());
		//拷贝
		//3、操作 (分段读取)
		byte[] flush = new byte[1024]; //缓冲容器
		int len = -1; //接收长度
		while((len=sis.read(flush))!=-1) {
			os.write(flush,0,len); //分段写出
		}			
		os.flush();	
		sis.close();
		os.close();
	}
	public static void main(String[] args) throws IOException {
		SplitFile sf = new SplitFile("src/com/sxt/io/SplitFile.java","dest") ;
		sf.split();
		sf.merge("aaa.java");
	}
}

8.CommonsIO

1.文件大小和目录大小

/**
 * 大小
 * @author 裴新
 *
 */
public class CIOTest01 {

	public static void main(String[] args) {
		//文件大小
		long len =FileUtils.sizeOf(new File("src/com/sxt/commons/CIOTest01.java"));
		System.out.println(len);
		//目录大小
		len = FileUtils.sizeOf(new File("D:/java300/IO_study04"));
		System.out.println(len);
	}

}

2.列出子孙级

/**
 * 列出子孙级
 * @author 裴新
 *
 */
public class CIOTest02 {

	public static void main(String[] args) {
		System.out.println("---------------------");
		Collection<File> files =FileUtils.listFiles(new File("D:\\java300\\IO_study04"),
				EmptyFileFilter.NOT_EMPTY, null);
		for (File file : files) {
			System.out.println(file.getAbsolutePath());
		}
		System.out.println("---------------------");
		 files =FileUtils.listFiles(new File("D:\\java300\\IO_study04"),
					EmptyFileFilter.NOT_EMPTY, DirectoryFileFilter.INSTANCE);
		for (File file : files) {
			System.out.println(file.getAbsolutePath());
		}
		System.out.println("---------------------");
		 files =FileUtils.listFiles(new File("D:\\java300\\IO_study04"),
					new SuffixFileFilter("java"), DirectoryFileFilter.INSTANCE);
		for (File file : files) {
			System.out.println(file.getAbsolutePath());
		}
		System.out.println("---------------------");
		 files =FileUtils.listFiles(new File("D:\\java300\\IO_study04"),
					FileFilterUtils.or(new SuffixFileFilter("java"),
							new SuffixFileFilter("class"),EmptyFileFilter.EMPTY), DirectoryFileFilter.INSTANCE);
		for (File file : files) {
			System.out.println(file.getAbsolutePath());
		}
		
		System.out.println("---------------------");
		 files =FileUtils.listFiles(new File("D:\\java300\\IO_study04"),
					FileFilterUtils.and(new SuffixFileFilter("java"),
							EmptyFileFilter.NOT_EMPTY), DirectoryFileFilter.INSTANCE);
		for (File file : files) {
			System.out.println(file.getAbsolutePath());
		}
	}

}

3.读取内容

public class CIOTest03 {

	public static void main(String[] args) throws IOException {
		//读取文件
		String msg =FileUtils.readFileToString(new File("emp.txt"),"UTF-8");
		System.out.println(msg);
		byte[] datas = FileUtils.readFileToByteArray(new File("emp.txt"));
		System.out.println(datas.length);
		
		//逐行读取1
		List<String> msgs= FileUtils.readLines(new File("emp.txt"),"UTF-8");
		 for (String string : msgs) {
			System.out.println(string);
		}
		//逐行读取2
		LineIterator it =FileUtils.lineIterator(new File("emp.txt"),"UTF-8");
		while(it.hasNext()) {
			System.out.println(it.nextLine());
		}
		
	}

}

4.写出内容

public class CIOTest04 {

	public static void main(String[] args) throws IOException {
		//写出文件
		FileUtils.write(new File("happy.sxt"), "学习是一件伟大的事业\r\n","UTF-8");
		FileUtils.writeStringToFile(new File("happy.sxt"), "学习是一件辛苦的事业\r\n","UTF-8",true);
		FileUtils.writeByteArrayToFile(new File("happy.sxt"), "学习是一件幸福的事业\r\n".getBytes("UTF-8"),true);
		
		//写出列表
		List<String> datas =new ArrayList<String>();
		datas.add("马云");
		datas.add("马化腾");
		datas.add("弼马温");
		
		FileUtils.writeLines(new File("happy.sxt"), datas,"。。。。。",true);
	}

}

5.拷贝

public class CIOTest05 {

	public static void main(String[] args) throws IOException {
		//复制文件
		//FileUtils.copyFile(new File("p.png"),new File("p-copy.png"));
		//复制文件到目录
		//FileUtils.copyFileToDirectory(new File("p.png"),new File("lib"));
		//复制目录到目录
		//FileUtils.copyDirectoryToDirectory(new File("lib"),new File("lib2"));
		//复制目录
		//FileUtils.copyDirectory(new File("lib"),new File("lib2"));
		//拷贝URL内容
		//String url = "https://pic2.zhimg.com/v2-7d01cab20858648cbf62333a7988e6d0_qhd.jpg";
		//FileUtils.copyURLToFile(new URL(url), new File("marvel.jpg"));
		String datas =IOUtils.toString(new URL("http://www.163.com"), "gbk");
		System.out.println(datas);
	}

}

24.多线程

1.概念

2.创建线程的3种方式

1.继承Thread类创建线程

/**
 * 创建线程方式一:
 * 1、创建:继承Thread+重写run
 * 2、启动: 创建子类对象 + start
 * @author 裴新 QQ:3401997271
 *
 */
public class StartThread extends Thread{
	/**
	 * 线程入口点
	 */
	@Override
	public void run() {
		for(int i=0;i<20;i++) {
			System.out.println("一边听歌");
		}
	}
	public static void main(String[] args) {			
		//创建子类对象
		StartThread st =new StartThread();
		//启动 
		st.start(); //不保证立即运行 cpu调用
		//st.run(); //普通方法调用
		for(int i=0;i<20;i++) {
			System.out.println("一边coding");
		}
	}

}

2.实现Runnable接口创建线程(推荐)

/**
 * 创建线程方式二:
 * 1、创建:实现Runnable+重写run
 * 2、启动: 创建实现类对象 +Thread对象+ start
 * 
 * 推荐: 避免单继承的局限性,优先使用接口
 * 方便共享资源
 * @author 裴新 QQ:3401997271
 *
 */
public class StartRun implements Runnable{
	/**
	 * 线程入口点
	 */
	@Override
	public void run() {
		for(int i=0;i<20;i++) {
			System.out.println("一边听歌");
		}
	}
	public static void main(String[] args) {			
		/*//创建实现类对象
		StartRun sr =new StartRun();
		//创建代理类对象
		Thread t =new Thread(sr);
		//启动 
		t.start(); //不保证立即运行 cpu调用
*/		
		new Thread(new StartRun()).start(); //匿名的方式,如果只使用一次的话可以这样写
		
		//st.run(); //普通方法调用
		for(int i=0;i<20;i++) {
			System.out.println("一边coding");
		}
	}

}

3.实现callable接口创建线程

3.静态代理

/**
 * 静态代理
 * 公共接口:
 * 1、真实角色
 * 2、代理角色
 * 
 * @author 裴新 QQ:3401997271
 *
 */
public class StaticProxy {
	public static void main(String[] args) {
		new WeddingCompany(new You()).happyMarry();
		
		//new Thread(线程对象).start();
	}
}
interface Marry{
	void happyMarry();
}
//真实角色
class You implements Marry{

	@Override
	public void happyMarry() {
		System.out.println("you and 嫦娥终于奔月了....");
	}
	
}
//代理角色
class WeddingCompany implements Marry{
	//真实角色
	private Marry target;
	public WeddingCompany(Marry target) {
			this.target = target;
	}
	@Override
	public void happyMarry() {
		ready();
		this.target.happyMarry();
		after();
	}
	
	private void ready() {
		System.out.println("布置猪窝。。。。");
	}
	private void after() {
		System.out.println("闹玉兔。。。。");
	}
}

4.lambda

/**
 * lambda推导 +参数+返回值
 * 
 * @author 裴新 QQ:3401997271
 *
 */
public class LambdaTest03 {
	
	public static void main(String[] args) {
		IInterest interest = (int a,int c)-> {
			System.out.println("i like lambda -->"+(a+c));
			return a+c;
		};
		interest.lambda(100,200);
		
		interest = (a,c)-> {
			System.out.println("i like lambda -->"+(a+c));
			return a+c;
		};
		
		interest.lambda(200,200);
		
		interest = (a,c)-> {
			return a+c;
		};
		
		interest = (a,c)-> a+c;
		
		interest = (a,c)-> 100;
		
		System.out.println(interest.lambda(10, 20));
	}
}
interface IInterest{
	int lambda(int a,int b);
}
//外部类
class Interest implements IInterest{

	@Override
	public int lambda(int a,int c) {
		System.out.println("i like lambda -->"+(a+c));
		return a+c;
	}
	
}
/**
 * lambda推导
 * 
 * @author 裴新 QQ:3401997271
 *
 */
public class LambdaTest04 {
	
	public static void main(String[] args) {
		new Thread(()->{
			for(int i=0;i<100;i++) {
				System.out.println("一边学习lambda");
			}
		}) .start();
		
		new Thread(()->	System.out.println("一边学习奔溃")) .start();
	}
}

5.线程状态

1.生命周期


2.线程的方法

3.终止线程

4.暂停线程

5.礼让线程

6.插队线程

6.线程的优先级

7.守护线程

8.常用的其他方法

9.线程同步

213~~ 232没看

25.网络编程

1.ip

2.端口

3.URL

/**
 * URL: 统一资源定位器,互联网三大基石之一(html http),区分资源
 * 1、协议
 * 2、域名、计算机
 * 3、端口: 默认80
 * 4、请求资源
 *  http://www.baidu.com:80/index.html?uname=shsxt&age=18#a
 * 
 * @author 裴新 QQ:3401997271
 *
 */
public class URLTest01 {

	public static void main(String[] args) throws MalformedURLException {
		URL url = new URL("http://www.baidu.com:80/index.html?uname=shsxt&age=18#a");
		//获取四个值
		System.out.println("协议:"+url.getProtocol());
		System.out.println("域名|ip:"+url.getHost());
		System.out.println("端口:"+url.getPort());
		System.out.println("请求资源1:"+url.getFile());
		System.out.println("请求资源2:"+url.getPath());
		
		//参数
		System.out.println("参数:"+url.getQuery());
		//锚点
		System.out.println("锚点:"+url.getRef());
	}

}

4.UDP

240~250不看

26.反射技术

1.注解

1.概述

2.内置注解


3.自定义注解

1.概述

2.元注解

1.target

2.retention

2.java动态性

1.反射机制


1.获取某个class的详细信息

274~285

2.动态编译

3.动态执行JavaScript代码

4.动态字节码操作

27.正则表达式

regexBuddy工具









/**
 * 测试正则表达式对象的基本用法
 * @author Administrator
 *
 */
public class Demo01 {
	public static void main(String[] args) {
		//在这个字符串:asfsdf23323,是否符合指定的正则表达式:\w+
		//表达式对象
		Pattern p = Pattern.compile("\\w+");
		//创建Matcher对象
		Matcher m = p.matcher("asfsdf2&&3323");
//		boolean yesorno = m.matches();	//尝试将整个字符序列与该模式匹配
//		System.out.println(yesorno);
		
//		boolean yesorno2 = m.find();	//该方法扫描输入的序列,查找与该模式匹配的下一个子序列
//		System.out.println(yesorno2);
		
//		System.out.println(m.find());
//		System.out.println(m.group());
//		System.out.println(m.find());
//		System.out.println(m.group());
		
	
		while(m.find()){
			System.out.println(m.group());	//group(),group(0)匹配整个表达式的子字符串
			System.out.println(m.group(0));
		}
		
	}
}
/**
 * 测试正则表达式对象中分组的处理
 * @author Administrator
 *
 */
public class Demo02 {
	public static void main(String[] args) {
		//在这个字符串:asfsdf23323,是否符合指定的正则表达式:\w+
		//表达式对象
		Pattern p = Pattern.compile("([a-z]+)([0-9]+)");
		//创建Matcher对象
		Matcher m = p.matcher("aa232**ssd445*sds223");
	
		while(m.find()){
			System.out.println(m.group());	//group(),group(0)匹配整个表达式的子字符串
			System.out.println(m.group(1));
			System.out.println(m.group(2));
		}
		
	}
}
/**
 * 测试正则表达式对象的替换操作
 * @author Administrator
 *
 */
public class Demo03 {
	public static void main(String[] args) {
		//表达式对象
		Pattern p = Pattern.compile("[0-9]");
		//创建Matcher对象
		Matcher m = p.matcher("aa232**ssd445*sds223");
		//替换
		String newStr = m.replaceAll("#");
		System.out.println(newStr);
		
		
	}
}
/**
 * 测试正则表达式对象的分割字符串的操作
 * @author Administrator
 *
 */
public class Demo04 {
	public static void main(String[] args) {
		String str = "a232b4334c3434";
		String[] arrs = str.split("\\d+");
		System.out.println(Arrays.toString(arrs));
		
	}
}

28.JDBC








29.gradle

1.项目结构

2.groovy语言

1.基本语法

println("hello")    //输出
def i=19    //定义变量
def list=['a','b',] //定义一个列表
list<<'c'   //往列表中添加元素
println(list.get(2))    //  取出列表中第三个元素
def map=['key1':'value1','key2':'value2',]  //定义一个字典
map.key3='value3'   //往字典中添加一个键值对
println(map.get('key3'))    //获取key3对应的值

2.闭包

什么是闭包?闭包其实就是一段代码块,在groovy中,我们主要是把闭包当参数来使用。

def b1={    //定义一个闭包(没有参数)
    println("闭包(没有参数)")
}
def method1(Closure closure){   //定义个方法
    closure()
}
method1(b1) //结果为   闭包(没有参数)
//######################################
def b2={    //定义一个闭包(有参数)
    v->
        println("闭包(有参数)${v}")
}
def method2(Closure closure){   //定义个方法
    closure("xiaoma")
}
method2(b2) //结果为   闭包(有参数)xiaoma

3.

30.spring

相关文章

    暂无相关文章
相关栏目:

用户点评