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(); ...

Creation of Files in Java เคœाเคตा เคฎें เคซाเค‡เคฒ เคจिเคฐ्เคฎाเคฃ

File creation is an essential part of file handling in Java. We use the `File` class from the java.io package to create new files. เคœाเคตा เคฎें เคซाเค‡เคฒ เคนैंเคกเคฒिंเค— เค•ा เคเค• เคฎเคนเคค्เคตเคชूเคฐ्เคฃ เคนिเคธ्เคธा เคซाเค‡เคฒ เคจिเคฐ्เคฎाเคฃ เคนै। เคนเคฎ เคจเคˆ เคซाเค‡เคฒ เคฌเคจाเคจे เค•े เคฒिเค java.io เคชैเค•ेเคœ เค•ी `File` เค•्เคฒाเคธ เค•ा เค‰เคชเคฏोเค— เค•เคฐเคคे เคนैं। Key Points / เคฎुเค–्เคฏ เคฌिंเคฆु:- 1. Use File class to create new files.    เคจเคˆ เคซाเค‡เคฒ เคฌเคจाเคจे เค•े เคฒिเค File เค•्เคฒाเคธ เค•ा เค‰เคชเคฏोเค— เค•เคฐें। 2. `createNewFile()` method returns true if the file is created successfully.    `createNewFile()` เคฎेเคฅเคก true เคฒौเคŸाเคคा เคนै เคฏเคฆि เคซाเค‡เคฒ เคธเคซเคฒเคคाเคชूเคฐ्เคตเค• เคฌเคจเคคी เคนै। 3. If the file already exists, the method returns false.    เคฏเคฆि เคซाเค‡เคฒ เคชเคนเคฒे เคธे เคฎौเคœूเคฆ เคนै, เคคो เคฎेเคฅเคก false เคฒौเคŸाเคคा เคนै। ๐Ÿ’ป Example 1: Create a File import java.io.File; import java.io.IOException; public class CreateFileExample {     public static void main(String[] args) {         try {             File file = new File("myfile.txt");             if (fil...

Input/Output Exceptions in Java เคœाเคตा เคฎें เค‡เคจเคชुเคŸ/เค†เค‰เคŸเคชुเคŸ เคเค•्เคธेเคช्เคถเคจ्เคธ

Input / Output (I/O) exceptions occur when a program fails to read or write data from a file, device, or network. These exceptions are part of the java.io package and usually occur due to: เค‡เคจเคชुเคŸ / เค†เค‰เคŸเคชुเคŸ (I/O) เคเค•्เคธेเคช्เคถเคจ เคคเคฌ เคนोเคคी เคนै เคœเคฌ เคช्เคฐोเค—्เคฐाเคฎ เคซाเค‡เคฒ, เคกिเคตाเค‡เคธ เคฏा เคจेเคŸเคตเคฐ्เค• เคธे เคกेเคŸा เคชเคข़เคจे เคฏा เคฒिเค–เคจे เคฎें เค…เคธเคซเคฒ เคฐเคนเคคा เคนै। เคฏे เคเค•्เคธेเคช्เคถเคจ  java.io  เคชैเค•ेเคœ เค•ा เคนिเคธ्เคธा เคนोเคคी เคนैं เค”เคฐ เคฎुเค–्เคฏ เคฐूเคช เคธे เค‡เคจ เค•ाเคฐเคฃों เคธे เคนोเคคी เคนैं: File not found เคซाเค‡เคฒ เค•ा เคจ เคฎिเคฒเคจा Lack of read/write permissions เคชเคข़เคจे/เคฒिเค–เคจे เค•ी เค…เคจुเคฎเคคि เคจ เคนोเคจा Disk or device failure เคกिเคธ्เค• เคฏा เคกिเคตाเค‡เคธ เคซेเคฒ เคนोเคจा Network disconnection during I/O operations เคจेเคŸเคตเคฐ्เค• เค•เคจेเค•्เคถเคจ เคŸूเคŸ เคœाเคจा In Java some common I/O exceptions are as follows เคœाเคตा เคฎें เค•ुเค› เคธाเคฎाเคจ्เคฏ I/O เคเค•्เคธेเคช्เคถเคจ เคจिเคฎ्เคจ เคนै:- IOException – General exception for I/O errors เคธाเคฎाเคจ्เคฏ I/O เคเคฐเคฐ FileNotFoundException – File is missing or inaccessible เคซाเค‡เคฒ เค—ाเคฏเคฌ เคฏा เคเค•्เคธेเคธ เคจ เคนो เคฐเคนी เคนो EOFException – End of file reached unexpectedly เคซाเค‡เคฒ เค…เคšाเคจเค• เคธเคฎाเคช्เคค เคนो เค—เคˆ We ca...

Java File Class เคœाเคตा เคซाเค‡เคฒ เค•्เคฒाเคธ

The File class in Java is part of the java.io package and is used to represent the path of files and directories. The File class does not read or write data directly; it only provides information about files and directories and methods to create, delete, or check properties. เคœाเคตा เคฎें File เค•्เคฒाเคธ java.io เคชैเค•ेเคœ เค•ा เคนिเคธ्เคธा เคนै เค”เคฐ เค‡เคธเค•ा เคช्เคฐเคฏोเค— เคซाเค‡เคฒ เคเคตं เคกाเคฏเคฐेเค•्เคŸเคฐी เค•े เคชाเคฅ เค•ो เคช्เคฐเคฆเคฐ्เคถिเคค เค•เคฐเคจे เค•े เคฒिเค เค•िเคฏा เคœाเคคा เคนै। File เค•्เคฒाเคธ เคกेเคŸा เค•ो เคธीเคงे เคชเคข़เคคी เคฏा เคฒिเค–เคคी เคจเคนीं เคนै; เคฏเคน เค•ेเคตเคฒ เคซाเค‡เคฒ เค”เคฐ เคกाเคฏเคฐेเค•्เคŸเคฐी เค•ी เคœाเคจเค•ाเคฐी เค”เคฐ เค‰เคจ्เคนें เคฌเคจाเคจे, เคนเคŸाเคจे เคฏा เคšेเค• เค•เคฐเคจे เค•ी เคธुเคตिเคงा เคฆेเคคी เคนै। ๐Ÿ”น Constructors of File Class (เค•เคจ्เคธ्เคŸ्เคฐเค•्เคŸเคฐ्เคธ) File(String pathname) – Creates a new File instance using a given pathname. File(String pathname) – เคฆिเคฏे เค—เคฏे เคชाเคฅเคจेเคฎ เค•े เค†เคงाเคฐ เคชเคฐ เคจเคˆ File เค‘เคฌ्เคœेเค•्เคŸ เคฌเคจाเคคा เคนै। File(String parent, String child) – Creates a File object from directory and filename separately. File(String parent, String child) – เคกाเคฏเคฐेเค•्เคŸเคฐी เค”เคฐ เคซाเค‡เคฒ เค•ा เคจाเคฎ เค…เคฒเค—-เค…เคฒเค— เคฆेเค•เคฐ File เค‘เคฌ्เคœेเค•्เคŸ เคฌเคจाเคคा เคนै। ๐Ÿ”น Common Methods of Fil...