The Java.io package contains almost all the classes needed to operate input and output. All these stream classes represent input sources and output destinations.
Streams in the Java.io package support many formats, such as basic types, objects, localized character sets, etc.
A stream can be understood as a sequence of data. An input stream represents reading data from a source, and an output stream represents writing data to a destination.
Java provides powerful and flexible support for I/O, making it more widely used in file transfer and network programming.
Console input for Java is done by System.in.
To obtain a character stream bound to the console, you can wrap System.in in a BufferedReader object to create a character stream.
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Once the BufferedReader object is created, we can use the read() method to read a character from the console, or the readLine() method to read a string.
The following shows how to read multi-character input. This code will continue to read the character input of the console until the user enters q:
char c;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter characters, press 'q' key to exit.");
do {
c = (char) br.read();
System.out.println(c);
}
while (c != 'q');
If you want to read a string, use readLine()
As mentioned before, console output is completed by print() and println(). These methods are defined by the class PrintStream, and System.out is a reference to the object of this class.
PrintStream inherits the OutputStream class and implements the write() method. In this way, write() can also be used to write operations to the console. However, this way of writing is not commonly used.
public static void main(String args[])
{
int b; b = 'A';
System.out.write(b);
System.out.write('\n');
}
According to the previous chapter, a stream is defined as a sequence of data. The input stream is used to read data from the source, and the output stream is used to write data to the target.
The two important streams in this chapter are FileInputStream and FileOutputStream.
This stream is used to read data from a file, and its objects can be created using the keyword new.
There are various constructor methods used to create objects.
You can use a string file name to create an input stream object to read the file:
InputStream f = new FileInputStream("C:/java/hello");
You can also use a file object to create an input stream object to read the file. We first have to use the File() method to create a file object:
File f = new File("C:/java/hello");
InputStream out = new FileInputStream(f);
This class is used to create a file and write data to the file.
If the target file does not exist before the stream opens the file for output, the stream creates the file.
There are two constructors that can be used to create FileOutputStream objects.
Create an output stream object using a string file name:
OutputStream f = new FileOutputStream("C:/java/hello")
You can also use a file object to create an output stream to write to the file. We first have to use the File() method to create a file object:
File f = new File("C:/java/hello"); OutputStream f = new FileOutputStream(f);
For specific methods, see: https://www.runoob.com/java/java-files-io.html
The Java File class represents file names and directory pathnames in an abstract way. This class is mainly used for creating files and directories, searching for files, and deleting files.
File objects represent files and directories that actually exist on disk. Create a File object through the following constructor.
Creates a new File instance by converting the given pathname string into an abstract pathname:
File(String pathname);
Create a new File instance based on the parent pathname string and the child pathname string:
File(String parent, String child);
Creates a new File instance by converting the given file: URI into an abstract pathname.
File(URI uri);
The FileReader class inherits from the InputStreamReader class. This class reads data from the stream character by character.
//Create a new FileReader given a File to read data from:
FileReader(File file);
//Create a new FileReader given a FileDescriptor to read data from.
FileReader(FileDescriptor fd);
//Create a new FileReader given the name of a file to read data from.
FileReader(String fileName);
The FileWriter class inherits from the OutputStreamWriter class. This class writes data to the stream character by character. You can create the required objects through the following construction methods
Constructs a FileWriter object given a File object:
FileWriter(File file);
Constructs a FileWriter object given a File object.
FileWriter(File file, boolean append);
The above is the detailed content of What are the basic knowledge points of Java streams and files?. For more information, please follow other related articles on the PHP Chinese website!