Java language input and output stream operation skills
The Java language provides developers with many convenient input and output stream classes, making file reading and writing operations very simple and efficient. In this article, we will introduce the basic knowledge of Java language input and output streams and some common operation techniques.
Java input and output streams: Overview
Java input and output streams are a mechanism that uses some predefined classes to read and write data streams or data blocks. They are classes under the Java.io package that can read and write various data types, such as text, binary, pictures, etc. Java input stream is used to read the input stream and Java output stream is used to write the output stream. In addition, Java input and output streams can also be used to read and write network sockets, files, and other input and output devices.
Classification of Java input and output streams
Java input and output streams can be divided into four main types:
Java input and output stream operation skills
The following introduces some commonly used Java input and output stream operation skills:
FileReader fr = new FileReader("file.txt"); BufferedReader br = new BufferedReader(fr); String line; while ((line = br.readLine()) != null) { System.out.println(line); } br.close(); fr.close();
DataOutputStream out = new DataOutputStream( new BufferedOutputStream( new FileOutputStream("binary.dat"))); out.writeInt(123); out.writeDouble(3.14159); out.writeChar('A'); out.close(); DataInputStream in = new DataInputStream( new BufferedInputStream( new FileInputStream("binary.dat"))); System.out.println(in.readInt()); System.out.println(in.readDouble()); System.out.println(in.readChar()); in.close();
DataInputStream in = new DataInputStream( new BufferedInputStream( new FileInputStream("binary.dat"))); try { while (true) { int i = in.readInt(); double d = in.readDouble(); char c = in.readChar(); System.out.println(i + " " + d + " " + c); } } catch (EOFException e) { System.out.println("文件读取完毕"); } in.close();
Conclusion
Java input and output streams are a very important part of Java programming. This article introduces the basic knowledge and common operation techniques of Java input and output streams. Through in-depth study and use of Java input and output streams, the reading and writing efficiency and performance of Java programs can be greatly improved.
The above is the detailed content of Java language input and output stream operation skills. For more information, please follow other related articles on the PHP Chinese website!