1.Stream
a.Definition:
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.
b.Console input:
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()
c.Console output:
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');
}
2.File file reading and writing:
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.
a.FileInputStream:
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);
b.FileOutputStream:
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
c.File Class:
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);
d.Java FileReader class:
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);
e.Java FileWriter class:
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!

Bytecodeachievesplatformindependencebybeingexecutedbyavirtualmachine(VM),allowingcodetorunonanyplatformwiththeappropriateVM.Forexample,JavabytecodecanrunonanydevicewithaJVM,enabling"writeonce,runanywhere"functionality.Whilebytecodeoffersenh

Java cannot achieve 100% platform independence, but its platform independence is implemented through JVM and bytecode to ensure that the code runs on different platforms. Specific implementations include: 1. Compilation into bytecode; 2. Interpretation and execution of JVM; 3. Consistency of the standard library. However, JVM implementation differences, operating system and hardware differences, and compatibility of third-party libraries may affect its platform independence.

Java realizes platform independence through "write once, run everywhere" and improves code maintainability: 1. High code reuse and reduces duplicate development; 2. Low maintenance cost, only one modification is required; 3. High team collaboration efficiency is high, convenient for knowledge sharing.

The main challenges facing creating a JVM on a new platform include hardware compatibility, operating system compatibility, and performance optimization. 1. Hardware compatibility: It is necessary to ensure that the JVM can correctly use the processor instruction set of the new platform, such as RISC-V. 2. Operating system compatibility: The JVM needs to correctly call the system API of the new platform, such as Linux. 3. Performance optimization: Performance testing and tuning are required, and the garbage collection strategy is adjusted to adapt to the memory characteristics of the new platform.

JavaFXeffectivelyaddressesplatforminconsistenciesinGUIdevelopmentbyusingaplatform-agnosticscenegraphandCSSstyling.1)Itabstractsplatformspecificsthroughascenegraph,ensuringconsistentrenderingacrossWindows,macOS,andLinux.2)CSSstylingallowsforfine-tunin

JVM works by converting Java code into machine code and managing resources. 1) Class loading: Load the .class file into memory. 2) Runtime data area: manage memory area. 3) Execution engine: interpret or compile execution bytecode. 4) Local method interface: interact with the operating system through JNI.

JVM enables Java to run across platforms. 1) JVM loads, validates and executes bytecode. 2) JVM's work includes class loading, bytecode verification, interpretation execution and memory management. 3) JVM supports advanced features such as dynamic class loading and reflection.

Java applications can run on different operating systems through the following steps: 1) Use File or Paths class to process file paths; 2) Set and obtain environment variables through System.getenv(); 3) Use Maven or Gradle to manage dependencies and test. Java's cross-platform capabilities rely on the JVM's abstraction layer, but still require manual handling of certain operating system-specific features.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Atom editor mac version download
The most popular open source editor

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Zend Studio 13.0.1
Powerful PHP integrated development environment

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function
