Java io fileinputstream
Java io fileinputstream
FileInputStream is meant for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader .
Constructor Summary
Constructor and Description |
---|
FileInputStream(File file) |
Method Summary
Modifier and Type | Method and Description |
---|---|
int | available() |
Methods inherited from class java.io.InputStream
Methods inherited from class java.lang.Object
Constructor Detail
FileInputStream
First, if there is a security manager, its checkRead method is called with the name argument as its argument.
If the named file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading then a FileNotFoundException is thrown.
FileInputStream
First, if there is a security manager, its checkRead method is called with the path represented by the file argument as its argument.
If the named file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading then a FileNotFoundException is thrown.
FileInputStream
If there is a security manager, its checkRead method is called with the file descriptor fdObj as its argument to see if it’s ok to read the file descriptor. If read access is denied to the file descriptor a SecurityException is thrown.
If fdObj is null then a NullPointerException is thrown.
This constructor does not throw an exception if fdObj is invalid . However, if the methods are invoked on the resulting stream to attempt I/O on the stream, an IOException is thrown.
Method Detail
The skip method may, for a variety of reasons, end up skipping over some smaller number of bytes, possibly 0 . If n is negative, an IOException is thrown, even though the skip method of the InputStream superclass does nothing in this case. The actual number of bytes skipped is returned.
This method may skip more bytes than are remaining in the backing file. This produces no exception and the number of bytes skipped may include some number of bytes that were beyond the EOF of the backing file. Attempting to read from the stream after skipping past the end will result in -1 indicating the end of the file.
available
In some cases, a non-blocking read (or skip) may appear to be blocked when it is merely slow, for example when reading large files over slow networks.
close
If this stream has an associated channel then the channel is closed as well.
getFD
getChannel
The initial position of the returned channel will be equal to the number of bytes read from the file so far. Reading bytes from this stream will increment the channel’s position. Changing the channel’s position, either explicitly or by reading, will change this stream’s file position.
finalize
- Overview
- Package
- >Java™ Platform
Standard Ed. 7
- Summary:
- Nested |
- Field |
- Constr |
- Method
- Detail:
- Field |
- Constr |
- Method
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2018, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.
Потоки ввода, InputStream
Существуют две параллельные иерархии классов ввода : InputStream и Reader. Класс Reader введен в последних версиях Java. В данной статье рассматривается вопрос использования потока байтового ввода InputStream, иерархия которого представлена на следующем рисунке.
Поток Stream— это абстрактное понятие источника или приёмника данных, которые способны обрабатывать информацию. Есть два типа потоков: байтовые и символьные. В некоторых ситуациях символьные потоки более эффективны, чем байтовые. Классы, производные от базовых InputStream или Reader, имеют методы read() для чтения отдельных байтов или массива байтов.
Входной поток InputStream
Базовый класс InputStream — это абстрактный класс, определяющий входной поток данных, и является родителем для классов, получающих данные из различных источников : массив байтов, строки (String), файлы, каналы pipe, у которых одна из сторон является входом, а вторая сторона играет роль выхода, и т.д. Методы класса InputStream при возникновении ошибки вызывают исключение IOException.
Методы класса InputStream :
Метод | Описание |
---|---|
boolean readBoolean() | байт булевого однобайтового значения |
byte readByte() | байт одного байта |
char readChar() | байт значения char |
double readDouble() | байт восьмибайтового значения double |
float readFloat() | чтение четырехбайтового значения float |
int readInt() | чтение целочисленного значения int |
long readLong() | чтение значения long |
short readShort() | чтение значения short |
String readUTF() | чтение строки в кодировке UTF-8 |
int skipBytes(int n) | пропуск при чтении n байтов |
Пример чтения из бинарного файла с использованием DataInputStream
ObjectInputStream
Класс ObjectInputStream отвечает за чтение ранее сериализованных данных из потока. В конструкторе он принимает ссылку на поток ввода:
Основные методы класса ObjectInputStream :