您的位置:首页 > 娱乐 > 明星 > 问:说说Java中有哪些IO流吧?

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

2024/12/21 21:43:36 来源:https://blog.csdn.net/li_guolin/article/details/141948618  浏览:    关键词:问:说说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流。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com