Posts

๐Ÿ“š Other Stream Classes in Java เคœाเคตा เคฎें เค…เคจ्เคฏ เคธ्เคŸ्เคฐीเคฎ เค•्เคฒाเคธेเคธ

In Java, apart from basic byte and character streams, there are several specialized stream classes that provide advanced input-output operations. เคœाเคตा เคฎें, เคฌेเคธिเค• เคฌाเค‡เคŸ เค”เคฐ เค•ैเคฐेเค•्เคŸเคฐ เคธ्เคŸ्เคฐीเคฎ्เคธ เค•े เค…เคฒाเคตा, เค•เคˆ เคตिเคถेเคท เคธ्เคŸ्เคฐीเคฎ เค•्เคฒाเคธेเคธ เคนोเคคी เคนैं เคœो เคเคกเคตांเคธ เค‡เคจเคชुเคŸ-เค†เค‰เคŸเคชुเคŸ เค‘เคชเคฐेเคถเคจ เคช्เคฐเคฆाเคจ เค•เคฐเคคी เคนैं। Key Points / เคฎुเค–्เคฏ เคฌिंเคฆु:- These classes extend InputStream, OutputStream, Reader, Writer . เคฏे เค•्เคฒाเคธेเคธ InputStream, OutputStream, Reader, Writer เค•ो เคเค•्เคธเคŸेंเคก เค•เคฐเคคी เคนैं। Provide special operations like file merging, compression, object serialization. เคฏे เคตिเคถेเคท เค‘เคชเคฐेเคถเคจ เคœैเคธे เคซाเค‡เคฒ เคฎเคฐ्เคœिंเค—, เค•เคฎ्เคช्เคฐेเคถเคจ, เค‘เคฌ्เคœेเค•्เคŸ เคธीเคฐिเคฏเคฒाเค‡เคœेเคถเคจ เค•เคฐเคคी เคนैं। Often used in real-time applications where basic streams are not enough. เค‡เคจเค•ा เคช्เคฐเคฏोเค— เคฐीเคฏเคฒ-เคŸाเค‡เคฎ เคเคช्เคฒिเค•ेเคถเคจ เคฎें เคนोเคคा เคนै เคœเคนाँ เคฌेเคธिเค• เคธ्เคŸ्เคฐीเคฎ เคชเคฐ्เคฏाเคช्เคค เคจเคนीं เคนोเคคी। ๐Ÿ”น Common Other Stream Classes / เคธाเคฎाเคจ्เคฏ เค…เคจ्เคฏ เคธ्เคŸ्เคฐीเคฎ เค•्เคฒाเคธेเคธ Stream Class Use / เค‰เคชเคฏोเค— SequenceInputStream Combine multiple input streams PushbackInputStream         Push data...

Interactive Input and Output in Java เคœाเคตा เคฎें เค‡ंเคŸเคฐैเค•्เคŸिเคต เค‡เคจเคชुเคŸ เค”เคฐ เค†เค‰เคŸเคชुเคŸ

Interactive Input and Output in Java means taking input from the user at runtime and providing dynamic output based on the input. เคœाเคตा เคฎें เค‡ंเคŸเคฐैเค•्เคŸिเคต เค‡เคจเคชुเคŸ เค”เคฐ เค†เค‰เคŸเคชुเคŸ เค•ा เค…เคฐ्เคฅ เคนै เคฐเคจเคŸाเค‡เคฎ เคชเคฐ เค‰เคชเคฏोเค—เค•เคฐ्เคคा เคธे เค‡เคจเคชुเคŸ เคฒेเคจा เค”เคฐ เค‰เคธเค•े เค†เคงाเคฐ เคชเคฐ เค†เค‰เคŸเคชुเคŸ เคฆेเคจा। Key Points / เคฎुเค–्เคฏ เคฌिंเคฆु:- Interactive Input : User enters data using console or GUI. เค‡ंเคŸเคฐैเค•्เคŸिเคต เค‡เคจเคชुเคŸ : เค‰เคชเคฏोเค—เค•เคฐ्เคคा เค•ंเคธोเคฒ เคฏा GUI เคธे เคกेเคŸा เคฆेเคคा เคนै। Interactive Output : Program responds according to input. เค‡ंเคŸเคฐैเค•्เคŸिเคต เค†เค‰เคŸเคชुเคŸ : เคช्เคฐोเค—्เคฐाเคฎ เค‡เคจเคชुเคŸ เค•े เค…เคจुเคธाเคฐ เค†เค‰เคŸเคชुเคŸ เคฆेเคคा เคนै। Common Classes : Scanner , BufferedReader , Console . เคธाเคฎाเคจ्เคฏ เค•्เคฒाเคธेเคธ : Scanner , BufferedReader , Console । ๐Ÿ’ป Example 1: Using Scanner for Interactive I/O import java.util.Scanner; public class InteractiveScanner { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter your name: "); String name = sc.nextLine(); System.out.print("Enter your age: "); int a...

๐Ÿ“„ Random Access Files in Java เคœाเคตा เคฎें เคฐैंเคกเคฎ เคเค•्เคธेเคธ เคซाเค‡เคฒ्เคธ

In Java, Random Access Files allow us to read and write data from any position in a file without reading it sequentially. เคœाเคตा เคฎें เคฐैंเคกเคฎ เคเค•्เคธेเคธ เคซाเค‡เคฒ्เคธ เคนเคฎें เคซाเค‡เคฒ เค•े เค•िเคธी เคญी เคธ्เคฅाเคจ เคธे เคกेเคŸा เคชเคข़เคจे เค”เคฐ เคฒिเค–เคจे เค•ी เค…เคจुเคฎเคคि เคฆेเคคी เคนैं, เคฌिเคจा เค‰เคธे เค•्เคฐเคฎเคตाเคฐ เคชเคข़े। Key Features / เคฎुเค–्เคฏ เคตिเคถेเคทเคคाเคँ:- Direct Access: We can move to any file position using a file pointer. เคกाเคฏเคฐेเค•्เคŸ เคเค•्เคธेเคธ: เคนเคฎ เคซाเค‡เคฒ เคชॉเค‡ंเคŸเคฐ เค•ा เค‰เคชเคฏोเค— เค•เคฐเค•े เคซाเค‡เคฒ เคฎें เค•िเคธी เคญी เคธ्เคฅाเคจ เคชเคฐ เคœा เคธเค•เคคे เคนैं। Read and Write Both: Same file can be used for reading and writing. เคฐीเคก เค”เคฐ เคฐाเค‡เคŸ เคฆोเคจों: เคเค• เคนी เคซाเค‡เคฒ เคธे เคชเคข़ เค”เคฐ เค‰เคธเคฎें เคฒिเค– เคธเค•เคคे เคนैं। Class Used: RandomAccessFile (from java.io package). เค‰เคชเคฏोเค— เค•ी เค—เคˆ เค•्เคฒाเคธ: RandomAccessFile ( java.io เคชैเค•ेเคœ เคธे)। ๐Ÿ’ป Example 1: Writing to Random Access File import java.io.*; public class RandomAccessWrite { public static void main(String[] args) { try { RandomAccessFile raf = new RandomAccessFile("random.txt", "rw"); raf.writeUTF("...

Concatenating and Buffering Files in Java เคœाเคตा เคฎें เคซ़ाเค‡เคฒों เค•ो เคœोเคก़เคจा เค”เคฐ เคฌเคซเคฐ เค•เคฐเคจा

In Java, we can merge multiple files into a single file (concatenation) and use buffering to improve file I/O performance. เคœाเคตा เคฎें เคนเคฎ เค•เคˆ เคซाเค‡เคฒों เค•ो เคเค• เคนी เคซाเค‡เคฒ เคฎें เคœोเคก़ เคธเค•เคคे เคนैं (เค•ंเค•ैเคŸिเคจेเคถเคจ) เค”เคฐ เคฌเคซเคฐिंเค— เค•ा เค‰เคชเคฏोเค— เค•เคฐเค•े เคซाเค‡เคฒ I/O เค•ी เค—เคคि เคฌเคข़ा เคธเค•เคคे เคนैं। Key Concepts / เคฎुเค–्เคฏ เคฌिंเคฆु:- Concatenation: Combining multiple files into one. เค•ंเค•ैเคŸिเคจेเคถเคจ: เค•เคˆ เคซाเค‡เคฒों เค•ो เคเค• เคซाเค‡เคฒ เคฎें เคœोเคก़เคจा। Buffering: Using a buffer to reduce disk access and improve speed. เคฌเคซเคฐिंเค—: เคฌเคซเคฐ เค•ा เค‰เคชเคฏोเค— เค•เคฐเค•े เคกिเคธ्เค• เคเค•्เคธेเคธ เค•เคฎ เค•เคฐเคจा เค”เคฐ เค—เคคि เคฌเคข़ाเคจा। Streams Used: SequenceInputStream , BufferedInputStream , BufferedOutputStream . เค‰เคชเคฏोเค— เค•ी เค—เคˆ เคธ्เคŸ्เคฐीเคฎ्เคธ: SequenceInputStream , BufferedInputStream , BufferedOutputStream । ๐Ÿ’ป Example 1: Concatenating Two Files import java.io.*; public class ConcatFiles { public static void main(String[] args) { try { FileInputStream fis1 = new FileInputStream("file1.txt"); FileInputStream fis2 = new FileInputStream("file2.txt"); ...

Handling Primitive Data Types in Java I/O เคœाเคตा I/O เคฎें เคช्เคฐिเคฎिเคŸिเคต เคกेเคŸा เคŸाเค‡เคช्เคธ เค•ो เคธंเคญाเคฒเคจा

Java provides special Data Streams to read and write primitive data types like int , float , boolean , and char . เคœाเคตा เคกाเคŸा เคธ्เคŸ्เคฐीเคฎ्เคธ เคช्เคฐเคฆाเคจ เค•เคฐเคคा เคนै เคœो เคช्เคฐिเคฎिเคŸिเคต เคกेเคŸा เคŸाเค‡เคช्เคธ เคœैเคธे int , float , boolean , เค”เคฐ char เค•ो เคชเคข़เคจे เค”เคฐ เคฒिเค–เคจे เค•े เคฒिเค เค‰เคชเคฏोเค— เคนोเคคी เคนैं। Key Points / เคฎुเค–्เคฏ เคฌिंเคฆु:- DataOutputStream writes primitive data types to a file. DataOutputStream เคซाเค‡เคฒ เคฎें เคช्เคฐिเคฎिเคŸिเคต เคกेเคŸा เคŸाเค‡เคช्เคธ เคฒिเค–เคคा เคนै। DataInputStream reads primitive data types from a file. DataInputStream เคซाเค‡เคฒ เคธे เคช्เคฐिเคฎिเคŸिเคต เคกेเคŸा เคŸाเค‡เคช्เคธ เคชเคข़เคคा เคนै। These are useful for structured data storage. เคฏเคน เคธ्เคŸ्เคฐเค•्เคšเคฐ्เคก เคกेเคŸा เคธ्เคŸोเคฐेเคœ เค•े เคฒिเค เค‰เคชเคฏोเค—ी เคนैं। ๐Ÿ’ป Example 1: Writing Primitive Data Types import java.io.*; public class WritePrimitives { public static void main(String[] args) { try { FileOutputStream fos = new FileOutputStream("data.bin"); DataOutputStream dos = new DataOutputStream(fos); dos.writeInt(101); dos.writeUTF("Ajay"); ...

Reading and Writing Bytes in Java / เคœाเคตा เคฎें เคฌाเค‡เคŸ्เคธ เคชเคข़เคจा เค”เคฐ เคฒिเค–เคจा

Java provides Byte Streams to handle 8-bit data (bytes) for reading and writing files. เคœाเคตा เคฌाเค‡เคŸ เคธ्เคŸ्เคฐीเคฎ्เคธ เคช्เคฐเคฆाเคจ เค•เคฐเคคा เคนै, เคœो 8-เคฌिเคŸ เคกेเคŸा (เคฌाเค‡เคŸ्เคธ) เค•ो เคซाเค‡เคฒ เคธे เคชเคข़เคจे เค”เคฐ เคฒिเค–เคจे เค•े เคฒिเค เคช्เคฐเคฏोเค— เคนोเคคी เคนैं। Key Points / เคฎुเค–्เคฏ เคฌिंเคฆु:- Use FileInputStream for reading bytes from a file. เคซाเค‡เคฒ เคธे เคฌाเค‡เคŸ्เคธ เคชเคข़เคจे เค•े เคฒिเค FileInputStream เค•ा เค‰เคชเคฏोเค— เค•เคฐें। Use FileOutputStream for writing bytes to a file. เคซाเค‡เคฒ เคฎें เคฌाเค‡เคŸ्เคธ เคฒिเค–เคจे เค•े เคฒिเค FileOutputStream เค•ा เค‰เคชเคฏोเค— เค•เคฐें। Suitable for binary files like images, PDFs, and videos. เคฏเคน เคฌाเค‡เคจเคฐी เคซाเค‡เคฒ्เคธ เคœैเคธे เค‡เคฎेเคœ, PDF เค”เคฐ เคตीเคกिเคฏो เค•े เคฒिเค เค‰เคชเคฏुเค•्เคค เคนै। ๐Ÿ’ป Example 1: Writing Bytes to a File import java.io.FileOutputStream; import java.io.IOException; public class WriteBytesExample { public static void main(String[] args) { try { FileOutputStream fos = new FileOutputStream("bytefile.txt"); String data = "Hello Byte Stream!"; fos.write(data.getBytes()); fos.close(); Sy...

Reading and Writing Characters in Java เคœाเคตा เคฎें เค•เคฐैเค•्เคŸเคฐ เคชเคข़เคจा เค”เคฐ เคฒिเค–เคจा

Reading and writing characters in Java is done using Character Streams, which handle 16-bit Unicode characters. เคœाเคตा เคฎें เค•เคฐैเค•्เคŸเคฐ เค•ो เคชเคข़เคจे เค”เคฐ เคฒिเค–เคจे เค•े เคฒिเค เค•ैเคฐेเค•्เคŸเคฐ เคธ्เคŸ्เคฐीเคฎ्เคธ เค•ा เค‰เคชเคฏोเค— เค•िเคฏा เคœाเคคा เคนै, เคœो 16-เคฌिเคŸ เคฏूเคจिเค•ोเคก เค•เคฐैเค•्เคŸเคฐ เค•ो เคธंเคญाเคฒเคคे เคนैं। Key Points / เคฎुเค–्เคฏ เคฌिंเคฆु:- Use FileReader for reading characters from a file. เคซाเค‡เคฒ เคธे เค•เคฐैเค•्เคŸเคฐ เคชเคข़เคจे เค•े เคฒिเค FileReader เค•ा เค‰เคชเคฏोเค— เค•เคฐें। Use FileWriter for writing characters to a file. เคซाเค‡เคฒ เคฎें เค•เคฐैเค•्เคŸเคฐ เคฒिเค–เคจे เค•े เคฒिเค FileWriter เค•ा เค‰เคชเคฏोเค— เค•เคฐें। Wrap them in BufferedReader and BufferedWriter for efficiency. เคฆเค•्เคทเคคा เคฌเคข़ाเคจे เค•े เคฒिเค BufferedReader เค”เคฐ BufferedWriter เค•ा เค‰เคชเคฏोเค— เค•เคฐें। ๐Ÿ’ป Example 1: Writing Characters to a File import java.io.FileWriter; import java.io.IOException; public class WriteCharactersExample { public static void main(String[] args) { try { FileWriter writer = new FileWriter("charfile.txt"); writer.write("Hello, Character Stream!"); writer.close(); ...