How to use IO stream function for data stream processing in Java
In Java programming, IO stream is a very important concept. It is used to process input and One of the basic ways of output. IO streams are used in scenarios such as reading files, network programming, and interacting with external devices. This article will introduce how to use IO stream functions for data stream processing in Java and give specific code examples.
Reading file example:
import java.io.FileInputStream; import java.io.IOException; public class ByteStreamExample { public static void main(String[] args) { try { FileInputStream fis = new FileInputStream("input.txt"); int data; while ((data = fis.read()) != -1) { System.out.print((char) data); } fis.close(); } catch (IOException e) { e.printStackTrace(); } } }
Writing file example:
import java.io.FileOutputStream; import java.io.IOException; public class ByteStreamExample { public static void main(String[] args) { try { FileOutputStream fos = new FileOutputStream("output.txt"); String data = "Hello, World!"; byte[] bytes = data.getBytes(); fos.write(bytes); fos.close(); } catch (IOException e) { e.printStackTrace(); } } }
2.2 Character stream function
Java's character stream class is also divided into input streams and output streams. When processing text files, the commonly used input stream class is FileReader, and the output stream class is FileWriter. Character stream functions handle character encoding issues in text files better than byte streams.
Reading file example:
import java.io.FileReader; import java.io.IOException; public class CharacterStreamExample { public static void main(String[] args) { try { FileReader reader = new FileReader("input.txt"); int data; while ((data = reader.read()) != -1) { System.out.print((char) data); } reader.close(); } catch (IOException e) { e.printStackTrace(); } } }
Writing file example:
import java.io.FileWriter; import java.io.IOException; public class CharacterStreamExample { public static void main(String[] args) { try { FileWriter writer = new FileWriter("output.txt"); String data = "你好,世界!"; writer.write(data); writer.close(); } catch (IOException e) { e.printStackTrace(); } } }
2.3 Buffered stream function
In order to improve IO performance, Java provides the buffered stream class. The buffered stream inherits from the byte stream or character stream and overrides some of its methods to provide buffering functionality. Commonly used buffered stream classes include BufferedInputStream, BufferedOutputStream, BufferedReader and BufferedWriter.
Example of reading a file using a buffered stream:
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class BufferedStreamExample { public static void main(String[] args) { try { BufferedReader reader = new BufferedReader(new FileReader("input.txt")); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } reader.close(); } catch (IOException e) { e.printStackTrace(); } } }
Example of writing a file using a buffered stream:
import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; public class BufferedStreamExample { public static void main(String[] args) { try { BufferedWriter writer = new BufferedWriter(new FileWriter("output.txt")); String data = "Hello, World!"; writer.write(data); writer.close(); } catch (IOException e) { e.printStackTrace(); } } }
The above is the detailed content of How to use IO stream functions for data streaming in Java. For more information, please follow other related articles on the PHP Chinese website!