如何在 Java 中将文件读入字符串

原文:https://www.studytonight.com/java-examples/how-to-read-a-file-to-string-in-java

在本教程中,我们将学习如何在 Java 中读取文件到字符串。将数据存储到文件中对于永久存储很有用。一般我们使用扩展名为*.txt 的文件,这些文件也被称为纯文本文件。虽然文件可以是任何 PDF、DOC 或 SQL 等。在 Java 中,读取文件主要有四类。这些类属于java.io包,用于 Java 中的文件处理。

  • FileReader

  • Scanner

  • FileInputStream

  • BufferedReader

在本教程中,我们将使用这个sampledata.txt 文件来执行读取操作,并且该数据将被转换为字符串。文件数据如下所示。

This is the sample data stored in a file
Rahul:10025
Sohan:10026
Madan:10027
Jack:10028

enlightened代码示例中显示的文件的所有路径都因机器而异。请验证系统中示例文件的路径,并将该路径正确添加到程序中。

使用FileReader类从文件中检索字符串的示例

在下面的例子中,我们使用FileReader 类从文件中读取数据。FileReader 类的一个对象指向一个文件的开头,当它返回-1 时,我们将打印所有内容,因为它指示文件的结尾,数据被附加到一个字符串变量中。

import java.io.*; 
public class StudyTonight 
{ 
    public static void main(String[] args) throws Exception 
    { 
        FileReader fr = new FileReader("E:\\Studytonight\\sampledata.txt"); 
        String fileData = "";
        int c; 
        while((c=fr.read()) != -1) 
          fileData += (char)c; 
        System.out.println(fileData);
    } 
}

这是存储在文件中的样本数据 Rahul:10025 Sohan:10026 马丹:10027 Jack:10028

使用扫描器类从文件中检索字符串的示例

Scanner 类从特定的源获取输入。大多数时候,我们提供System.in作为从键盘输入的输入源。在我们的程序中,我们将从文本文件中读取数据,因此我们将提供该文件作为输入源。

不要忘记添加useDelimiter("\\Z") 方法来分隔文件中的读数。

import java.io.*;  
import java.util.Scanner; 

public class StudyTonight 
{ 
    public static void main(String[] args) throws Exception 
    { 
        File file = new File("E:\\Studytonight\\sampledata.txt"); 
        //Scanner will read all the data from source file
        Scanner sc = new Scanner(file); 
        //Delimiter reads upto the end of input 
        sc.useDelimiter("\\Z"); 
        System.out.println(sc.next()); 
    } 
}

这是存储在文件中的样本数据 Rahul:10025 Sohan:10026 马丹:10027 Jack:10028

使用FileInputStream类从文件中检索字符串的示例

在这个例子中,我们将使用FileInputStream类从文件中读取数据,然后我们将创建一个 10 字节大小的缓冲区来存储从文件中读取后的临时字节。这些缓冲区将被添加到StringBuilder中,稍后我们将使用toString()方法将该数据转换为字符串。

import java.io.*; 
public class StudyTonight 
{ 
    public static void main(String[] args) throws Exception 
    { 
        File file = new File("E:\\Studytonight\\sampledata.txt"); 
        FileInputStream fileInputStream = new FileInputStream(file);
        byte[] buffer = new byte[10];
        StringBuilder sb = new StringBuilder();
        while (fileInputStream.read(buffer) != -1)
        {
            sb.append(new String(buffer));
            buffer = new byte[10];
        }
        fileInputStream.close();
        String fileData = sb.toString();
        System.out.println(fileData); 
    } 
}

这是存储在文件中的样本数据 Rahul:10025 Sohan:10026 马丹:10027 Jack:10028

使用BufferedReader类类从文件中检索字符串的示例

BufferedReader 是最喜欢用简单的方式读取数据的类。它会同时读取所有数据,然后我们用readLine()方法逐行打印。在文件的末尾readLine()将返回一个空值,在那里我们将停止读取数据。

import java.io.*; 
public class StudyTonight 
{ 
    public static void main(String[] args) throws Exception 
    { 
        File file = new File("E:\\Studytonight\\sampledata.txt"); 
        BufferedReader br = new BufferedReader(new FileReader(file)); 
        String line; 
        while ((line = br.readLine()) != null) 
            System.out.println(line);
    } 
}

这是存储在文件中的样本数据 Rahul:10025 Sohan:10026 马丹:10027 Jack:10028

结论

在本文中,我们看到了许多从文件到字符串读取数据的方法,例如从文件中检索数据的四种方法,我们在其中使用FileReader, Scanner, FileInputStream, UsingBufferedReader.`` 在某些情况下,我们需要注意分隔符,否则它将读取无限次。