📜  将 InputStream 转换为 String 的Java程序

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

将 InputStream 转换为 String 的Java程序

读取和写入操作是用户在任何应用程序中执行的基本功能。每种编程语言都提供 I/O 流来读取和写入数据。 Java的 FileInputStream 类和 FileOutputStream 类对文件执行 I/O 操作。 FileInputStream 类用于从文件中读取数据,FileOutputStream 类用于将数据写入文件。有几种方法可以使用内置库和外部库在Java中将 InputStream 对象转换为 String。

方法:将 InputStream 对象转换为 String 的三种方法是:

  1. 使用 InputStreamReader 类
  2. 使用 BufferedReader 类
  3. 使用 Scanner 类

方法一:使用InputStreamReader

InputStreamReader 将字节流转换为字符流。它读取字节流并使用指定的字符集将它们解码为字符。如果未指定字符集,则使用默认字符集

在此方法中,用户提供的文件名作为文件对象启动的参数传递。该文件对象再次作为 InputStream 对象启动的参数传递。 InputStreamReader 对象是通过传递 InputStream 对象启动的。 InputStreamReader 将字节流转换为字符流。获取的字符流使用 InputStreamReader 类的 read() 方法存储在字符数组中。最后,字符数组被转换为字符串。

执行:

Java
import java.util.*;
import java.io.*;
public class InputStreamReaderDemo {
  
    public static void main(String[] args)
    {
        Scanner s = new Scanner(System.in);
        System.out.println("Enter the file path: ");
        String filename = s.nextLine();
        
        // Creating a File object
        File file = new File(filename);
        try {
            // Creating an InputStream object
            InputStream is = new FileInputStream(file);
            
            // creating an InputStreamReader object
            InputStreamReader isr = new InputStreamReader(is);
            
            // Creating a character array
            char charArray[] = new char[(int)file.length()];
            
            // Reading the contents of the reader
            isr.read(charArray);
            
            // Converting character array to a String
            String contents = new String(charArray);
            System.out.println(contents);
        }
        catch (IOException e) {
            System.out.println(e);
        }
    }
}


Java
// Importing java libraries
import java.io.*;
import java.util.Scanner;
  
public class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
        Scanner s = new Scanner(System.in);
        System.out.println("Enter the file path: ");
        String filename = s.nextLine();
        try {
            // Creating an InputStream object
            InputStream is = new FileInputStream(filename);
  
            // creating an InputStreamReader object
            InputStreamReader isr
                = new InputStreamReader(is);
  
            // Creating a BufferedReader object
            BufferedReader br = new BufferedReader(isr);
            StringBuffer sb = new StringBuffer();
  
            String str;
  
            while ((str = br.readLine()) != null) {
                sb.append(str);
            }
            System.out.println(sb.toString());
        }
        catch (IOException e) {
            System.out.println(e);
        }
    }
}


Java
// Importing java libraries
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;
  
public class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
  
        // Taking input from the user
        Scanner s = new Scanner(System.in);
        System.out.println("Enter the file path: ");
        String filename = s.nextLine();
        try {
  
            // Creating an InputStream object
            FileInputStream fis
                = new FileInputStream(filename);
  
            // Creating a Scanner object
            Scanner sc = new Scanner(fis);
  
            // Reading line by line from scanner to
            // StringBuffer
  
            StringBuffer sb = new StringBuffer();
            while (sc.hasNext()) {
                sb.append(sc.nextLine());
            }
  
            System.out.println(sb.toString());
        }
  
        // Catch block to handle exceptions
        catch (IOException e) {
            System.out.println(e);
        }
    }
}


输出:

方法二:使用BufferedReader

BufferedReader 类用于从输入源(此处为文件)读取字符流。 BufferedReader 类的 readLine() 方法从缓冲区读取器对象的内容中读取每一行。

在下面的示例中,用户指定传递给 InputStream 构造函数以启动 InputStream 对象的文件名。 InputStream 对象被传递给 InputStreamReader 构造函数。 BufferedReader 对象是使用 InputStreamReader 对象实例化的。创建一个 StringBuffer 对象,它存储从 BufferedReader 对象读取的行。最后,将 StringBuffer 对象的内容转换为 String。

执行:

Java

// Importing java libraries
import java.io.*;
import java.util.Scanner;
  
public class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
        Scanner s = new Scanner(System.in);
        System.out.println("Enter the file path: ");
        String filename = s.nextLine();
        try {
            // Creating an InputStream object
            InputStream is = new FileInputStream(filename);
  
            // creating an InputStreamReader object
            InputStreamReader isr
                = new InputStreamReader(is);
  
            // Creating a BufferedReader object
            BufferedReader br = new BufferedReader(isr);
            StringBuffer sb = new StringBuffer();
  
            String str;
  
            while ((str = br.readLine()) != null) {
                sb.append(str);
            }
            System.out.println(sb.toString());
        }
        catch (IOException e) {
            System.out.println(e);
        }
    }
}

输出:

方法 3:使用Scanner

Scanner 类用于从标准输入读取输入。 Scanner 类具有接受原始类型输入的方法,如整数、双精度、浮点数以及字符串输入。

在下面的示例中,用户指定传递给 InputStream 构造函数以启动 FileInputStream 对象的文件名。然后将 FileInputStream 对象传递给 Scanner 类构造函数。扫描器对象的内容被迭代,提取的每一行都被附加到 StringBuffer 对象。最后,将 StringBuffer 内容转换为 String 并打印到标准输出。

执行:

Java

// Importing java libraries
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;
  
public class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
  
        // Taking input from the user
        Scanner s = new Scanner(System.in);
        System.out.println("Enter the file path: ");
        String filename = s.nextLine();
        try {
  
            // Creating an InputStream object
            FileInputStream fis
                = new FileInputStream(filename);
  
            // Creating a Scanner object
            Scanner sc = new Scanner(fis);
  
            // Reading line by line from scanner to
            // StringBuffer
  
            StringBuffer sb = new StringBuffer();
            while (sc.hasNext()) {
                sb.append(sc.nextLine());
            }
  
            System.out.println(sb.toString());
        }
  
        // Catch block to handle exceptions
        catch (IOException e) {
            System.out.println(e);
        }
    }
}

输出