← Back to Core Java Course | Chapter 12: File I/O | Lesson 7 of 9

Java में FileInputStream और FileOutputStream

FileInputStream और FileOutputStream raw bytes पढ़ते और लिखते हैं, किसी भी file के basic 1s और 0s, music से pictures तक कुछ भी copy करने जैसा। वे किसी भी तरह की file के साथ काम करते हैं।
Syntax
java
FileInputStream input = new FileInputStream("file_name");
FileOutputStream output = new FileOutputStream("file_name");
output.write(bytes);

Writing Bytes with FileOutputStream

FileOutputStream characters के बजाय एक file में raw bytes लिखता है, इसे images या serialized objects जैसे binary data के लिए सही choice बनाते हुए जहां कोई text encoding apply नहीं होती।

उदाहरण: Writing Bytes with FileOutputStream

java
import java.io.FileOutputStream;
import java.io.IOException;
public class Main {
	public static void main(String[] args) throws IOException {
		FileOutputStream out = new FileOutputStream("data.bin");
		out.write(65); // raw byte, not text
		out.close();
		System.out.println("Written");
	}
}

Reading Bytes with FileInputStream

FileInputStream उसी तरह एक file से raw bytes वापस पढ़ता है, binary round-tripping के लिए FileOutputStream के साथ जोड़ा जाता है — एक String के बजाय एक byte[] buffer में पढ़ते हुए।

उदाहरण: Reading Bytes with FileInputStream

java
// Import java.io.FileOutputStream so it can be used by its short name
import java.io.FileOutputStream;
// Import java.io.FileInputStream so it can be used by its short name
import java.io.FileInputStream;
import java.io.IOException;
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) throws IOException {
		// Create a new FileOutputStream object and store it in out
		FileOutputStream out = new FileOutputStream("data.bin");
		out.write(65);
		out.close();
		// Create a new FileInputStream object and store it in in
		FileInputStream in = new FileInputStream("data.bin");
		// Print a line to the console
		System.out.println(in.read());
		in.close();
	}
}

Copying Binary Files

एक binary file copy करने का मतलब है एक FileInputStream से chunks में bytes पढ़ना और हर chunk सीधे एक FileOutputStream में लिखना, bytes को कभी text के रूप में interpret किए बिना।

उदाहरण: Copying Binary Files

java
// Import java.io.FileOutputStream so it can be used by its short name
import java.io.FileOutputStream;
// Import java.io.FileInputStream so it can be used by its short name
import java.io.FileInputStream;
import java.io.IOException;
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) throws IOException {
		// Create a new FileOutputStream object and store it in out
		FileOutputStream out = new FileOutputStream("source.bin");
		out.write(new byte[]{1, 2, 3});
		out.close();
		// Create a new FileInputStream object and store it in in
		FileInputStream in = new FileInputStream("source.bin");
		FileOutputStream copy = new FileOutputStream("copy.bin");
		// Declare buffer and set it to new byte[1024]
		byte[] buffer = new byte[1024];
		int len;
		// Repeat while (len = in.read(buffer)) != -1 is true
		while ((len = in.read(buffer)) != -1) {
			copy.write(buffer, 0, len);
		}
		in.close();
		copy.close();
		System.out.println("Copied");
	}
}

Auto-Close Try-With-Resources

Try-with-resources byte streams के लिए बिल्कुल वही तरीके से काम करता है जैसे character streams के लिए, copy operation पूरा होने या throw करने पर दोनों input और output streams अपने आप बंद करते हुए।

उदाहरण: Auto-Close Try-With-Resources

java
import java.io.FileOutputStream;
import java.io.IOException;
public class Main {
	public static void main(String[] args) throws IOException {
		try (FileOutputStream out = new FileOutputStream("data.bin")) { // auto-closed
			out.write(65);
		}
		System.out.println("Closed automatically");
	}
}

String to Bytes conversion

String.getBytes() text को raw bytes में convert करता है (एक specified या default charset उपयोग करके) जब आपको एक character-oriented stream के बजाय एक byte-oriented stream से string data लिखनी है।

उदाहरण: String to Bytes conversion

java
// Import java.io.FileOutputStream so it can be used by its short name
import java.io.FileOutputStream;
// Import java.io.IOException so it can be used by its short name
import java.io.IOException;
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) throws IOException {
		// Declare bytes and set it to "Hello".getBytes()
		byte[] bytes = "Hello".getBytes();
		// Try code that might throw an exception
		try (FileOutputStream out = new FileOutputStream("text.bin")) {
			out.write(bytes);
		}
		// Print a line to the console
		System.out.println("Written as bytes");
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. Encoding specify किए बिना text के लिए byte streams उपयोग करना।
  2. बिना buffering के एक समय में एक byte से एक file पढ़ना, जो धीमा है।
  3. Streams बंद करना भूल जाना।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.