Home >Java >javaTutorial >Detailed explanation and practice of JAVA underlying IO programming
Detailed explanation and practice of JAVA underlying IO programming
In JAVA programming, IO (Input/Output) is an important concept. It represents the exchange of data with the outside world, including reading data from or writing data to an external device. JAVA provides a wealth of IO classes and interfaces for handling different types of data exchange. This article will introduce JAVA underlying IO programming in detail and provide some specific code examples.
1. Classification of JAVA IO streams
In JAVA, IO streams are divided into two types: byte stream and character stream.
1. Byte stream: Byte stream transmits data in bytes. It is mainly used for processing binary data or non-text data. The byte stream classes in JAVA mainly include InputStream and OutputStream.
2. Character stream: Character stream transmits data in units of characters. It is mainly used for processing text data. The character stream classes in JAVA mainly include Reader and Writer.
2. Use of byte streams
In JAVA, byte streams are mainly operated through the InputStream and OutputStream interfaces.
1. Example of the InputStream class:
import java.io.*; public class InputStreamExample { public static void main(String[] args) { try { InputStream inputStream = new FileInputStream("input.txt"); int data = inputStream.read(); while (data != -1) { System.out.print((char) data); data = inputStream.read(); } inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } }
The above code shows how to use InputStream to read the contents of the file input.txt and print it to the console byte by byte.
2.Example of OutputStream class:
import java.io.*; public class OutputStreamExample { public static void main(String[] args) { try { OutputStream outputStream = new FileOutputStream("output.txt"); String data = "Hello World!"; outputStream.write(data.getBytes()); outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } }
The above code shows how to use OutputStream to write the string "Hello World!" to the file output.txt.
3. Use of character streams
In JAVA, character streams are mainly operated through the Reader and Writer interfaces.
1.Example of Reader class:
import java.io.*; public class ReaderExample { public static void main(String[] args) { try { Reader reader = new FileReader("input.txt"); int data = reader.read(); while (data != -1) { System.out.print((char) data); data = reader.read(); } reader.close(); } catch (IOException e) { e.printStackTrace(); } } }
The above code shows how to use Reader to read the contents of the file input.txt and print it to the console character by character.
2.Writer class example:
import java.io.*; public class WriterExample { public static void main(String[] args) { try { Writer writer = new FileWriter("output.txt"); String data = "Hello World!"; writer.write(data); writer.close(); } catch (IOException e) { e.printStackTrace(); } } }
The above code shows how to use Writer to write the string "Hello World!" to the file output.txt.
4. Summary
This article is based on JAVA underlying IO programming, introduces the use of byte streams and character streams in detail, and provides specific code examples. Readers can learn and master the relevant knowledge of JAVA IO programming in depth based on these sample codes. Mastering IO programming is a basic requirement for JAVA development. It is very important for operations such as file processing and network communication. I hope this article can be helpful to readers.
The above is the detailed content of Detailed explanation and practice of JAVA underlying IO programming. For more information, please follow other related articles on the PHP Chinese website!