GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||
GeezerVison is showing www.tutorialspoint.com — Java - InputStream Class
Selected Reading
Java - InputStream ClassIntroductionThe Java InputStream class is the superclass of all classes representing an input stream of bytes. Applications that need to define a subclass of InputStream must always provide a method that returns the next byte of input. Class declarationFollowing is the declaration for Java.io.InputStream class − public abstract class InputStream
extends Object
implements Closeable
Class constructors
Class methods
Methods inheritedThis class inherits methods from the following classes −
Example - Using close() with FileInputStream (Automatic Closure with Try-With-Resources)The following example shows the usage of Java InputStream close() method. InputStreamDemo.javapackage com.tutorialspoint;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
public class InputStreamDemo {
public static void main(String[] args) {
try (InputStream inputStream = new FileInputStream("example.txt")) {
int data;
while ((data = inputStream.read()) != -1) { // Read character by character
System.out.print((char) data);
}
// No need to manually close, try-with-resources handles it
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output(if example.txt contains "Hello")Let us compile and run the above program, this will produce the following result− Hello Explanation
Example - Using mark(int readLimit) with BufferedInputStreamThe following example shows the usage of Java InputStream mark(int readLimit) method. InputStreamDemo.javapackage com.tutorialspoint;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
public class InputStreamDemo {
public static void main(String[] args) {
try (InputStream inputStream = new BufferedInputStream(new FileInputStream("example.txt"))) {
System.out.println("Mark supported? " + inputStream.markSupported());
// Read and print first character
System.out.print((char) inputStream.read());
// Mark the current position
inputStream.mark(5); // Can read up to 5 bytes before mark expires
// Read next two characters
System.out.print((char) inputStream.read());
System.out.print((char) inputStream.read());
// Reset back to the marked position
inputStream.reset();
// Read again from the marked position
System.out.print((char) inputStream.read());
System.out.print((char) inputStream.read());
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output(if example.txt contains "Hello")Let us compile and run the above program, this will produce the following result− Mark supported? true Helo elo Explanation
Example - Checking markSupported() for BufferedInputStreamThe following example shows the usage of Java InputStream markSupported() method. InputStreamDemo.javapackage com.tutorialspoint;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
public class InputStreamDemo {
public static void main(String[] args) {
try (InputStream inputStream = new BufferedInputStream(new FileInputStream("example.txt"))) {
System.out.println("Mark supported? " + inputStream.markSupported());
inputStream.mark(5); // Mark the position
System.out.print((char) inputStream.read()); // Read a character
System.out.print((char) inputStream.read()); // Read another character
inputStream.reset(); // Reset back to the mark
System.out.print((char) inputStream.read()); // Read again
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output(if example.txt contains "Java")Let us compile and run the above program, this will produce the following result− Mark supported? true Jav Explanation
Advertisements
| ||||||||||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |