搜尋
首頁Javajava教程Java 檔案輸入流

Java 檔案輸入流

Aug 30, 2024 pm 03:38 PM
java

Java FileInputStream is a class that is used to read data and the streams of bytes from the file. The FileInputStream class is a built-in class in java that defines the java.io.FileInputStream package. The InputStream is a superclass of the FileInputStream. The FileInputStream class is used to reads the streams of raw bytes (byte by byte) like an image data video, audio, etc., whereas to read streams of characters (character by character), we can use FileReaderbuiltin class.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

The declaration syntax of the FileInputStream class in java:

The following is the declaration forjava.io.FileInputStream class.

public class FileInputStream extends InputStream
{
// Constructors and methods of the FileInputStream class
}

The above is the syntax of the FileInputStream, where it is extended to the InputStream class.

FileInputStreamclass member Functions : The FileInputStream class contains constructors and some functions as a FileInputStream class member function.

Constructors of Java FileInputStream Class

  • FileInputStream(File file ): This constructor creates an instance of FileInputstreamby opening a connection to a specified file to read from this instance.
  • FileInputStream(FileDescriptorfdobj): This constructor creates an instance of FileInputstream by using the file descriptor fdobj, which specifies an existing connection to an actual specified file in the file system to read from this instance.
  • FileInputStream(String fname): This constructor creates an instance of FileInputstream by opening a connection to a specified file fname parameter to read from this instance.

Functions of Java FileInputStream Class

Function and syntax are given below:

1. read()

This function is used to read a byte of data from the input stream.

public int read() throws IOException

2. read(byte[] b )

This function is used to read bytes of data up to b array size from the input stream.

public int read(byte[] b)throws IOException

3. read(byte[] b, int off, intlen)

This function is used to read bytes of data from the input stream up to specified len into destination b array at start offset.

public int read(byte[] b, int offset, intlen) throws IOException

4. available()

This function is used to return the estimated number of bytes that can be read from the input stream.

public int available() throws IOException

5. skip( long n )

This function is used to skip n bytes of data from the input stream.

public long skip(long n) throws IOException

6. getChannel()

This function is used to get the unique FileChannel object of the file input stream.

public FileChannelgetChannel()

7. finalize()

This function is used to ensure that when there is no more reference, the file input stream’s close method is to be called.

protected void finalize() throws IOException

8. getFD() – T

his function is used to get the FileDescriptor object, which specifies the connection to the file system’s actual file.

public final FileDescriptorgetFD() throws IOException

9. close()

This function is used to close the File stream and release the resource file.

public void close() throws IOException

Functions and Examples of File Input Stream Class

Working and examples for the FileInputStream class and its methods in java:

Next, we write the java code to understand the FileInputStream class more clearly with the following example where we create a FileInputStream object by using the FileInputStream class constructor and pass the file name to read a character, as below –

Example #1

Code:

//package p1;
import java.io.FileInputStream;
public class Demo {
public static void main( String[] arg) {
int i;
char c;
try{
// create object of file input stream by opening connection data.txt file
FileInputStream fobj=new FileInputStream("D:\\data.txt");
// data.txt file contain "Hello!, How are you?" data in it
i = fobj.read();
c = (char) i;
System.out.println("The First byte is :" +c);
System.out.println("The Number of remaining bytes are :" +fobj.available());
// skip method to skip 3 bytes
fobj.skip(3);
i = fobj.read();
// converts byte to character
c = (char) i;
System.out.println("The Next byte after 3 byte skip is :" +c);
fobj.close();
}catch(Exception e)
{
System.out.println(e);
}
}
}

Output:

Java 檔案輸入流

Example #2

Next, we write the java code to understand the FileInputStream class more clearly; we create an FileInputStream object to read all characters from the file, as below:

Code:

//package p1;
import java.io.FileInputStream;
public class Demo {
public static void main( String[] arg) {
int i;
char c;
// create object of file input stream by opening connection data.txt file
try {
FileInputStream fobj=new FileInputStream("D:\\data.txt");
// data.txt file contain "Hello!, How are you?" data in it
while((i = fobj.read())!=-1)
{
c = (char) i;
System.out.print(c);
}
// getChannel() method
System.out.println("\nThe unique FileChannel object is : " +fobj.getChannel());
// getFD() method
System.out.println("The FileDescriptor object is : " +fobj.getFD());
fobj.close();
}catch(Exception e)
{
System.out.println(e);
}
}
}

Output:

Java 檔案輸入流

Example #3

Next, we write the java code to understand the FileInputStream class where we read the data into the byte array, and we extend the user define class to FileInputStream class to use the finalize() method, as below –

Code:

//package p1;
import java.io.FileInputStream;
public class Demo extends FileInputStream {
public Demo(String file) throws Exception {
super(file);
}
public static void main( String[] arg) {
byte[] b = new byte[16];
int i;
char c;
// create object of file input stream by opening connection data.txt file
try {
Demo fobj=new Demo("D:\\data.txt");
// data.txt file contain "Hello!, How are you?" data in it
i = fobj.read(b, 1, 15);
System.out.println("The total bytes read are : "+i);
System.out.print("The bytes read are : ");
for(byte t : b) {
c = (char)t;
System.out.print(c);
}
// finalize() method
fobj.finalize();
fobj.close();
}catch(Exception e)
{
System.out.println(e);
}
}
}

Output:

Java 檔案輸入流

以上是Java 檔案輸入流的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
如何將Maven或Gradle用於高級Java項目管理,構建自動化和依賴性解決方案?如何將Maven或Gradle用於高級Java項目管理,構建自動化和依賴性解決方案?Mar 17, 2025 pm 05:46 PM

本文討論了使用Maven和Gradle進行Java項目管理,構建自動化和依賴性解決方案,以比較其方法和優化策略。

如何使用適當的版本控制和依賴項管理創建和使用自定義Java庫(JAR文件)?如何使用適當的版本控制和依賴項管理創建和使用自定義Java庫(JAR文件)?Mar 17, 2025 pm 05:45 PM

本文使用Maven和Gradle之類的工具討論了具有適當的版本控制和依賴關係管理的自定義Java庫(JAR文件)的創建和使用。

如何使用咖啡因或Guava Cache等庫在Java應用程序中實現多層緩存?如何使用咖啡因或Guava Cache等庫在Java應用程序中實現多層緩存?Mar 17, 2025 pm 05:44 PM

本文討論了使用咖啡因和Guava緩存在Java中實施多層緩存以提高應用程序性能。它涵蓋設置,集成和績效優勢,以及配置和驅逐政策管理最佳PRA

如何將JPA(Java持久性API)用於具有高級功能(例如緩存和懶惰加載)的對象相關映射?如何將JPA(Java持久性API)用於具有高級功能(例如緩存和懶惰加載)的對象相關映射?Mar 17, 2025 pm 05:43 PM

本文討論了使用JPA進行對象相關映射,並具有高級功能,例如緩存和懶惰加載。它涵蓋了設置,實體映射和優化性能的最佳實踐,同時突出潛在的陷阱。[159個字符]

Java的類負載機制如何起作用,包括不同的類載荷及其委託模型?Java的類負載機制如何起作用,包括不同的類載荷及其委託模型?Mar 17, 2025 pm 05:35 PM

Java的類上載涉及使用帶有引導,擴展程序和應用程序類負載器的分層系統加載,鏈接和初始化類。父代授權模型確保首先加載核心類別,從而影響自定義類LOA

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
4 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
4 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
4 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
1 個月前By尊渡假赌尊渡假赌尊渡假赌

熱工具

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中