I-O

I/O操作的目标

输入:从数据源当中读取数据
输出:将数据写入到数据目的地当中

I/O的流向

例:文件/键盘/网络—输-入—>Java程序—输-出—>文件/键盘/网络
输入和输出是相对于Java程序本身而言的.如果Java程序需要读取数据,就会用到输入;如果Java程序需要把数据写到一个位置,就会用到输出.

I/O分类

三种分类方法

  1. 1)输入流
    2)输出流

  2. 1)字节流 读取数据时以字节为基础,每次读写1个或几个字节
    2)字符流 读取数据时以字符为基础,每次读写1个或几个字符

  3. 1)节点流
    2)处理流
    在节点流的基础上对数据再加工,处理流是节点流的装饰者;节点流是被装饰者.

字节流,字符流

1)字节流
I/O中的核心类
InputStream OutputStream 是所有字节流的父类,是抽象类
FileInputStream FileOutputStream 分别是输入输出最常用的子类常用方法:
常用方法:
1)InputStream:
int read(byte[],int off,int len)
读取数据.(读进来的字节数据数组,偏移量(从第几位开始),一次最多读多少数据)
返回值是读的数据长度
2)OutputStream:
void write(byte[] b,int off,int len)
写入数据(需要写入的数据,偏移量,写入多少数据)
返回值是写入的数据长度

public class TestRead {
    public static void main(String[] args) {
        //声明输入流引用
        FileInputStream fis = null;
        //声明输出流引用
        FileOutputStream fos = null;

        try {
            //生成代表输入流的对象
            fis = new FileInputStream("D:\\code\\HelloWorld\\src\\com\\test0305\\www\\From");//这个参数是需要传输的数据的位置
            //生成代表输出流的对象
            fos = new FileOutputStream("D:\\code\\HelloWorld\\src\\com\\test0305\\www\\To");
            //生成一个字节数组
            byte[] buffer = new byte[100];
            //调用输入流对象的read方法,读取数据
            int temp = fis.read(buffer, 2, buffer.length - 2);//返回值是实际读了多少值
            //调用输出流对象的write方法,读取数据
            fos.write(buffer, 2, temp);

            for (int i = 0; i < buffer.length; i++) {
                System.out.println(buffer[i]);
            }
            String s = new String(buffer);
            //调用一个String对象的trim方法,将会去除掉这个字符串的首尾空格和空字符.
            s = s.trim();
            System.out.println(s);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }                                                                      
}

大文件传输:

byte[] buffer = new byte[1024];
while (true) {
    int temp = fis.read(buffer, 0, buffer.length);
    if (temp == -1) {
        break;
    }
    fos.write(buffer, 0, temp);
}

2)字符流:
字节数入流:父类 Reader <– FileReader
int read(char[] c,,int off, int len)
字符输出流:父类 Writer <– FileWriter
void write(char[] c,int off, int len)

public class CharReadTest {
    public static void main(String[] args) {
        FileReader fr = null;
        FileWriter fw = null;
        try {
            fr = new FileReader("D:\\code\\HelloWorld\\src\\com\\test0401\\www\\From");
            fw = new FileWriter("D:\\code\\HelloWorld\\src\\com\\test0401\\www\\To");
            char[] buffer = new char[4000];
            int temp = fr.read(buffer, 0, buffer.length);
            fw.write(buffer, 0, temp);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {

                fr.close();
                fw.close();
            } catch (Exception e) {
                System.out.println(e);
            }
        }
    }
}

.flush();//刷新
节点流,处理流

1)处理流(BufferedReader)—字符输入处理流

public String readline()//一次性读入一行数据
    throws IOEException;

import java.io.BufferedReader;
import java.io.FileReader;

public class BufferReaderTest {
    public static void main(String agrs[]) {
        FileReader fileReader = null;
        BufferedReader bufferedReader = null;
        try {
            fileReader = new FileReader("D:\\code\\HelloWorld\\src\\com\\test0401\\www\\From1");
            bufferedReader = new BufferedReader(fileReader);//装饰者模式
            String line = null;
            while (true) {
                line = bufferedReader.readLine();
                if (line == null) {
                    break;
                }
                System.out.println(line);
            }

        } catch (Exception e) {
            System.out.println(e);
        } finally {
            try {
                fileReader.close();
                bufferedReader.close();
            } catch (Exception e) {
                System.out.println(e);
            }
        }

    }
}

二进制文件读写

DataInputStream

资源文件properties

以键值对存放的properties文件

key1=value1
key2=value2

//读取文件
Properties properties = new Properties();
InputStream is = new FileInputStream("");
properties.load(is);

文件操作

持久化:把数据从瞬时状态向永久状态转变的过程
方式: 文件操作,数据量较小,格式件单
数据库操作,数据量大,数据复杂

File file = new file("路径");//创建文件
boolean isExist = file.exists();
try{
    if(isExist==false){
        boolean isSuccess = file.createNewFile();//
        if(isSuccess){
            System.out.println("文件创建成功");
            System.out.println("能否执行"+file.canExecute());
            System.out.println("能否读取"+file.canRead());
            System.out.println("能否写入"+file.canWrite());
        }else{
            System.out.println("文件创建失败");
        }
    }else{
    System.out.println("文件已存在");
    }
}
if(file.exists()){
//绝对路径
System.out.println(file.getAbsolutePath());
//文件名
System.out.println(file.getName());
//文件大小 单位字节
System.out.println(file.length());
//文件拓展名
String ex = getExtends(file.getName());
}else{
    System.out.println("文件不存在");
}

public String getExtends(String name){

}

NIO

IO:BIO 阻塞的IO
NIO:非阻塞的IO
组成:
Channels
通道:把缓冲区的内容,写入到目标文件中,把目标文件的内容,读取到缓冲区
Buffers 缓冲区:划分出的制定大小的内存区域
Selectors:选择器

//创建缓冲区
ByteBuffer bb = ByteBuffer.allocate(1024);
//向缓冲区中写入内容
bb.put("你好".getBytes());
//切换缓冲区读模式
bb.flip();
//读取内容
byte[] ary = new byte[bb.limit()];
bb.get(ary);
System.out.println(new String(ary));
bb.clear();

capacity:Buffer的固定大小值.
position
写数据到buffer中时,表示当前位置,初始值为0,当一个数据写到buffer后,position移动到下一个buffer单元.最大值为capacity-1
读取数据时,position重置为0

Buffer.mark();标记Buffer中的一个特定position

FileOutputStream fos = null;
FileChannel fc = null;
try {
    fos = new FileOutputStream("D:\\code\\HelloWorld\\resources\\To", true);
    //获取通道
    fc = fos.getChannel();
    //创建缓冲区
    ByteBuffer buffer = ByteBuffer.allocate(1045);
    //向缓冲区写入数据
    buffer.put("愚かの人間目".getBytes());
    buffer.flip();
    //通过通道写入目标文件
    fc.write(buffer);

} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
} finally {
    //关闭资源
    try {
        if (fc != null) {
            fc.close();
        }
        if (fos != null) {
            fos.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}        

大文件传输

//创建通道
FileChannel inChannel = null;
FileChannel outChannel = null;
try {
    //读取大文件
    inChannel = new RandomAccessFile("D:\\code\\HelloWorld\\resources\\源码笔记.rar", "r").getChannel();
    //写入大文件
    outChannel = new RandomAccessFile("D:\\code\\HelloWorld\\源码笔记.rar", "rw").getChannel();
    //把内存映射为缓冲区
    MappedByteBuffer map = inChannel.map(FileChannel.MapMode.READ_ONLY, 0, inChannel.size());
    //把内存缓冲区的内容写到输出管道
    outChannel.write(map);
    System.out.println("read over");
} catch (IOException e) {
    e.printStackTrace();
} finally {
    try {
        if (inChannel != null) {
            inChannel.close();
        }
        if (outChannel != null) {
            outChannel.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

XML文件

可扩展标记语言
以特定的格式存放数据
特点:
1)跨平台
2)
3)