Skip to content

Commit 54eb4dc

Browse files
committed
feat: merge two input files into one using SequenceInputStream with character transformation
Overview Introduced SCIO2 program to demonstrate the use of SequenceInputStream in Java for concatenating the content of multiple input streams. The program reads from two source files (Source1.txt and Source3.txt) and writes the combined content into a destination file (Destination.txt). While copying, it applies a simple transformation by converting uppercase letters to lowercase. Implementation Details Two input streams are created using FileInputStream, one for each source file. A SequenceInputStream is then created to logically concatenate these two input streams, allowing sequential reading as if they were a single continuous stream. An output stream (FileOutputStream) is opened for writing to Destination.txt. The program reads one byte at a time from the sequence input stream. The loop continues until read() returns -1, which indicates that the end of both input files has been reached. During processing, the program checks each byte. If the byte represents an ASCII value between 65 and 120, it adds 32 to convert uppercase alphabetic characters into lowercase. Otherwise, the byte is written as is. The output stream writes the processed characters directly into the destination file. All streams are explicitly closed at the end of execution to ensure proper release of resources. Key Learning Points Demonstrates the use of SequenceInputStream to merge multiple input streams into one logical sequence. Shows how data can be transformed during the streaming process rather than after reading everything. Highlights byte-level manipulation using ASCII values for uppercase-to-lowercase conversion. Reinforces the importance of resource management by closing input and output streams. Illustrates how sequential file content merging can be achieved with minimal code using Java I/O. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 9a63eb1 commit 54eb4dc

File tree

1 file changed

+23
-38
lines changed

1 file changed

+23
-38
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
11
Upper and Lower Bounds in Java Generics:
22

3-
In Java Generics, bounded type parameters allow us to restrict the types that can be used as arguments for type parameters (T).
3+
In Java Generics, bounded type parameters allow us to restrict the types that can be used as arguments for
4+
type parameters (T).
45

56
Java provides two main types of bounds:
6-
77
1. Upper Bounded (? extends T) – Restricts the type to be a subclass of T.
88
2. Lower Bounded (? super T) – Restricts the type to be a superclass of T.
99

1010

11-
1️⃣ Upper Bounded Wildcards (? extends T)
12-
13-
The ? extends T wildcard ensures that the type argument is either T or a subclass of T. This is useful when reading data but not modifying it.
11+
1️⃣ Upper Bounded Wildcards (? extends T):
12+
The ? extends T wildcard ensures that the type argument is either T or a subclass of T.
13+
This is useful when reading data but not modifying it.
1414

1515
🔹 Use Case: When you want to read data but not modify it.
1616

17-
1817
Example: Sum of Numbers
1918

2019
import java.util.List;
21-
2220
class UpperBoundExample {
2321
static double sumOfNumbers(List<? extends Number> list) {
2422
double sum = 0.0;
@@ -27,7 +25,6 @@ class UpperBoundExample {
2725
}
2826
return sum;
2927
}
30-
3128
public static void main(String[] args) {
3229
List<Integer> intList = List.of(1, 2, 3);
3330
List<Double> doubleList = List.of(1.1, 2.2, 3.3);
@@ -38,23 +35,20 @@ class UpperBoundExample {
3835
}
3936

4037
Output:
41-
4238
Sum of Integers: 6.0
4339
Sum of Doubles: 6.6
4440

45-
✅ Key Points:
46-
41+
Key Points:
4742
- The method sumOfNumbers() works with any subtype of Number (e.g., Integer, Double, Float).
4843
- We can read elements (num.doubleValue()) but cannot add new elements (except null).
4944

50-
5145
2️⃣ Lower Bounded Wildcards (? super T)
52-
53-
The ? super T wildcard ensures that the type argument is either T or a superclass of T. This is useful when adding data but not reading it in a specific type.
46+
The ? super T wildcard ensures that the type argument is either T or a superclass of T.
47+
This is useful when adding data but not reading it in a specific type.
5448

5549
🔹 Use Case: When you want to write data, ensuring elements are of a certain type or its superclass.
5650

57-
Example: Adding Integers to a List
51+
Example: Adding Integers to a List:
5852

5953
import java.util.List;
6054
import java.util.ArrayList;
@@ -66,7 +60,6 @@ class LowerBoundExample {
6660
list.add(30);
6761
// list.add(3.5); // ❌ Compilation Error! Only Integer or its superclasses allowed
6862
}
69-
7063
public static void main(String[] args) {
7164
List<Number> numList = new ArrayList<>();
7265
addNumbers(numList); // Works with Number (superclass of Integer)
@@ -75,24 +68,22 @@ class LowerBoundExample {
7568
}
7669

7770
Output:
78-
7971
[10, 20, 30]
8072

81-
✅ Key Points:
82-
73+
Key Points:
8374
- The method addNumbers() ensures that only Integer or its superclasses (Number, Object) can be used.
8475
- We can add elements of type Integer, but reading requires explicit casting.
8576
- This prevents accidental modifications with incompatible types.
8677

87-
3️⃣ Difference Between Upper and Lower Bounded Wildcards:
78+
Difference Between Upper and Lower Bounded Wildcards:
8879

89-
| Feature | ? extends T (Upper Bound) | ? super T(Lower Bound) |
90-
|------------------------|--------------------------------------------------|-------------------------------------------------------|
91-
| Restriction | T or its subtypes | T or its supertypes |
92-
| Use Case | Reading elements (covariance) | Writing elements (contravariance) |
93-
| Example | List<? extends Number> (allows Integer, Double) | List<? super Integer> (allows Number, Object) |
94-
| Can Add Elements? | ❌ No (except null) | ✅ Yes (but only of type T) |
95-
| Can Read Elements? | ✅ Yes (but as T) | ❌ No (requires casting) |
80+
| Feature | ? extends T (Upper Bound) | ? super T (Lower Bound) |
81+
|-------------------|--------------------------------------------|------------------------------------|
82+
| Restriction | T or any subclass (downward view) | T or any superclass (upward view) |
83+
| Use Case | Safe for reading (covariant) | Safe for writing (contravariant) |
84+
| Example | List<? extends Number> Integer, Double | List<? super Integer> Number, Object |
85+
| Can Add Elements? | ❌ No (except null) | ✅ Yes (T or its subclasses) |
86+
| Can Read Elements?| ✅ Yes (as T) | ❌ No (only as Object) |
9687

9788
4️⃣ Example Using Both Upper and Lower Bounds
9889

@@ -117,25 +108,19 @@ class BoundedExample {
117108
}
118109

119110
Output:
120-
121111
Processing: 1.1
122112
Processing: 2.2
123113
Processing: 3.3
124114
Numbers List: [1, 2, 3]
125115

126-
127116
5️⃣ When to Use Upper and Lower Bounds?
117+
- Use ? extends T (Upper Bound) when you need to read elements but not modify them.
118+
- Use ? super T (Lower Bound) when you need to write elements but don’t care about reading.
128119

129-
- ✅ Use ? extends T (Upper Bound) when you need to read elements but not modify them.
130-
- ✅ Use ? super T (Lower Bound) when you need to write elements but don’t care about reading.
131-
132-
133-
Conclusion
120+
Conclusion:
134121
- Upper Bounded Wildcards (? extends T) – Use when you need to read data.
135122
- Lower Bounded Wildcards (? super T) – Use when you need to write data.
136123
- Avoid using both in the same method unless necessary, as it may lead to ambiguity.
137124

138-
139-
bounded type parameters
140-
141-
<T extends Number> in classes and methods
125+
bounded type parameters:
126+
<T extends Number> in classes and methods

0 commit comments

Comments
 (0)