Java IO - Input Streams [#3]

preview_player
Показать описание
In this episode, I show you how the InputStream class works, and how to use it's methods so that we can therefore use any input stream. #Java #IO #JavaIO

Code:

More Videos coming soon.
Leave a comment for any future video suggestions.
Рекомендации по теме
Комментарии
Автор

Finally you are the only person who try to really teach and explain details . Guys any 3 minute video about IO is bu... Java is all about DETAILS thank you Kody

irakliulumbelashvili
Автор

The udemy course i bought for java makes it tougher to understand. but u makes it easy for free. thank u so so much:)

harig
Автор

You do really explain well, thank you sir.
Hope you could present more tutorial in other topics!

roychan
Автор

Could you please, tell, why is there a space in there? ( 8:20 )
Truly appreciate your work man!

ericmovsessian
Автор

11:35

I'm getting a file not found exception even after putting it in a try-catch block. The txt file is in the same folder as the Java file.

P. S. : the class file and Java file are in the same folder

brownchocohuman
Автор

# Java IO: Input Stream Cheat Sheet

## Overview

### Key Concepts
- **InputStream**: Abstract class for reading byte-oriented input from various sources.
- **Abstract Methods**: Must be overridden in subclasses.
- **Read Methods**: Used to read bytes from the stream.

## Basic Structure

### Reading a Single Byte
```java
InputStream input = System.in; // Example with system input stream
int data = input.read(); // Reads one byte
if (data != -1) {
// Process data
}
```
- **Returns**: 0 to 255 for a byte, -1 for end of stream.
- **Exception**: `IOException` must be handled.

### Handling Exceptions
```java
try {
int data = input.read();
} catch (IOException e) {
e.printStackTrace();
}
```

## Reading Multiple Bytes

### Using a For Loop
```java
int[] data = new int[10];
for (int i = 0; i < data.length; i++) {
data[i] = input.read();
}
```

### Using a Byte Array
```java
byte[] data = new byte[20];
input.read(data);
```
- **Faster**: More efficient for larger data reads.

## Reading Until End of Stream

### Using a While Loop
```java
int data;
while ((data = input.read()) != -1) {
System.out.write(data);
}
System.out.flush();
```

## Advanced Techniques

### Reading Chunks of Data
```java
byte[] buffer = new byte[1024];
int bytesRead = input.read(buffer, 0, buffer.length);
```
- **Parameters**:
- `buffer`: The array into which data is read.
- `offset`: Start offset in array.
- `length`: Maximum number of bytes to read.

### Reading from a File
```java
try (InputStream fileInput = new FileInputStream("file.txt")) {
byte[] data = new byte[fileInput.available()];
fileInput.read(data);
System.out.write(data);
System.out.flush();
} catch (IOException e) {
e.printStackTrace();
}
```

### Checking Available Bytes
```java
int availableBytes = input.available();
```

## Closing Streams
```java
try (InputStream input = new FileInputStream("file.txt")) {
// Use the input stream
} catch (IOException e) {
e.printStackTrace();
}
```
- **Automatic Resource Management**: Use try-with-resources to automatically close streams.

## Important Methods

### InputStream Methods
- **read()**: Reads the next byte of data from the input stream.
- **read(byte[] b)**: Reads some number of bytes from the input stream and stores them into the buffer array `b`.
- **read(byte[] b, int off, int len)**: Reads up to `len` bytes of data from the input stream into an array of bytes.
- **available()**: Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking.
- **close()**: Closes this input stream and releases any system resources associated with the stream.

## Example Usage

### Simple Console Input Example
```java
import java.io.IOException;
import java.io.InputStream;

public class SimpleInputExample {
public static void main(String[] args) {
InputStream input = System.in;
try {
int data = input.read();
System.out.write(data);
System.out.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```

### File Reading Example
```java
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class FileInputExample {
public static void main(String[] args) {
try (InputStream input = new {
byte[] data = new byte[input.available()];
input.read(data);
System.out.write(data);
System.out.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```

## Conclusion
This cheat sheet covers the essential aspects of using `InputStream` in Java, including reading single and multiple bytes, handling end of stream, closing streams, and reading from files. By mastering these concepts, you'll be well-equipped to handle byte-oriented input in your Java applications.

vitezhrabri
Автор

You said data got placed into a buffer by System.in.write(), but what actually IS a buffer?

goldenraisins
Автор

So you read it and put it on buffer.
Write it and put it on stream.
Then flush it to console.
Correct me if I'm wrong.

leogura
Автор

Don't you think it's better to use try-with-resources instead of try-catch-finally? The try-with-resources is completely error free unlike the other ways.

dedathestriker
join shbcf.ru