Java IO: Java中的输入输出与示例
Java通过其 I/O 包带来了各种 Streams,可帮助用户执行所有输入输出操作。这些流支持所有类型的对象、数据类型、字符、文件等,以完全执行 I/O 操作。
在探索各种输入和输出流之前,让我们看看Java必须提供的3 个标准或默认流,它们也是最常用的:
- System.in:这是用于从键盘或任何其他标准输入设备读取字符的标准输入流。
- System.out:这是标准输出流,用于在计算机屏幕等输出设备上生成程序结果。
以下是我们用于输出语句的各种打印函数的列表:
- print(): Java中的此方法用于在控制台上显示文本。此文本作为参数以字符串的形式传递给此方法。此方法在控制台上打印文本,并且光标保持在控制台上的文本末尾。下一次打印就是从这里进行的。
句法:System.out.print(parameter);
例子:
// Java code to illustrate print() import java.io.*; class Demo_print { public static void main(String[] args) { // using print() // all are printed in the // same line System.out.print("GfG! "); System.out.print("GfG! "); System.out.print("GfG! "); } }
输出:
GfG! GfG! GfG!
- println():这个方法在Java中也用于在控制台上显示一个文本。它在控制台上打印文本,光标移动到控制台下一行的开头。下一次打印从下一行开始。
句法:System.out.println(parameter);
例子:
// Java code to illustrate println() import java.io.*; class Demo_print { public static void main(String[] args) { // using println() // all are printed in the // different line System.out.println("GfG! "); System.out.println("GfG! "); System.out.println("GfG! "); } }
输出:
GfG! GfG! GfG!
- printf():这是所有方法中最简单的,因为它类似于 C 中的 printf。注意 System.out.print() 和 System.out.println() 采用单个参数,但 printf() 可能采用多个参数.这用于格式化Java中的输出。
例子:// A Java program to demonstrate working of printf() in Java class JavaFormatter1 { public static void main(String args[]) { int x = 100; System.out.printf( "Printing simple" + " integer: x = %d\n", x); // this will print it upto // 2 decimal places System.out.printf( "Formatted with" + " precision: PI = %.2f\n", Math.PI); float n = 5.2f; // automatically appends zero // to the rightmost part of decimal System.out.printf( "Formatted to " + "specific width: n = %.4f\n", n); n = 2324435.3f; // here number is formatted from // right margin and occupies a // width of 20 characters System.out.printf( "Formatted to " + "right margin: n = %20.4f\n", n); } }
输出:
Printing simple integer: x = 100 Formatted with precision: PI = 3.14 Formatted to specific width: n = 5.2000 Formatted to right margin: n = 2324435.2500
- print(): Java中的此方法用于在控制台上显示文本。此文本作为参数以字符串的形式传递给此方法。此方法在控制台上打印文本,并且光标保持在控制台上的文本末尾。下一次打印就是从这里进行的。
- System.err:这是标准错误流,用于在计算机屏幕或任何标准输出设备上输出程序可能抛出的所有错误数据。
此流还使用上述所有 3 个函数来输出错误数据:
- 打印()
- 打印函数()
- 打印函数()
例子:
// Java code to illustrate standard // input output streams import java.io.*; public class SimpleIO { public static void main(String args[]) throws IOException { // InputStreamReader class to read input InputStreamReader inp = null; // Storing the input in inp inp = new InputStreamReader(System.in); System.out.println("Enter characters, " + " and '0' to quit."); char c; do { c = (char)inp.read(); System.out.println(c); } while (c != '0'); } }
输入:
GeeksforGeeks0
输出:
Enter characters, and '0' to quit. G e e k s f o r G e e k s 0
流类型:
- 根据操作的类型,流可以分为两个主要类别:
- 输入流:这些流用于读取必须作为来自源数组或文件或任何外围设备的输入的数据。例如,FileInputStream、BufferedInputStream、ByteArrayInputStream 等。
- 输出流:这些流用于将数据作为输出写入数组或文件或任何输出外围设备。例如,FileOutputStream、BufferedOutputStream、ByteArrayOutputStream 等。
- 输入流:这些流用于读取必须作为来自源数组或文件或任何外围设备的输入的数据。例如,FileInputStream、BufferedInputStream、ByteArrayInputStream 等。
- 根据文件的类型,流可以分为两个主要类,可以进一步分为其他类,如下图所示,后面有解释。
- ByteStream:用于逐字节(8 位)处理数据。虽然它有很多类,但 FileInputStream 和 FileOutputStream 是最流行的。 FileInputStream 用于从源读取,FileOutputStream 用于写入目标。以下是各种 ByteStream 类的列表:
Stream class Description BufferedInputStream It is used for Buffered Input Stream. DataInputStream It contains method for reading java standard datatypes. FileInputStream This is used to reads from a file InputStream This is an abstract class that describes stream input. PrintStream This contains the most used print() and println() method BufferedOutputStream This is used for Buffered Output Stream. DataOutputStream This contains method for writing java standard data types. FileOutputStream This is used to write to a file. OutputStream This is an abstract class that describe stream output. 例子:
// Java Program illustrating the // Byte Stream to copy // contents of one file to another file. import java.io.*; public class BStream { public static void main( String[] args) throws IOException { FileInputStream sourceStream = null; FileOutputStream targetStream = null; try { sourceStream = new FileInputStream("sorcefile.txt"); targetStream = new FileOutputStream("targetfile.txt"); // Reading source file and writing // content to target file byte by byte int temp; while (( temp = sourceStream.read()) != -1) targetStream.write((byte)temp); } finally { if (sourceStream != null) sourceStream.close(); if (targetStream != null) targetStream.close(); } } }
输出:
Shows contents of file test.txt
- CharacterStream:在Java中,字符是使用 Unicode 约定存储的(有关详细信息,请参阅此内容)。字符流自动允许我们逐个字符字符读取/写入数据。虽然它有很多类,但 FileReader 和 FileWriter 是最受欢迎的。 FileReader 和 FileWriter 是分别用于从源读取和写入目标的字符流。以下是各种 CharacterStream 类的列表:
Stream class Description BufferedReader It is used to handle buffered input stream. FileReader This is an input stream that reads from file. InputStreamReader This input stream is used to translate byte to character. OutputStreamReader This output stream is used to translate character to byte. Reader This is an abstract class that define character stream input. PrintWriter This contains the most used print() and println() method Writer This is an abstract class that define character stream output. BufferedWriter This is used to handle buffered output stream. FileWriter This is used to output stream that writes to file. 例子:
// Java Program illustrating that // we can read a file in a human-readable // format using FileReader // Accessing FileReader, FileWriter, // and IOException import java.io.*; public class GfG { public static void main( String[] args) throws IOException { FileReader sourceStream = null; try { sourceStream = new FileReader("test.txt"); // Reading sourcefile and // writing content to target file // character by character. int temp; while (( temp = sourceStream.read()) != -1) System.out.println((char)temp); } finally { // Closing stream as no longer in use if (sourceStream != null) sourceStream.close(); } } }
请参阅此处了解Java中 Byte 和字符 Stream Class 之间的完整区别。
- ByteStream:用于逐字节(8 位)处理数据。虽然它有很多类,但 FileInputStream 和 FileOutputStream 是最流行的。 FileInputStream 用于从源读取,FileOutputStream 用于写入目标。以下是各种 ByteStream 类的列表: