📜  Java中的字符流与字节流

📅  最后修改于: 2022-05-13 01:55:05.103000             🧑  作者: Mango

Java中的字符流与字节流

是数据序列。 I/O 流 指不太可能是顺序访问文件的方法的流。 I/O 流是指代表不同类型源(例如磁盘文件)的输入源或输出目的地。 Java.io 包提供的类允许您在 Unicode字符流和非 Unicode 文本的字节流之间进行转换。

  • 输入流:从源中读取数据。
  • 输出流:将数据写入目的地。

使用和处理上述任何流时的要点如下:

  1. 字符流的名称通常以 Reader/Writer 结尾,字节流的名称通常以 InputStream/OutputStream 结尾
  2. 示例代码中使用的流是无缓冲的流,效率较低。我们通常将它们与缓冲读取器/写入器一起使用以提高效率。我们将很快讨论使用 BufferedReader/BufferedWriter(用于字符流)和 BufferedInputStream/BufferedOutputStream(用于字节流)类。
  3. 如果不再使用,始终建议关闭流。这样可以确保发生任何错误时不会影响流。
  4. 由于文件可能不存在,上述代码可能无法在在线编译器中运行。

字符流

在Java中,字符是使用 Unicode 约定存储的。字符流自动允许我们逐个字符字符读取/写入数据。例如,FileReader 和 FileWriter 是用于从源读取和写入目标的字符流。

字符流

例子

Java
// Java Program illustrate Reading
// a File in Human Readable
// Format Using FileReader Class
 
// Importing required classes
import java.io.*;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
        throws IOException
    {
 
        // Initially assiging null as we have not read
        // anything
        FileReader sourceStream = null;
 
        // Try block to check for exceptions
        try {
 
            // Reading from file
            sourceStream = new FileReader(
                "/Users/mayanksolanki/Desktop/demo.rtf");
 
            // Reading sourcefile and writing content to
            // target file character by character.
 
            int temp;
 
            // If there is content inside file
            // than read
            while ((temp = sourceStream.read()) != -1)
                System.out.println((char)temp);
           
          // Display message for successfu execution of program
          System.out.print("Program successfully executed");
        }
 
        // finally block that executes for sure
        // where we are closing file connections
        // to avoid memory leakage
        finally {
 
            // Closing stream as no longer in use
            if (sourceStream != null)
                sourceStream.close();
        }
    }
}


Java
// Java Program Illustrate ByteStream Class to
// Copy Contents of One File to Another File
 
// Importing required classes
import java.io.*;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
        throws IOException
    {
 
        // Initially assigning null ot objects for
        // reading and writing to file
        FileInputStream sourceStream = null;
        FileOutputStream targetStream = null;
 
        // Try block to check for exceptions
        try {
 
            // Passing the files via lcoal directory
            sourceStream = new FileInputStream(
                "/Users/mayanksolanki/Desktop/demo.rtf");
            targetStream = new FileOutputStream(
                "/Users/mayanksolanki/Desktop/democopy.rtf");
 
            // Reading source file and writing content to
            // target file byte by byte
            int temp;
 
            // If there is content inside file
            // than read
            while ((temp = sourceStream.read()) != -1)
                targetStream.write((byte)temp);
           
          // Display message for successfu execution of program
          System.out.print("Program successfully executed");
        }
 
        // finally block that executes for sure
        // where we are closing file connections
        // to avoid memory leakage
        finally {
 
            if (sourceStream != null)
                sourceStream.close();
 
            if (targetStream != null)
                targetStream.close();
        }
    }
}


输出:字符字符目标文件

Program successfully executed

字节流

字节流逐字节(8 位)处理数据。例如,FileInputStream 用于从源读取,FileOutputStream 用于写入目标。

例子:

Java

// Java Program Illustrate ByteStream Class to
// Copy Contents of One File to Another File
 
// Importing required classes
import java.io.*;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
        throws IOException
    {
 
        // Initially assigning null ot objects for
        // reading and writing to file
        FileInputStream sourceStream = null;
        FileOutputStream targetStream = null;
 
        // Try block to check for exceptions
        try {
 
            // Passing the files via lcoal directory
            sourceStream = new FileInputStream(
                "/Users/mayanksolanki/Desktop/demo.rtf");
            targetStream = new FileOutputStream(
                "/Users/mayanksolanki/Desktop/democopy.rtf");
 
            // Reading source file and writing content to
            // target file byte by byte
            int temp;
 
            // If there is content inside file
            // than read
            while ((temp = sourceStream.read()) != -1)
                targetStream.write((byte)temp);
           
          // Display message for successfu execution of program
          System.out.print("Program successfully executed");
        }
 
        // finally block that executes for sure
        // where we are closing file connections
        // to avoid memory leakage
        finally {
 
            if (sourceStream != null)
                sourceStream.close();
 
            if (targetStream != null)
                targetStream.close();
        }
    }
}

输出:

Program successfully executed