We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4
Byte Streams
FileInputStream:
Use: Read raw bytes from a file.
Example: java Copy code FileInputStream fis = new FileInputStream("file.txt"); int data = fis.read(); while (data != -1) { // Process data data = fis.read(); } fis.close(); FileOutputStream:
Example: java Copy code BufferedInputStream bis = new BufferedInputStream(new FileInputStream("file.txt")); int data = bis.read(); while (data != -1) { // Process data data = bis.read(); } bis.close(); BufferedOutputStream:
Use: Efficiently write binary data with buffering.
Example: java Copy code BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("file.txt")); bos.write(data); bos.close(); Character Streams FileReader:
Use: Read text data from a file.
Example: java Copy code FileReader fr = new FileReader("file.txt"); int data = fr.read(); while (data != -1) { // Process data data = fr.read(); } fr.close(); FileWriter:
Use: Efficiently read text data with buffering, especially line-by-line.
Example: java Copy code BufferedReader br = new BufferedReader(new FileReader("file.txt")); String line; while ((line = br.readLine()) != null) { // Process line } br.close(); BufferedWriter:
Use: Efficiently write text data with buffering.
Example: java Copy code BufferedWriter bw = new BufferedWriter(new FileWriter("file.txt")); bw.write("Hello, World!"); bw.newLine(); bw.write("This is a test."); bw.close(); Conversion Streams InputStreamReader:
Use: Convert bytes to characters with specified character encoding.
Example: java Copy code InputStreamReader isr = new InputStreamReader(new FileInputStream("file.txt"), "UTF-8"); int data = isr.read(); while (data != -1) { // Process data data = isr.read(); } isr.close(); OutputStreamWriter:
Use: Convert characters to bytes with specified character encoding.
Example: java Copy code ObjectInputStream ois = new ObjectInputStream(new FileInputStream("object.dat")); Person person = (Person) ois.readObject(); ois.close(); Data Streams DataInputStream:
Use: Read primitive data types from an input stream.
Example: java Copy code DataInputStream dis = new DataInputStream(new FileInputStream("data.dat")); int number = dis.readInt(); double value = dis.readDouble(); dis.close(); DataOutputStream:
Use: Write primitive data types to an output stream.
Example: java Copy code DataOutputStream dos = new DataOutputStream(new FileOutputStream("data.dat")); dos.writeInt(42); dos.writeDouble(3.14); dos.close(); Random Access RandomAccessFile: Use: Read from and write to a file at random positions. Example: java Copy code RandomAccessFile raf = new RandomAccessFile("file.txt", "rw"); raf.seek(10); // Move to position 10 raf.writeBytes("Hello"); raf.close(); Print Streams PrintWriter: Use: Write formatted text to a file. Example: java Copy code PrintWriter pw = new PrintWriter(new FileWriter("file.txt")); pw.println("Hello, World!"); pw.printf("Value: %.2f\n", 3.14159); pw.close(); Summary Each of these classes serves a specific purpose in Java I/O operations, whether for reading or writing data, handling text or binary data, or managing serialization. Understanding their roles and using them appropriately can greatly enhance the efficiency and functionality of your Java applications when dealing with input and output operations.