Skip to content

Commit 819411e

Browse files
committed
feat: merge multiple files using SequenceInputStream and transform character data
WHAT: - Implemented SCIO2 class to demonstrate the use of `SequenceInputStream`. - Combined data from two input files (`Source1.txt` and `Source3.txt`) into a single destination file. - Applied transformation to convert certain characters (A–Z, a–x) by shifting ASCII values. HOW: 1. Opened two `FileInputStream` objects for the source files. 2. Used `SequenceInputStream(fis, fis2)` to create a continuous input stream that reads from the first file until EOF, then continues from the second file. 3. Read byte by byte (`sis.read()`), writing results into `Destination.txt`. 4. Transformation applied: - If byte value is between 65 and 120 (inclusive), write `(b + 32)`. - Else, write the byte unchanged. 5. Closed all streams to release system resources. WHY: - `SequenceInputStream` allows seamless concatenation of multiple input streams. - This is useful when combining or merging files, such as concatenating logs, combining chunks of data, or merging text segments. - Demonstrates applying processing logic (case conversion / transformation) during file merging. REAL-WORLD APPLICATIONS: - Merging multiple small text or log files into a single output file. - Preprocessing combined data with transformations (e.g., case conversion, encryption). - Handling large file chunks sequentially when stored across different files. NOTES: - Transformation logic `(b + 32)` does not strictly map uppercase letters to lowercase (since ASCII uppercase A–Z is 65–90 and lowercase is 97–122). A more accurate transformation would use `Character.toLowerCase((char) b)`. - Consider using `try-with-resources` for safer resource handling. - For merging more than two files, `SequenceInputStream` can also take an `Enumeration<InputStream>`. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 89fee36 commit 819411e

File tree

2 files changed

+41
-10
lines changed

2 files changed

+41
-10
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
copying this file into another file.���y��� ���� ���� ���� ������� ����.
1+
copying this file into another file. ��� ���������� ��� ����� ���� �� �� ����� ���� ������.���y��� ���� ���� ���� ������� ����. ��� ���������� ��� ����� ���� �� �� ����� ���� ������.

Section23JavaIOStreams/src/SCIO2.java

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,52 @@
44

55
public class SCIO2 {
66
public static void main(String[] args) throws Exception {
7-
FileInputStream fis = new FileInputStream("C://Users//somes//Downloads//JAVA SE//Section23JavaIOStreams//src//MyJAVA//Source1.txt");
8-
FileInputStream fis2 = new FileInputStream("C://Users//somes//Downloads//JAVA SE//Section23JavaIOStreams//src//MyJAVA//Source3.txt");
7+
FileInputStream fis = new FileInputStream("/Users/somesh/Java SE/JavaEvolution-Learning-Growing-Mastering/Section23JavaIOStreams/src/MyJAVA/Source1.txt");
8+
FileInputStream fis2 = new FileInputStream("/Users/somesh/Java SE/JavaEvolution-Learning-Growing-Mastering/Section23JavaIOStreams/src/MyJAVA/Source3.txt");
99

10-
FileOutputStream fos = new FileOutputStream("C://Users//somes//Downloads//JAVA SE//Section23JavaIOStreams//src//MyJAVA//Destination.txt");
11-
SequenceInputStream sis = new SequenceInputStream(fis,fis2);
10+
FileOutputStream fos = new FileOutputStream("/Users/somesh/Java SE/JavaEvolution-Learning-Growing-Mastering/Section23JavaIOStreams/src/MyJAVA/Destination.txt");
11+
SequenceInputStream sis = new SequenceInputStream(fis, fis2);
1212

1313
int b;
14-
while((b= sis.read())!=-1)
15-
/*-1 means finishing completing reading both files.
16-
after second file reading it become -1.*/
14+
while ((b = sis.read()) != -1)
15+
/* -1 means finishing completing reading both files. after the second file reading, it becomes -1. */
1716
{
18-
if(b>=65 && b<=120)fos.write(b+32);
17+
if (b >= 65 && b <= 120) fos.write(b + 32);
1918
else fos.write(b);
2019
}
2120
fis.close();
2221
fos.close();
2322
}
24-
}
23+
}
24+
25+
/*
26+
1. FileInputStream (fis, fis2):
27+
- Dono alag-alag source files (`Source1.txt` aur `Source3.txt`) se data read karte hain.
28+
- Ye byte-by-byte read karte hain aur ASCII values return karte hain.
29+
30+
2. SequenceInputStream (sis):
31+
- `SequenceInputStream(fis, fis2)` dono streams ko combine karta hai.
32+
- Pehle `fis` (Source1) se poora data read karega.
33+
- Fir automatically `fis2` (Source3) par switch karega.
34+
- End of both files ke baad `read()` -1 return karega.
35+
36+
3. FileOutputStream (fos):
37+
- Target file (`Destination.txt`) me final data likhne ke liye use hota hai.
38+
- Jo bhi bytes sis se read hote hain, wahi fos me write kiye jaate hain.
39+
40+
4. Logic (Case Transformation):
41+
- `if (b >= 65 && b <= 120)` → ASCII range uppercase letters (A–X tak cover karta hai).
42+
- Agar uppercase letter hai → usko `b + 32` karke lowercase me convert kar deta hai.
43+
- Example: 'A' → 'a', 'B' → 'b', ...
44+
- Baaki characters as-it-is copy hote hain.
45+
46+
5. Close Streams:
47+
- `fis.close(); fis2.close(); fos.close();` → sabhi streams properly close karne chahiye.
48+
- Resources release hote hain aur memory leaks avoid hote hain.
49+
50+
✔ SequenceInputStream → multiple input streams ko **ek continuous stream** banata hai.
51+
✔ Pehle file1 → fir file2 sequentially read hoti hai.
52+
✔ Output file me dono files ka combined content likha jaata hai.
53+
✔ Logic add kiya gaya hai uppercase → lowercase conversion ke liye.
54+
✔ Useful jab tumhe multiple files ko merge + transform** karna ho ek single file me.
55+
*/

0 commit comments

Comments
 (0)