Skip to content

Commit 89fee36

Browse files
committed
feat: implement file copy with uppercase-to-lowercase transformation using streams
Overview Added SCIO1 program demonstrating Java file I/O using FileInputStream and FileOutputStream. The program copies content from one file to another while converting uppercase letters to lowercase during the transfer. Implementation Details Input stream (FileInputStream): Reads bytes sequentially from the source file (Source1.txt). File must exist before execution. Output stream (FileOutputStream): Writes processed bytes into the destination file (Source3.txt). If the file exists, it will be overwritten. Processing logic: - Data is read one byte at a time (fis.read()), returning -1 at EOF. - Each byte is checked against the ASCII range 65–90 (uppercase A–Z). - If the condition matches, 32 is added to the byte to convert it into the corresponding lowercase character. - Otherwise, the original byte is written unchanged. Resource management: Both input and output streams are explicitly closed to release system resources and avoid memory leaks. Key Learning Points Demonstrates low-level byte stream operations in Java. Shows how to transform data while streaming from one file to another. Reinforces understanding of ASCII-based character manipulation. Highlights the importance of explicitly closing I/O streams. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 814cc72 commit 89fee36

File tree

3 files changed

+42
-8
lines changed

3 files changed

+42
-8
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.
1+
COPYING THIS FILE INTO ANOTHER FILE. and converting the lower case to an upper case letter.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
copying this file into another file.
1+
copying this file into another file. and converting the lower case to an upper case letter.

Section23JavaIOStreams/src/SCIO1.java

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,50 @@
33

44
public class SCIO1 {
55
public static void main(String[] args) throws Exception {
6-
FileInputStream fis = new FileInputStream("C://Users//somes//Downloads//JAVA SE//Section23JavaIOStreams//src//MyJAVA//Source1.txt"); //Must Be created file.
7-
FileOutputStream fos = new FileOutputStream("C://Users//somes//Downloads//JAVA SE//Section23JavaIOStreams//src//MyJAVA//Source3.txt");
6+
FileInputStream fis = new FileInputStream("/Users/somesh/Java SE/JavaEvolution-Learning-Growing-Mastering/Section23JavaIOStreams/src/MyJAVA/Source1.txt");
7+
//Must Be created file.
8+
9+
FileOutputStream fos = new FileOutputStream("/Users/somesh/Java SE/JavaEvolution-Learning-Growing-Mastering/Section23JavaIOStreams/src/MyJAVA/Source3.txt");
810

911
int b;
10-
while((b= fis.read())!=-1)
12+
while((b = fis.read())!=-1)
1113
{
12-
if(b>=65 && b<=120)fos.write(b+32);
13-
else fos.write(b);
14+
if (b >= 65 && b <= 90) // 'A' to 'Z'
15+
fos.write(b + 32); // Convert uppercase to lowercase
16+
else
17+
fos.write(b);
1418
}
1519
fis.close();
1620
fos.close();
1721
}
18-
}
22+
}
23+
24+
/*
25+
1. FileInputStream (fis):
26+
- Source file (`Source1.txt`) se byte-by-byte data read karta hai.
27+
- Har ek character ko ASCII value ke form me deta hai.
28+
29+
2. FileOutputStream (fos):
30+
- Target file (`Source3.txt`) me data write karta hai.
31+
- Jo bhi byte likhte ho, wahi directly file me save hota hai.
32+
33+
3. Logic (Transformation Part):
34+
- `if (b >= 65 && b <= 90)` → Ye ASCII range uppercase letters (A–Z) ko represent karti hai.
35+
- Agar character uppercase hai toh `b + 32` karke lowercase me convert hota hai.
36+
- Example: 'A' (65) + 32 = 'a' (97), 'B' → 'b', ... 'Z' → 'z'.
37+
- Agar uppercase nahi hai, toh byte as-it-is copy hota hai.
38+
39+
4. Close Streams:
40+
- `fis.close(); fos.close();` → Input/output streams properly close karna zaroori hai.
41+
- Ye ensure karta hai ki resources release ho jayein aur file corruption na ho.
42+
43+
5. Output Behavior:
44+
- Source file se uppercase letters convert hoke lowercase ban jaayenge.
45+
- Baaki characters (digits, spaces, symbols, lowercase) unchanged rahenge.
46+
47+
✔ FileInputStream → file se data read karta hai (byte-by-byte).
48+
✔ FileOutputStream → file me data write karta hai.
49+
✔ Logic → uppercase letters ko lowercase me convert karta hai.
50+
✔ Program ek file copy + case transformation ka simple example hai.
51+
✔ Useful for preprocessing text files (e.g., normalize data to lowercase).
52+
*/

0 commit comments

Comments
 (0)