📜  Java FileInputStream read() 方法及示例

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

Java FileInputStream read() 方法及示例

Java中的 FileInputStream 类可用于以字节序列的形式从文件中读取数据。 FileInputStream 用于读取原始字节流,例如图像数据。要读取字符流,请考虑使用 FileReader。

InputStream 类的 read() 方法从输入流中读取一个字节的数据。返回下一个数据字节,如果到达文件末尾则返回 -1,如果发生 I/O 错误则抛出异常。请参阅程序。

句法:

public abstract int read()

返回值:此方法返回数据的下一个字节,如果到达流的末尾,则返回 -1。

异常: IOException - 如果发生 I/O 错误。

如何调用read() 方法?

按照以下步骤使用 FileInputStream 从文件中读取数据,这是 FileInputClass 的最后通牒

第 1 步:将文件附加到 FileInputStream,因为这将使我们能够从文件中读取数据,如下所示:

FileInputStream  fileInputStream =new FileInputStream(“file.txt”);

第 2 步:现在,要从文件中读取数据,我们应该从 FileInputStream 中读取数据,如下所示:

ch=fileInputStream.read();

步骤 3(a):当没有更多数据可供进一步读取时,read() 方法返回 -1;

步骤 3(b):然后,我们应该将监视器附加到输出流。为了显示数据,我们可以使用 System.out.print。

System.out.print(ch);

执行:

原始文件内容:(“file.txt”)

GeeksforGeeks is a computer science portal
Java
// Java program to demonstrate the working
// of the FileInputStream read() method
 
import java.io.File;
import java.io.FileInputStream;
 
public class abc {
 
    public static void main(String[] args) {       
         
            // Creating file object and specifying path
            File file = new File("file.txt");
 
            try {
                FileInputStream input= new FileInputStream(file);
                int character;
                // read character by character by default
                // read() function return int between 0 and 255.
 
                while ((character = input.read()) != -1) {
                    System.out.print((char)character);
                }
            }
            catch (Exception e) {
                e.printStackTrace();
            }
    }
}


Java
// Java program to read a file
// without using -1 in while loop
 
import java.io.File;
import java.io.FileInputStream;
 
public class abc {
 
    public static void main(String[] args) {
         
            // Creating file object and specifying path
            File file = new File("file.txt");
 
            try {
                FileInputStream input= new FileInputStream(file);
                int character;
               
                // read character by character by default
                // read() function return int between 0 and 255.
                while (input.available()!=0) {
                    character = input.read();
                    System.out.print((char)character);   
                }
               
                input.close();   
            }
       
            catch (Exception e) {
                 
                e.printStackTrace();
            }
    }
}


输出

GeeksforGeeks is a computer science portal

在while循环中不使用-1读取文件

我们将在此使用 available() 方法的概念。 available() 方法用于返回还有多少字节要读取。我们将使用 read() 方法打印字符,直到剩下 0 个字符需要读取。

示例:原始文件内容:(“file.txt”)

GeeksforGeeks

Java

// Java program to read a file
// without using -1 in while loop
 
import java.io.File;
import java.io.FileInputStream;
 
public class abc {
 
    public static void main(String[] args) {
         
            // Creating file object and specifying path
            File file = new File("file.txt");
 
            try {
                FileInputStream input= new FileInputStream(file);
                int character;
               
                // read character by character by default
                // read() function return int between 0 and 255.
                while (input.available()!=0) {
                    character = input.read();
                    System.out.print((char)character);   
                }
               
                input.close();   
            }
       
            catch (Exception e) {
                 
                e.printStackTrace();
            }
    }
}

输出

GeeksforGeeks