JavaIO流

前言

JavaIO流就是输入输出流,通过流对文件中的数据进行读写。
1、流的分类:
按流的方向:输入输出流
按读取数据的方式:字节流(只能读取普通的文本文件),字符流(可以读取任意类型的文件),需要注意的是中文在java中占两个字节,在windows中一个中文占一个字节
IO的四个抽象的超类:InputStream,OutputStream,Reader,Writer.

2、常用IO类
1)缓冲流:将数据放入到缓冲区,当缓冲区的数据到达一定量的时候在存取,可以直接读取一行
2)文件流:对文件中的数据进行输入输出
3)对象流:对象的序列化和反序列化
4)数据流:存储的数据类型和取出的数据类型必须一致
5)转换流:字节流转字符流
6)随机读写流:指针跳跃。文件暂停下载。
7)标准输入输出流:系统硬件的输入输出设备

3、继承关系图:
字节流:
在这里插入图片描述
字符流:

在这里插入图片描述

一、字节流(Input/OutputStream)

1、 文件流(FileInputStream,FileOutputStream)

read读取一个字节:从文件的第一个字节前开始,依次往下读取,每读取一个字节,指针就会走到下一个字节,如果到达末尾,返回-1.
read读取字节数组,每一次读取都是赋值给指定的偏移量和长度数组中(覆盖),如果到末尾,返回-1

/**
 *读取完成之后,一定要使用close()关闭数据流。
 * 可以一次性独处所有字节,但是如果文件过大,数组的长度不能过大(内存中连续的空间)
 *
 * FileInputStream:
 *        构造方法:可以是路径字符串(绝对路径,相对路径),路径分割符是\,在java中是转移字符,所以需要\\
 *                 File对象
 *      1、read读取一个字节大小(0-256),-1表示读取到文件末尾,也可以读取到指定大小byte数组,
 *      2、可以通过String的构造方法将byte[]转成String,默认是UTF-8解码
 *      3、available(),剩余可用的字节数
 *      4、skip(n),从当前位置跳过指定字节,也就是从第n+1个字节开始读取
 *      5、读取字节数组时,在最后的字节解码时,如果是中文字符很容易乱码,
 *      
 * FileOutputStream:
 *         构造:可以是路径字符串(绝对路径,相对路径),路径分割符是\,在java中是转移字符,所以需要\\
 *              File对象,追加
 *      1、write,写入一个int类型,也可以写入一个byte数组
 *      2、字符编码,通过String的getBytes获取字符串的byte数组,默认是UTF-8编码
 *      3、flush,刷新缓冲区(强制将缓冲区的数据写入到文件中), close方法会自动执行。
 *
 *
 */
public class TestFileStream {
    public static void main(String[] args) {
        //write("test.txt",129);
        write("test.txt",new String("ab").getBytes());
        //System.out.println(readBytes("test.txt"));
        //readAll("test.txt");
        //System.out.println(readInt("test.txt"));

    }

    /**
     * 向指定路径中写入一个int类型的值
     * @param path 路径
     * @param a 写入值
     */

    public static void write(String path ,int a){
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(path);//创建文件输出流,抛出FileNotFoundException,可以追加到文件中,new FileOutputStream("test.txt",true);
            fos.write(a);//写入一个字节,抛出IOException,编码方式?
            fos.flush();//刷新缓冲区,在关闭流会自动执行,将存储在缓冲区中的数据输出到文件
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    /**
     *
     * @param path 路径
     * @param bytes 写入的字节数据
     */
    public static void write(String path ,byte [] bytes){
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(path);//创建文件输出流,抛出FileNotFoundException,可以追加到文件中,new FileOutputStream("test.txt",true);
            fos.write(bytes);//编码方式?
            fos.flush();//刷新缓冲区,在关闭流会自动执行,将存储在缓冲区中的数据输出到文件
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 读取一个字节
     * @param path 读取的路径
     * @return 读取的字节
     */

    public static int readInt(String path){
        int read = -1;
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(path);
            fis.skip(1);//跳跃一个字节
            read=fis.read();//读取一个字节(0-256),可以循环读取,判断是否为-1
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally{
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return read;
    }

    /**
     * 读取字节数组
     * @param path 文件路径
     * @return 字节数组
     */
    public static int readBytes(String path){
        int read = 0;
        FileInputStream fis = null;
        try {
            //fis = new FileInputStream(path);
            byte [] bytes = new byte[15];//设置大小字节数组的大小
            //while((read=fis.read(bytes,0,bytes.length))!=-1) System.out.println(new String(bytes,0,read));//每次读取指定个字节,read表示实际读取到的长度,然后输出字符串:你的名字,-1表示已到达文件末尾,读取完了
            while((read=fis.read(bytes))!=-1) System.out.println(Arrays.toString(bytes));//每次读取指定个字节,read表示实际读取到的长度,输出字节数据[-28, -67, -96, -25, -102, -124, -27, -112, -115, -27, -83, -105, 0, 0, 0]
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally{
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return read;
    }


    /**
     * 读取全部
     * @param path 文件路径
     */
    public static void readAll(String path){
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(path);
            int length = fis.available();//获取所有字节数
            byte [] bytes = new byte[length];//设置数据大小为字节数量,一次性读出
            fis.read(bytes);
            System.out.println(new String(bytes));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally{
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}

2、缓冲流(BufferedInputStream,BufferedOutputStream)

1)当使用BufferedInputStream读取字节文件时,BufferedInputStream会一次性从
文件中读取8192个(8Kb),存在缓冲区中,直到缓冲区装满了,才重新从文件中
读取下一个8192个字节数组。
2)BufferedOutputStream,是将要写入的数据存储到缓冲数组中,数组满了之后,才会写入到文件中
3)原理:先将从硬盘读取的数据存取到8KB的字节数组中,程序通过从内存中的数组读取数据,这样就更快一些。

/**
 * 图片加密和解密(方式一样);
 */
public class MyByteBuffered {
    public static void main(String[] args){
        BufferedInputStream br = null;
        BufferedOutputStream bw = null;
        try{
            br = new BufferedInputStream(new FileInputStream("encryption.jpg"));
            bw = new BufferedOutputStream(new FileOutputStream("fileTest\\decrypt.jpg"));
            byte bytes [] = new byte[1024];
            int read = -1;
            while((read=br.read(bytes,0,bytes.length))!=-1) {encryption(bytes,0,read);bw.write(bytes,0,read);}
            bw.flush();//刷新缓冲区
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 加密和解密一样
     *
     * @param bytes
     * @param off
     * @param length
     */
    public static void encryption(byte [] bytes,int off, int length){
        for(;off<length;off++) {
            if(bytes[off]<0) bytes[off]+=128;
            else bytes[off]-=128;
        }
    }
}

二、字符流(Reader/Writer)

1、文件流(FileReader,FileWriter)

和FileStream是相似的,只不过字符流只能读取文本文件

public class TestFile {
    public static void main(String[] args) {
        write("test.txt","问候你的家人");
        read("test.txt");
    }

    public static void read(String path){
        FileReader fileReader = null;
        try {
            fileReader = new FileReader("test.txt");
            System.out.println("字符编码方式:"+fileReader.getEncoding());
            char [] chars = new char[10];
            int count = 0;
            while((count=fileReader.read(chars))!=-1) System.out.print(new String(chars,0,count));//循环读取字符并且输出
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fileReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    public static void write(String path,String data){
        FileWriter fileWriter = null;
        try {
            fileWriter = new FileWriter(path);
            fileWriter.write(data);
            fileWriter.flush();//刷新
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fileWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

2、转换流(InputStreamReader,OutputStreamWriter)

字节流转字符流

3、缓冲流(BufferedReader,BufferWriter)

缓冲流:相当于基本数据类型和包装类,缓冲流就是对文件流(节点流)的包装(流),包装流关闭只需要关闭最外面的包装类就可以,因为包装类内部关闭了里面的节点流。
缓冲数组8KB,和BufferedInputStream原理一致
BufferedReader:可以读取一行,不会读取换行符.
BufferedWriter:

 public class MyCharBuffer {
    public static void main(String[] args) {
        BufferedWriter bw = null;
        BufferedReader br = null;
        try {
            bw = new BufferedWriter(new FileWriter("fileTest\\fileTestCopy.txt"));
            br = new BufferedReader(new FileReader("fileTest\\fileTest.txt"));
            bw.write("fileTestCopy:\n");
            String data=null;
            while((data=br.readLine())!=null) {bw.write(data);bw.newLine();}
            bw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                bw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

三、数据流(DataInputStream,DataOutputStream)

只能读写入java中的基本数据类型和String类型,FilterInput/OutputStream的子类

public class DataStreamTest {
    public static void main(String[] args) throws IOException {
        DataInputStream dis = new DataInputStream(new FileInputStream("fileTest\\fileTest.txt"));
        DataOutputStream dos = new DataOutputStream(new FileOutputStream("fileTest\\fileTest.txt"));
        dos.writeBoolean(true);
        dos.writeInt(2);
        dos.writeFloat(1.0f);
        dos.writeDouble(2);
        dos.writeUTF("打工人,打工魂");
        dos.flush();

        System.out.println(dis.readInt());//16777216
        System.out.println(dis.readBoolean());//true
        System.out.println(dis.readFloat());//1.0
        System.out.println(dis.readDouble());//2.0
        System.out.println(dis.readUTF());//"打工人,打工魂"
        dis.close();
        dos.close();
    }
}

四、标准输入输出流(PrintStream,InputStream)

1、指操作系统的输入输出设备:鼠标键盘,System.in(InputStream,IO流的基类),System,out(PrintStream,FilterOutputStream子类)。
2、修改流:System.setIn/setOut

/*
*  InputStream
*
*/
public class SystemTest {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String read = "";
        while((read=br.readLine())!=null) {
            if(read.equalsIgnoreCase("e")||read.equalsIgnoreCase("exit")) break;
            System.out.println("请继续输入,e或exit结束输入");
        }
        br.close();//同时会关闭系统的输入流,
        //System.in.read();//流已经关闭
    }
}

```java
/**
 * PrintStream
 * 构造:PrintStream(OutputStream),PrintStream(File)
 * 具有自动flush的功能,不用自己设置flush
 */
public class SystemOut {
    public static void main(String[] args) throws FileNotFoundException {
        FileOutputStream fos = new FileOutputStream("fileTest\\fileTest.txt");//创建输出流
        PrintStream ps = new PrintStream(fos);//转为打印流
        System.setOut(ps);
        int i = 0;
        while(i<128) {//打印ASCII表
            System.out.print((char)i);
            if(i%20==0) System.out.println();
            i++;
        }
    }
}

五、对象流(ObjectInputStream,ObjectOutputStream)

1、序列化:与对象的序列化(内存中程序运行着的类的变量信息(除transient和satic修饰)和方法转换成二进制流,可以保存到本地文件,也可以在网络中传输(redis存储实体类))有关。实体类需要实现可序列化接口(Externalizable或者Serializable),并且定义一个不会改变的序列化版本号(常量),反序列化时,如果我们修改过实体类(根据类中变量,方法是否有过变动,由类的内部细节决定),那么在版本号将会改变,这样反序列化就会失败。所以需要定义为常量(private static final long serialVersionUID=1L;)
2、分开读写无法读取对象,必须在统一程序中完成读写?

public class TestObjectStream {
    public static void main(String[] args) throws IOException {
        Entity entity = new Entity();
        entity.setAge(21);
        entity.setName("ly");
        ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream("objectStream"));
        ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream("objectStream"));

        //outputStream.writeObject(entity);
        outputStream.flush();
        try {
            Entity entity1 = (Entity) inputStream.readObject();
            System.out.println(entity1);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        outputStream.close();
        inputStream.close();
    }
}

六、File

/**
 *   File:操作系统的文件/文件夹,
 *   构造方法:绝对路径/相对路径的文件/路径名,(还有其他的,(String,File),(String,String),(File,String)(前者表示父路径,后者表示子路径))
 *   常用方法:
 *      1、isFile/Direaction--boolean:是否为路径,不能根据判断一个不存在的文件/文件夹是否存在
 *      2、createNewile--boolean:是否为文件,跑出IOException
 *      3、madir(s)--boolean:创建(多级)文件夹
 *      4、exists--boolean:路径是否存在
 *      5、get(Absolute)Path--String:获取相对/绝对路径
 *      6、canRead/Write--boolean:可读可写
 *      7、getParent--String:获取父路径
 *      8、lastModified--long:获取最后 一次跟新的毫秒数
 *
 *
 */
public class FileTest {
    public static void main(String[] args) throws IOException {
        File file = new File("fileTest");
        if(!file.exists())file.mkdir();//如果不存在,创建目录
        System.out.println("isDirectory:"+file.isDirectory());//是否文件夹
        System.out.println("path:"+file.getPath());//相对路径
        System.out.println("isFile:"+file.isFile());
        System.out.println("absolutePath:"+file.getAbsolutePath());//绝对路径
        //file = new File(file.getAbsolutePath()+"\\fileTest.txt");
        file = new File(file,"fileTest.txt");
        if(!file.exists()) file.createNewFile();//不存在,创建文件
        System.out.println("canRead:"+file.canRead());//可读
        System.out.println("canWrite:"+file.canWrite());//可写
        System.out.println("canExecute:"+file.canExecute());//可执行
        System.out.println("lastModifiedTime:"+new Date(file.lastModified()));//最后一次修改时间
        file=new File(file.getParent(),"fileTest2.txt");
        //file = new File("D:\\ly\\idea\\project\\java\\fileTest");
        file.createNewFile();
        //file.delete();//删除文件(夹),不走回收站,而且不能删除非空目录;程序不会报错,也不会有任何效果
    }
}


八、随机读写流(RandomAccessFile)

1、实现了DataInput,DataOutput两个接口,既读又可写。
2、功能:跳读,追加内容
3、利用多线程实现文件暂停下载。


/**
 * 构造方法:string,string
 *          文件刘静,操作类型(r只读,rw读写)
 *  常用方法:
 *     1、getFilePointer--long:获取当前指针位置,从0开始
 *     2、seek(long)--void:指针跳跃到指定位置
 *     3、readInt()--int:读取int类型的值
 *     4、readLine()--String:读取一行的字符串。
 *
 */
public class Test {
    public static void main(String[] args) throws IOException {
        RandomAccessFile rw = new RandomAccessFile("test.txt", "rw");
        long pointer = rw.getFilePointer();
        rw.seek(pointer+1);
        System.out.println(pointer);
        System.out.println(rw.readLine());//从位置1开始获取一行
        rw.close();
    }
}

九、Properties与FileInputStream

Hashtables的子类properties,可以很好的存储键值对的信息,这样存储的信息读取和可视化更方便,不需要我们读取数据再解析数据

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

/**
 * properties的store方法,将properties属性写入到指定的文件中
 * FileOutputStream的load,指定properties类存储键值对
 *
 */
public class test_properties {
    public static void main(String[] args) {
        Properties properties = new Properties();//Hashtable的子类。
        properties.setProperty("1:","liyang");
        properties.setProperty("2:","luohuanpu");
        properties.setProperty("3:","chenjie");
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try{
            fos = new FileOutputStream("test_properties.properties");
            fis = new FileInputStream("test_properties.properties");
            properties.store(fos,"");
            fos.flush();//写入之后刷新通道。
            System.out.println("存入properties文件成功");
            System.out.println("从properties文件中取出数据");
            properties  = new Properties();//新的类
            properties.load(fis);
            Iterator it = properties.entrySet().iterator();
            while(it.hasNext()){
                Map.Entry entry = (Map.Entry) it.next();
                System.out.println("key:"+entry.getKey()+"\t"+"value:"+entry.getValue());
            }
        }catch(IOException e){
            System.out.println(e.getMessage());
        }finally{
            if(fis!=null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(fos!=null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

十、ResourceBundle与类路径

可以说是properties的优化,但是只能读取*.properties文件,

class ResourceBundle {
    public static void main(String[] args) {
        System.out.println("======");
        String path = Thread.currentThread().getContextClassLoader().getResource("").getPath();//获取类路径,
        System.out.println(path);///E:/Java_dev_tool/IDEA/project/JavaSE/out/production/IO/,中文路径可能会乱码
        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("test");//当前类路径下的test.properties文件。中文路径会乱码
        String str = bundle.getString("user");
        System.out.println(str);
    }
}

十一、字符编码

1、字符集和编码
ASCII:7位(0-127)表示常见字符和英文字母
IOS-8859-1:欧洲编码,一个字节,不能表示中文
GBK2312:中文编码,最多两个字节编码所有字符
GBK:能够表示更多的中文字符,最多两个字节编码;
Unicode(字符集):java默认编码,国际标准码,融于了人类所有使用的字符,固定两个字节表示
UTF-8(编码方式):变长编码,1-4个字节表示一个字符,每8个位传输数据(UTF-16:每16位传输数据)
2、字符集只是字符的集合,而对字符对应的字节,如何编码和解码(文字转为计算中字节01位存储,01的字节转为肉眼可见的文字),取决于字符编码

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值