GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing zetcode.com — Java InputStream - reading data with Java InputStream Java InputStreamlast modified February 21, 2024
In this article we show how to work with
Java stream is a flow of data from a source or into a destination. A
good metaphor for Java streams is water flowing from a tap into a bathtub and
later into a drainage.
Streams support many different types of data, including simple bytes, primitive data types, localized characters, and objects. Subclasses
The close method
The The read method
Read text
The following example shows how to read a text file with thermopylae.txt
The Battle of Thermopylae was fought between an alliance of Greek city-states, led by King Leonidas of Sparta, and the Persian Empire of Xerxes I over the course of three days, during the second Persian invasion of Greece. In the example, we use this text file. Main.java
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
void main() throws IOException {
String fileName = "thermopylae.txt";
try (InputStream fis = new FileInputStream(fileName);
InputStreamReader isr = new InputStreamReader(fis, StandardCharsets.UTF_8);
BufferedReader br = new BufferedReader(isr)) {
br.lines().forEach(line -> System.out.println(line));
}
}
The text file is read with try (InputStream fis = new FileInputStream(fileName);
InputStreamReader isr = new InputStreamReader(fis, StandardCharsets.UTF_8);
BufferedReader br = new BufferedReader(isr)) {
br.lines().forEach(line -> System.out.println(line)); The data is read by lines from a buffered reader. Read bytes
The read methods of Main.java
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
void main() throws IOException {
String fileName = "ball.png";
try (InputStream is = new FileInputStream(fileName)) {
byte[] buffer = new byte[is.available()];
is.read(buffer);
int i = 0;
for (byte b : buffer) {
if (i % 10 == 0) {
System.out.println();
}
System.out.printf("%02x ", b);
i++;
}
}
System.out.println();
}
The example reads bytes from a PNG image and prints the bytes in hexadecimal format to the console. try (InputStream is = new FileInputStream(fileName)) {
We use byte[] buffer = new byte[is.available()]; is.read(buffer);
With the int i = 0;
for (byte b: buffer) {
if (i % 10 == 0) {
System.out.println();
}
System.out.printf("%02x ", b);
i++;
}
We go through the array and print the bytes to the console in hexadecimal format. 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 00 00 00 0a 00 00 00 0a 08 06 00 00 00 8d 32 cf bd 00 00 00 04 73 42 49 54 08 08 08 08 7c 08 64 88 00 00 00 09 70 48 59 73 00 00 0d d7 00 00 0d d7 01 42 28 9b 78 00 00 00 19 74 45 58 74 53 6f ... This is a partial sample output of the example. Read from URL
Main.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URI;
import java.net.URISyntaxException;
void main() throws IOException, URISyntaxException {
String webSite = "https://www.something.com";
URL url = new URI(webSite).toURL();
try (InputStream is = url.openStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is))) {
br.lines().forEach(System.out::println);
}
}
The example opens an try (InputStream is = url.openStream();
An <html><head><title>Something.</title></head> <body>Something.</body> </html> Read deserialized data
Main.java
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
void main() throws IOException {
String fileName = "myfile.dat";
try (OutputStream fis = new FileOutputStream(fileName);
ObjectOutputStream out = new ObjectOutputStream(fis)) {
List<Country> countries = new ArrayList<>();
countries.add(new Country("Slovakia", 5429000));
countries.add(new Country("Norway", 5271000));
countries.add(new Country("Croatia", 4225000));
countries.add(new Country("Russia", 143439000));
out.writeObject(countries);
}
}
record Country(String name, int population) implements Serializable {}
The example serializes a list of objects. out.writeObject(countries);
A list of countries is written to the Main.java
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.util.List;
void main() throws IOException, ClassNotFoundException {
String fileName = "myfile.dat";
try (InputStream fis = new FileInputStream(fileName);
ObjectInputStream oin = new ObjectInputStream(fis)) {
List<Country> countries = (List<Country>) oin.readObject();
countries.forEach(System.out::println);
}
}
record Country(String name, int population) implements Serializable {}
We use the Read sequence of streams
Main.java
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.SequenceInputStream;
void main() throws IOException {
String fileName1 = "myfile.txt";
String fileName2 = "myfile1.txt";
String fileName3 = "myfile2.txt";
try (InputStream is1 = new FileInputStream(fileName1);
InputStream is2 = new FileInputStream(fileName2);
InputStream is3 = new FileInputStream(fileName3);
SequenceInputStream sis1 = new SequenceInputStream(is1, is2);
SequenceInputStream sis = new SequenceInputStream(sis1, is3)) {
int b = sis.read();
while (b != -1) {
System.out.printf("%c", b);
b = sis.read();
}
System.out.println();
}
}
The example reads from three try (InputStream is1 = new FileInputStream(fileName1);
InputStream is2 = new FileInputStream(fileName2);
InputStream is3 = new FileInputStream(fileName3);
SequenceInputStream sis1 = new SequenceInputStream(is1, is2);
SequenceInputStream sis = new SequenceInputStream(sis1, is3)) {
We define three input streams and these streams are placed into int b = sis.read();
while (b != -1) {
System.out.printf("%c", b);
b = sis.read();
}
We read the data from the streams with SourceJava InputStream - language reference
In this article we have presented the Java AuthorMy name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming. List all Java tutorials. |
| 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. |