0
点赞
收藏
分享

微信扫一扫

Java--IO流

Sky飞羽 2022-03-11 阅读 25
java

Java--IO流

基础知识

文件在程序中是以流的方式操作的
在这里插入图片描述

创建文件的方式

在这里插入图片描述

//第二种方式
public void create01(){
        File file = new File("C:\\");
        String fileName = "news2.txt";
        File file1 = new File(file,fileName);
        try {
            file1.createNewFile();
            System.out.println("创建成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

常用的一些操作

获取文件信息

目录操作

在这里插入图片描述

public void exercise2(){
        File file = new File("D:\\aa\\bb");
        if(file.exists()){
            System.out.println("已经存在");
        }else {
            file.mkdirs();
            System.out.println("创建成功");
        }
    }

IO流的分类

在这里插入图片描述
在这里插入图片描述

FileInputStream使用

@Test
    public void input1() {
        String filename = "D:\\news2.txt";
        int readData = 0;
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(filename);
            while((readData = fileInputStream.read())!=-1){
                System.out.print((char)readData);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    @Test
    public void input2() {
        String filename = "D:\\news2.txt";
        int readlength = 0;
        byte[] buf = new byte[8];
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(filename);
            while ((readlength = fileInputStream.read(buf))!=-1){
                System.out.print(new String(buf,0,readlength));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

FileOutputStream使用

@Test
    public void writeFile(){
        String fileName = "D:\\news1.txt";
        FileOutputStream fileOutputStream = null;
        try {
            //第一种方式,我们每次写入都将覆盖之前写入的
            //fileOutputStream = new FileOutputStream(fileName);
            //第二种方式,我们每次写入在上一次写入的末尾进行添加
            fileOutputStream = new FileOutputStream(fileName,true);
            //fileOutputStream.write('H');只写入一个字节
            String writeFile = "Txltxl";
            fileOutputStream.write(writeFile.getBytes());//写入一个字节数组
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

FileReader

在这里插入图片描述

跟FileInputStream的使用差不多,只是读取的是字符

FileWriter

在这里插入图片描述

FileWriter使用后, 必须要关闭(close)或刷新(flush),否则写入不到指定的文件!

节点流与处理流

1.节点流可以从一个特定的数据源读写数据,如FileReader、FileWriter
2.处理流(也叫包装流)是“连接”在已存在的流(节点流或处理流)之上,为程序提供更为强大的读写功能,也更加灵活,如BufferedReader、BufferedWriter
在这里插入图片描述
从源码中我们可以看到BufferedReader的属性中包括了一个定义的Reader,这个Reader可以是所有它的子类,这里体现了面向对象的多态。
在这里插入图片描述

BufferedReader

public static void main(String[] args) throws IOException {
        String fileName = "D:\\news1.txt";
        String line;//用于接收读取到的字符串
        BufferedReader bufferedReader = new BufferedReader(new FileReader(fileName));
        while ((line = bufferedReader.readLine())!=null){
            System.out.println(line);
        }
        //对于包装流的关闭,只需要关闭外部流即可,其实关闭的还是节点流
        bufferedReader.close();
    }

BufferedWriter

public static void main(String[] args) throws IOException {
        String fileName = "D:\\news1.txt";
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(fileName));
        bufferedWriter.write("hello,txl");
        //通常我们会在添加一句之后,增加一个换行符,这样就不会只在一行添加
        bufferedWriter.newLine();
        bufferedWriter.write("hello1,txl");
        bufferedWriter.close();
    }

BufferedInputStream与BufferedOutStream

public static void main(String[] args) {
        String srcFile = "D:\\dl.png";
        String desFile = "D:\\txl.jpg";
        int length = 0;
        byte[] buf = new byte[1024];
        BufferedInputStream bufferedInputStream = null;
        BufferedOutputStream bufferedOutputStream = null;
        try {
            bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(desFile));
            bufferedInputStream = new BufferedInputStream(new FileInputStream(srcFile));
            while ((length = bufferedInputStream.read(buf))!=-1){
                bufferedOutputStream.write(buf,0,length);
            }
            System.out.println("拷贝完成!");
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try{
                if(bufferedInputStream!=null){
                    bufferedInputStream.close();
                }
                if(bufferedOutputStream!=null){
                    bufferedOutputStream.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

ObjectInputStream与ObjectOutputStream

序列化和反序列化
(1).序列化就是在保存数据时,保存数据的值和数据类型,反序列化就是在恢复数据时,恢复数据的值和数据类型.
(2).需要让某个对象支持序列化机制,则必须让其类是可序列化的,为了让某个类是可序列化的,该类必须实现如下两个接口之一:

ObjectOutputStream 提供 序列化功能
ObjectInputStream 提供 反序列化功能

public class ObjectOutputStream_ {
    public static void main(String[] args) throws IOException {
        String fileName = "D:\\object.dat";
        ObjectOutputStream obj = new ObjectOutputStream(new FileOutputStream(fileName));
        obj.writeInt(10);
        obj.writeDouble(4.12);
        obj.writeBoolean(true);
        obj.writeObject(new Dog("小黄",12));
        System.out.println("序列化完毕");
        obj.close();
    }
}
class Dog implements Serializable {
    private String name;
    private int age;
    public Dog(String name, int age) {
        this.name = name;
        this.age = age;
    }
}
public class ObjectInputStream_ {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        String filename = "D:\\object.dat";
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filename));
        System.out.println(ois.readInt());
        System.out.println(ois.readDouble());
        System.out.println(ois.readBoolean());
        System.out.println(ois.readObject());
        ois.close();
    }
}

注意事项:
1)读写顺序要一致
2)要求序列化或反序列化对象,需要实现 Serializable
3)序列化的类中建议添加SerialVersionUID,为了提高版本的兼容性
4)序列化对象时,默认将里面所有属性都进行序列化,但除了static或transient修饰的成员
5)序列化对象时,要求里面属性的类型也需要实现序列化接口
6)序列化具备可继承性,也就是如果某类已经实现了序列化,则它的所有子类也已经默认实现了序列化

标准输入输出流

在这里插入图片描述
public final static InputStream in = null;
in 是System的一个属性,编译类型:InputStream
运行类型 : BufferedInputStream
public final static PrintStream out = null;
out 是System的一个属性,编译类型:PrintStream
运行类型 : PrintStream

转换流

当我们将文件保存的编码设置为如下形式时,我们通常读取是按照utf-8读取的,如果仍然按照之前的方法,就会出现乱码,因此需要新的类。
在这里插入图片描述
1.InputStreamReader : Reader的子类,可以将InputStream(字节流)包装成(转换)Reader(字符流)
2.OutputStreamWriter:Writer的子类,实现将OutputStream(字节流)
包装成Writer(字符流)
3.当处理纯文本数据时,如果使用字符流效率更高,并且可以有效解决中文问题,所以建议将字节流转换成字符流
4.可以在使用时指定编码格式(比如utf-8, gbk , gb2312, ISO8859-1等)

InputStreamReader

public class InputStreamReader_ {
    public static void main(String[] args) throws IOException {
        String filename = "D:\\news1.txt";
        InputStreamReader isr = new InputStreamReader(new FileInputStream(filename), "gbk");
        BufferedReader bus = new BufferedReader(isr);
        System.out.println(bus.readLine());
        bus.close();
    }
}

OutputStreamWriter

public class OutputStreamWriter_ {
    public static void main(String[] args) throws IOException {
        String filePath = "D:\\news3.txt";
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(filePath),"gbk");
        outputStreamWriter.write("后端四等奖撒会发生");
        outputStreamWriter.close();
        System.out.println("写出成功");
    }
}

打印流PrintStream与PrintWriter

//在默认情况下,PrintStream 输出数据的位置是 标准输出,即显示器
 PrintStream ps = System.out;
        ps.print("hello");
//去修改打印流输出的位置/设备
System.setOut(new PrintStream("D:\\abc.txt"));
        System.out.println("hellodhasjk");
        ps.close();

PrintWriter与上面那个使用基本类似,可以在控制台打印,也可以在指定的文件中打印

Properties类

1)专门用于读写配置文件的集合类
配置文件的格式:
键=值
键=值
2)注意:键值对不需要有空格,值不需要用引号引起来, 默认类型是String
3) Properties的常见方法

查询unicode的网址:http://tool.chinaz.com/tools/unicode.aspx

public class properties01 {
    public static void main(String[] args) throws IOException {
        //创建properties对象
        Properties properties = new Properties();
        //加载配置文件
        properties.load(new FileReader("src\\mysql.properties"));
        //将信息输出到控制台
        properties.list(System.out);
        //获取到指定的信息
        System.out.println("ip= " + properties.getProperty("ip"));
        System.out.println("user= "+ properties.getProperty("user"));
    }
}
public class properties02 {
    public static void main(String[] args) throws IOException {
        Properties properties = new Properties();
        properties.setProperty("user","yjt");
        properties.setProperty("pwd","123456");
        properties.store(new FileWriter("src\\mysql2.properties"),null);
        System.out.println("创建成功");
    }
}
举报

相关推荐

JAVA--IO流

Java--IO流.基础

Java--IO流(File对象)

Java--IO

JAVA--IO(后)

java io流

Java IO流

0 条评论