How to Get Read Datainputstream to Start at Beginning Again Java
The FileInputStream class of the coffee.io bundle tin can be used to read data (in bytes) from files.
It extends the InputStream abstract class.
Before nosotros learn about FileInputStream, make sure to know nearly Coffee Files.
Create a FileInputStream
In order to create a file input stream, we must import the java.io.FileInputStream package first. One time we import the package, here is how we can create a file input stream in Java.
1. Using the path to file
FileInputStream input = new FileInputStream(stringPath); Here, we have created an input stream that will be linked to the file specified by the path.
2. Using an object of the file
FileInputStream input = new FileInputStream(File fileObject); Here, nosotros have created an input stream that will exist linked to the file specified past fileObject.
Methods of FileInputStream
The FileInputStream class provides implementations for different methods present in the InputStream course.
read() Method
-
read()- reads a single byte from the file -
read(byte[] assortment)- reads the bytes from the file and stores in the specified array -
read(byte[] assortment, int start, int length)- reads the number of bytes equal to length from the file and stores in the specified assortment starting from the position start
Suppose we have a file named input.txt with the following content.
This is a line of text inside the file. Allow'due south try to read this file using FileInputStream.
import coffee.io.FileInputStream; public class Main { public static void chief(String args[]) { try { FileInputStream input = new FileInputStream("input.txt"); System.out.println("Data in the file: "); // Reads the first byte int i = input.read(); while(i != -1) { System.out.impress((char)i); // Reads next byte from the file i = input.read(); } input.close(); } take hold of(Exception e) { e.getStackTrace(); } } } Output
Data in the file: This is a line of text inside the file.
In the higher up instance, we take created a file input stream named input. The input stream is linked with the input.txt file.
FileInputStream input = new FileInputStream("input.txt"); To read information from the file, nosotros have used the read() method inside the while loop.
available() Method
To get the number of available bytes, we tin can utilise the bachelor() method. For case,
import java.io.FileInputStream; public class Main { public static void primary(String args[]) { try { // Suppose, the input.txt file contains the post-obit text // This is a line of text within the file. FileInputStream input = new FileInputStream("input.txt"); // Returns the number of available bytes Arrangement.out.println("Available bytes at the outset: " + input.bachelor()); // Reads three bytes from the file input.read(); input.read(); input.read(); // Returns the number of bachelor bytes System.out.println("Available bytes at the finish: " + input.available()); input.close(); } catch (Exception eastward) { eastward.getStackTrace(); } } } Output
Available bytes at the get-go: 39 Bachelor bytes at the end: 36
In the above example,
- Nosotros beginning employ the
available()method to check the number of bachelor bytes in the file input stream. - Nosotros then have used the
read()method iii times to read 3 bytes from the file input stream. - At present, after reading the bytes we over again take checked the available bytes. This fourth dimension the bachelor bytes decreased by 3.
skip() Method
To discard and skip the specified number of bytes, we can utilize the skip() method. For example,
import java.io.FileInputStream; public class Main { public static void main(String args[]) { endeavour { // Suppose, the input.txt file contains the post-obit text // This is a line of text inside the file. FileInputStream input = new FileInputStream("input.txt"); // Skips the 5 bytes input.skip(5); System.out.println("Input stream after skipping v bytes:"); // Reads the first byte int i = input.read(); while (i != -1) { System.out.print((char) i); // Reads adjacent byte from the file i = input.read(); } // shut() method input.shut(); } take hold of (Exception eastward) { e.getStackTrace(); } } } Output
Input Stream after skipping v bytes: is a line of text inside the file.
In the in a higher place example, we accept used the skip() method to skip 5 bytes of data from the file input stream. Hence, the bytes representing the text "This " is not read from the input stream.
close() Method
To close the file input stream, nosotros can utilise the shut() method. One time the close() method is called, nosotros cannot use the input stream to read information.
In all the above examples, nosotros have used the close() method to close the file input stream.
Other Methods Of FileInputStream
| Methods | Descriptions |
|---|---|
finalize() | ensures that the close() method is called |
getChannel() | returns the object of FileChannel associated with the input stream |
getFD() | returns the file descriptor associated with the input stream |
mark() | mark the position in input stream up to which data has been read |
reset() | returns the command to the point in the input stream where the marking was gear up |
To learn more, visit Java FileInputStream (official Java documentation).
Source: https://www.programiz.com/java-programming/fileinputstream
0 Response to "How to Get Read Datainputstream to Start at Beginning Again Java"
Post a Comment