Input / output Streams in java
Input / output Streams in hindi
हर एक प्रोग्रामिंग भाषा में इनपुट और आउटपुट स्ट्रीम होता है।
इनपुट / आउटपुट के लिए java.io package को import किया जाता है।
इनपुट / आउटपुट के लिए java.io package को import किया जाता है।
जावा इनपुट / आउटपुट का उपयोग इनपुट स्ट्रीम को Processed करने और output installation का उत्पादन करने के लिए किया जाता है।
हम इनपुट / आउटपुट API द्वारा जावा में file handling कर सकते है |
इनपुट / आउटपुट ऑपरेशन को पूरी तरह से execute करने के लिए streams objects data type character files को सभी प्रकारों का समर्थन करते हैं।
जावा फ़ाइलें और नेटवर्क से संबंधित इनपुट / आउटपुट के लिए मजबूत लेकिन visible समर्थन प्रदान करता है |
Three streams created for I/o.
system.out
यह एक आउटपुट स्ट्रीम डिस्प्ले करने के लिए उपयोग किया जाता है..(standard output stream).
यह एक आउटपुट स्ट्रीम डिस्प्ले करने के लिए उपयोग किया जाता है..(standard output stream).
system.in
यह एक इनपुट स्ट्रीम display करने के लिए उपयोग किया जाता है..(standard input stream)
system.err
यह एक error स्ट्रीम डिस्प्ले करने के लिए उपयोग किया जाता है..(standard error stream).
example - system.out and System.errpublic class Demo
{
public static void main(string[] args)
{
system.out.println("Hello java");
system.out.println("Error");
}
}
output:- Hello java
Stream in java
- stream एक Bytes का sequence होता है |
- Input stream
- Output stream
Input Stream
एक java.io package के लिए superclass है |इनपुट स्ट्रीम के बहुत सारे sub-classes होते हैं जिसका उपयोग आउटपुट के लिए किया जाता है।
- abstract in read()
- int available
- void close()
- int read(byte[]b)
- int read (byte[]b, int off, int length)
java application किसी source से data read के लिए इनपुट स्ट्रीम का उपयोग करते है। यह एक file और Peripheral device या socket हो सकता है।
Byte stream character stream
byte stream इसकी bytes के इनपुट / आउटपुट से निपटने के लिए एक सुविधाजनक साधन प्रदान करता है | character stream यह इनपुट / आउटपुट से निपटने के लिए convenient meaning provide करता है।
public abstract int read() throws IOException input stream से data की अगली byte जो files के अंत में वापस आती है।public int available ()throw IOExceptionscurrent input stream से पढ़ी जा सकने वाली bytes की संख्या के अनुमान के अनुसार वापस करता है । public void close() throws IOExceptions यह current input stream को close करना है।
output stream
the आउटपुट स्ट्रीम ये एक java.io package के लिए superclass है|
output stream बहुत सारे subclass होते हैं जिसका उपयोग आउटपुट के लिए किया जाता है।
- void close(0
- void write(byte[]b)
- void flush()
- void write[(byte)b, int off, int len]
आउटपुट स्ट्रीम एक abstract class है। इसका का उपयोग किसी destination पर डाटा लिखने के लिए किया जाता है।
आउटपुट स्ट्रीम के लिए एक उपयोगी मेथड्स
public void write(int) throw IOExceptions इसका उपयोग current आउटपुट स्ट्रीम में byte लिखने के लिए किया जाता है। public void write(byte) throw IOExceptions इसका use current output stream में byte में एक array लिखने के लिए किया जाता है।
public void Flush(byte) throw IOExceptions
public void close throw IOExceptions
इसका उपयोग वर्तमान आउटपुट स्ट्रीम को बंद करने के लिए किया जाता है।
Byte Streams
जावा बाइट स्ट्रीम का उपयोग 8-बिट बाइट्स के इनपुट और आउटपुट को करने के लिए किया जाता है। हालांकि बाइट स्ट्रीम से संबंधित कई क्लास हैं लेकिन सबसे अधिक उपयोग की जाने वाली क्लास FileInputStream और FileOutputStream हैं ।
एक उदाहरण से समज़ते है जो इनपुट फ़ाइल को आउटपुट फ़ाइल में कॉपी करने के लिए इन दो क्लास का उपयोग करता है
import java.io.*;
public class ReadFile
{
public static void main(String args[]) throws IOException {
FileInputStream in = null;
FileOutputStream out = null;
try
{
in = new FileInputStream("input.txt");
out = new FileOutputStream("output.txt");
int r;
while ((r = in.read()) != -1)
{
out.write(r);
}
}
finally
{
if (in != null)
{
in.close();
}
if (out != null)
{
out.close();
}
}
}
}
Character Streams
जावा बाइट स्ट्रीम का उपयोग 8-बिट बाइट्स के इनपुट और आउटपुट को करने के लिए किया जाता है जबकि जावा कैरेक्टर स्ट्रीम का उपयोग 16-बिट यूनिकोड इनपुट और आउटपुट करने के लिए किया जाता है।हालांकि क्लास स्ट्रीम से संबंधित कई क्लास हैं लेकिन सबसे ज्यादा उपयोग की जाने वाली क्लास हैं। FileReader और FileWriter।
हालांकि आंतरिक रूप से FileReader FileInputStream का उपयोग करता है और FileWriter FileOutputStream का उपयोग करता है लेकिन यहाँ प्रमुख अंतर यह है कि FileReader एक समय में दो बाइट्स पढ़ता है और FileWriter एक बार में दो बाइट्स लिखता है।
उदाहरण से समजते हैं जो इनपुट फाइल को एक आउटपुट फाइल में कॉपी करने के लिए इन दो क्लास का उपयोग करता है।
import java.io.*;
public class ReadFile
{
public static void main(String args[]) throws IOException
{
FileReader in = null;
FileWriter out = null;
try
{
in = new FileReader("input.txt");
out = new FileWriter("output.txt");
int r;
while ((r = in.read()) != -1)
{
out.write(r);
}
}
finally
{
if (in != null)
{
in.close();
}
if (out != null)
{
out.close();
}
}
}
}