Inhaltsverzeichnis
Constructors of Java FileInputStream Class
Functions of Java FileInputStream Class
1. read()
2. read(byte[] b )
3. read(byte[] b, int off, intlen)
4. available()
5. skip( long n )
6. getChannel()
7. finalize()
8. getFD() – T
9. close()
Functions and Examples of File Input Stream Class
Example #3
Heim Java javaLernprogramm Java FileInputStream

Java FileInputStream

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
}
Nach dem Login kopieren

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
Nach dem Login kopieren

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
Nach dem Login kopieren

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
Nach dem Login kopieren

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
Nach dem Login kopieren

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
Nach dem Login kopieren

6. getChannel()

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

public FileChannelgetChannel()
Nach dem Login kopieren

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
Nach dem Login kopieren

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
Nach dem Login kopieren

9. close()

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

public void close() throws IOException
Nach dem Login kopieren

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);
}
}
}
Nach dem Login kopieren

Output:

Java FileInputStream

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);
}
}
}
Nach dem Login kopieren

Output:

Java FileInputStream

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);
}
}
}
Nach dem Login kopieren

Output:

Java FileInputStream

Das obige ist der detaillierte Inhalt vonJava FileInputStream. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn

Heiße KI -Werkzeuge

Undresser.AI Undress

Undresser.AI Undress

KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover

AI Clothes Remover

Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool

Undress AI Tool

Ausziehbilder kostenlos

Clothoff.io

Clothoff.io

KI-Kleiderentferner

Video Face Swap

Video Face Swap

Tauschen Sie Gesichter in jedem Video mühelos mit unserem völlig kostenlosen KI-Gesichtstausch-Tool aus!

Heiße Werkzeuge

Notepad++7.3.1

Notepad++7.3.1

Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version

SublimeText3 chinesische Version

Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1

Senden Sie Studio 13.0.1

Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6

Dreamweaver CS6

Visuelle Webentwicklungstools

SublimeText3 Mac-Version

SublimeText3 Mac-Version

Codebearbeitungssoftware auf Gottesniveau (SublimeText3)

Fragen zum Java Spring-Interview Fragen zum Java Spring-Interview Aug 30, 2024 pm 04:29 PM

In diesem Artikel haben wir die am häufigsten gestellten Fragen zu Java Spring-Interviews mit ihren detaillierten Antworten zusammengestellt. Damit Sie das Interview knacken können.

Brechen oder aus Java 8 Stream foreach zurückkehren? Brechen oder aus Java 8 Stream foreach zurückkehren? Feb 07, 2025 pm 12:09 PM

Java 8 führt die Stream -API ein und bietet eine leistungsstarke und ausdrucksstarke Möglichkeit, Datensammlungen zu verarbeiten. Eine häufige Frage bei der Verwendung von Stream lautet jedoch: Wie kann man von einem Foreach -Betrieb brechen oder zurückkehren? Herkömmliche Schleifen ermöglichen eine frühzeitige Unterbrechung oder Rückkehr, aber die Stream's foreach -Methode unterstützt diese Methode nicht direkt. In diesem Artikel werden die Gründe erläutert und alternative Methoden zur Implementierung vorzeitiger Beendigung in Strahlverarbeitungssystemen erforscht. Weitere Lektüre: Java Stream API -Verbesserungen Stream foreach verstehen Die Foreach -Methode ist ein Terminalbetrieb, der einen Vorgang für jedes Element im Stream ausführt. Seine Designabsicht ist

PHP: Eine Schlüsselsprache für die Webentwicklung PHP: Eine Schlüsselsprache für die Webentwicklung Apr 13, 2025 am 12:08 AM

PHP ist eine Skriptsprache, die auf der Serverseite weit verbreitet ist und insbesondere für die Webentwicklung geeignet ist. 1.PHP kann HTML einbetten, HTTP -Anforderungen und Antworten verarbeiten und eine Vielzahl von Datenbanken unterstützt. 2.PHP wird verwendet, um dynamische Webinhalte, Prozessformdaten, Zugriffsdatenbanken usw. mit starker Community -Unterstützung und Open -Source -Ressourcen zu generieren. 3. PHP ist eine interpretierte Sprache, und der Ausführungsprozess umfasst lexikalische Analyse, grammatikalische Analyse, Zusammenstellung und Ausführung. 4.PHP kann mit MySQL für erweiterte Anwendungen wie Benutzerregistrierungssysteme kombiniert werden. 5. Beim Debuggen von PHP können Sie Funktionen wie error_reporting () und var_dump () verwenden. 6. Optimieren Sie den PHP-Code, um Caching-Mechanismen zu verwenden, Datenbankabfragen zu optimieren und integrierte Funktionen zu verwenden. 7

Zeitstempel für Datum in Java Zeitstempel für Datum in Java Aug 30, 2024 pm 04:28 PM

Anleitung zum TimeStamp to Date in Java. Hier diskutieren wir auch die Einführung und wie man Zeitstempel in Java in ein Datum konvertiert, zusammen mit Beispielen.

PHP vs. Python: Verständnis der Unterschiede PHP vs. Python: Verständnis der Unterschiede Apr 11, 2025 am 12:15 AM

PHP und Python haben jeweils ihre eigenen Vorteile, und die Wahl sollte auf Projektanforderungen beruhen. 1.PHP eignet sich für die Webentwicklung mit einfacher Syntax und hoher Ausführungseffizienz. 2. Python eignet sich für Datenwissenschaft und maschinelles Lernen mit präziser Syntax und reichhaltigen Bibliotheken.

Java -Programm, um das Kapselvolumen zu finden Java -Programm, um das Kapselvolumen zu finden Feb 07, 2025 am 11:37 AM

Kapseln sind dreidimensionale geometrische Figuren, die aus einem Zylinder und einer Hemisphäre an beiden Enden bestehen. Das Volumen der Kapsel kann berechnet werden, indem das Volumen des Zylinders und das Volumen der Hemisphäre an beiden Enden hinzugefügt werden. In diesem Tutorial wird erörtert, wie das Volumen einer bestimmten Kapsel in Java mit verschiedenen Methoden berechnet wird. Kapselvolumenformel Die Formel für das Kapselvolumen lautet wie folgt: Kapselvolumen = zylindrisches Volumenvolumen Zwei Hemisphäre Volumen In, R: Der Radius der Hemisphäre. H: Die Höhe des Zylinders (ohne die Hemisphäre). Beispiel 1 eingeben Radius = 5 Einheiten Höhe = 10 Einheiten Ausgabe Volumen = 1570,8 Kubikeinheiten erklären Berechnen Sie das Volumen mithilfe der Formel: Volumen = π × R2 × H (4

PHP vs. Python: Kernmerkmale und Funktionen PHP vs. Python: Kernmerkmale und Funktionen Apr 13, 2025 am 12:16 AM

PHP und Python haben jeweils ihre eigenen Vorteile und eignen sich für verschiedene Szenarien. 1.PHP ist für die Webentwicklung geeignet und bietet integrierte Webserver und reichhaltige Funktionsbibliotheken. 2. Python eignet sich für Datenwissenschaft und maschinelles Lernen mit prägnanter Syntax und einer leistungsstarken Standardbibliothek. Bei der Auswahl sollte anhand der Projektanforderungen festgelegt werden.

Php gegen andere Sprachen: Ein Vergleich Php gegen andere Sprachen: Ein Vergleich Apr 13, 2025 am 12:19 AM

PHP eignet sich für die Webentwicklung, insbesondere für die schnelle Entwicklung und Verarbeitung dynamischer Inhalte, ist jedoch nicht gut in Anwendungen auf Datenwissenschaft und Unternehmensebene. Im Vergleich zu Python hat PHP mehr Vorteile in der Webentwicklung, ist aber nicht so gut wie Python im Bereich der Datenwissenschaft. Im Vergleich zu Java wird PHP in Anwendungen auf Unternehmensebene schlechter, ist jedoch flexibler in der Webentwicklung. Im Vergleich zu JavaScript ist PHP in der Back-End-Entwicklung präziser, ist jedoch in der Front-End-Entwicklung nicht so gut wie JavaScript.

See all articles