问:说说Java中有哪些IO流吧?

Java 中的 IO 流(输入/输出流),主要包括以下几种基本类型:

  1. 字节输入/输出流

    • 输入流InputStream 及其子类
    • 输出流OutputStream 及其子类
  2. 字符输入/输出流

    • 输入流Reader 及其子类
    • 输出流Writer 及其子类
  3. 装饰者流:用于增强功能,如缓冲、处理等。

    • 缓冲流BufferedInputStream, BufferedOutputStream, BufferedReader, BufferedWriter
    • 处理流DataInputStream, DataOutputStream, FileReader, FileWriter
  4. 转换流

    • InputStreamReader:将字节输入流转换为字符输入流
    • OutputStreamWriter:将字符输出流转换为字节输出流
  5. 文件相关流

    • FileInputStreamFileOutputStream
    • FileReaderFileWriter
  6. 其他流

    • ByteArrayInputStreamByteArrayOutputStream
    • PipedInputStreamPipedOutputStream
    • SequenceInputStream
    • PrintStreamPrintWriter
    • RandomAccessFile

示例

1. 字节流示例:文件复制

使用 FileInputStreamFileOutputStream 复制文件:

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileCopy {
    public static void main(String[] args) {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream("source.txt");
            fos = new FileOutputStream("destination.txt");
            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = fis.read(buffer)) != -1) {
                fos.write(buffer, 0, bytesRead);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fis != null) fis.close();
                if (fos != null) fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
2. 字符流示例:文件内容读取和写入

使用 FileReaderFileWriter

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class FileReadWrite {
    public static void main(String[] args) {
        FileReader fr = null;
        FileWriter fw = null;
        try {
            fr = new FileReader("source.txt");
            fw = new FileWriter("destination.txt");
            char[] buffer = new char[1024];
            int charsRead;
            while ((charsRead = fr.read(buffer)) != -1) {
                fw.write(buffer, 0, charsRead);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fr != null) fr.close();
                if (fw != null) fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
3. 缓冲流示例:高效文件复制

使用 BufferedInputStreamBufferedOutputStream

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class BufferedFileCopy {
    public static void main(String[] args) {
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            bis = new BufferedInputStream(new FileInputStream("source.txt"));
            bos = new BufferedOutputStream(new FileOutputStream("destination.txt"));
            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = bis.read(buffer)) != -1) {
                bos.write(buffer, 0, bytesRead);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bis != null) bis.close();
                if (bos != null) bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
4. 转换流示例:字节流转换为字符流

使用 InputStreamReaderOutputStreamWriter

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.IOException;

public class ConvertStreamExample {
    public static void main(String[] args) {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        InputStreamReader isr = null;
        OutputStreamWriter osw = null;
        try {
            fis = new FileInputStream("source.txt");
            fos = new FileOutputStream("destination.txt");
            isr = new InputStreamReader(fis, "UTF-8");
            osw = new OutputStreamWriter(fos, "UTF-8");
            char[] buffer = new char[1024];
            int charsRead;
            while ((charsRead = isr.read(buffer)) != -1) {
                osw.write(buffer, 0, charsRead);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (isr != null) isr.close();
                if (osw != null) osw.close();
                if (fis != null) fis.close();
                if (fos != null) fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

基于 Java IO 流的不同用法,帮助大家在实际应用中选择和使用合适的IO流。

你可能感兴趣的:(java,开发语言)