IO知识浅谈

337 阅读2分钟

本文已参与「掘力星计划

IO常用的类的使用

  1. File
public static void main(String[] args) {
    //windows 路径使用(需要使用\转义) linux使用/
    String pathName = "E:\Java";
    //File.separator为通用的文件符
    String commonPathName = "E:" + File.separator + "Java";
    File file = new File(pathName);
    System.out.println(file);
    System.out.println(new File(commonPathName));
}

(1)File的常用方法

public static void main(String[] args) {
        String pathName = "E:"+ File.separator+"Java";
        File file = new File(pathName);
        String name = file.getName();
        //获取文件名字
        System.out.println(name);
        //获取文件绝对路径
        System.out.println(file.getAbsolutePath());
        //获取你写的路径
        System.out.println(file.getPath());
        //获取文件的大小
        System.out.println(file.length());
        //获取文件的最后修改时间
        System.out.println(new Date(file.lastModified()));
        //文件是否存在
        System.out.println(file.exists());
        //创建目录
//        boolean mkdirs = file.mkdirs();
        //删除目录
        //file.delete();
        //判读是否文件
        System.out.println(file.isFile());
        //判断是否文件夹
        System.out.println(file.isDirectory());
    }
  1. IO流图

IO流.png

  1. FileInputStream的使用
public static void main(String[] args) throws IOException {
    File file = new File("D:" + File.separator + "test.txt");
    InputStream in = new FileInputStream(file);
    byte[] b = new byte[1024];
    int read = in.read(b);
    System.out.println(read);
    in.close();
    System.out.println(new String(b));
}
  1. FileOutputStream
public static void main(String[] args) throws IOException {
    File file = new File("D:" + File.separator + "test.txt");
    OutputStream outputStream = new FileOutputStream(file);
    String text = "xxx abc 哈哈哈";
    outputStream.write(text.getBytes(StandardCharsets.UTF_8));
    outputStream.flush();
    outputStream.close();
}
  1. ObjectOutputStream和ObjectInputStream 序列化流 用于对象序列化

  2. FilterInputStream和FilterOutStream FilterInputStream是用于封装其它输入流,并为它们提供额外的功能 FilterOutputStream是用于封装其它输出流,并为它们提供额外的功能 FilterInputStream的常见子类有BufferedInputStream,DataInputStream,PushBackInputStream FilterOutputStream的常见子类有BufferedOutputStream,DataOutputStream,PushBacjOutputStream (1)BufferedInputStream和BufferedOutputStream是提供缓存功能

package com.cn;

import java.io.*;

public class BufferedStreamTest {
    public static void main(String[] args) {
        byte[] b = new byte[2];
        try (BufferedInputStream in = new BufferedInputStream(new FileInputStream("D:" + File.separator + "test.txt"))) {
            int len = 0;
            while ((len = in.read(b)) != -1) {
                String s = new String(b, 0, len);
                System.out.println(len + s);
            }
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }
}

(2)DataInputStream和DataOutputStream用于装饰其它流。数据输入流允许应用程序以与机器无关方式从底层输入流中读取基本 Java 数据类型。

package com.cn;

import java.io.*;

public class DataStreamTest {
    public static void main(String[] args) {
        try (DataInputStream in = new DataInputStream(new FileInputStream("D:" + File.separator + "test.txt"));
             BufferedReader r = new BufferedReader(new InputStreamReader(in))) {
            String data;
            while ((data = r.readLine()) != null) {
                System.out.println(data);
            }
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }
}

(3)PushBackInputStream与PushBacjOutputStream

回推流,以允许读取字节,然后再将它们返回(回推)到流中。

  1. 字符流 Reader和Writer 主要使用BufferedReader和BufferedWriter
package com.cn;

import java.io.*;

public class BufferedReaderTest {
    public static void main(String[] args) throws IOException {
        File file;
        FileReader fr = new FileReader("D:"+ File.separator+"test.txt");
        BufferedReader bufferedReader = new BufferedReader(fr);
        String line = null;
        while ((line=bufferedReader.readLine())!=null){
            System.out.println(line);
        }
        bufferedReader.close();
    }
}
package com.cn;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class BuffedWriterTest {
    public static void main(String[] args) throws IOException {
        FileWriter fw = new FileWriter("D:" + File.separator + "test.txt");
        BufferedWriter bufferedWriter = new BufferedWriter(fw);
        bufferedWriter.write("哈哈哈哈");
        bufferedWriter.newLine();
        bufferedWriter.write("123");
        bufferedWriter.flush();
        bufferedWriter.close();
    }
}

字节流和字符流的区别

java中的字节流处理的基本单位为单个字节,它通常用来处理二进制数据。

java中字符流的处理基本单位为Unicode码(2字节)

  • 字节流操作的基本单元为字节;字符流操作的基本单元为Unicode码元。
  • 字节流默认不使用缓冲区;字符流使用缓冲区。
  • 字节流通常用于处理二进制数据,实际上它可以处理任意类型的数据,但它不支持直接写入或读取Unicode码元;字符流通常处理文本数据,它支持写入及读取Unicode码元。