From basic to advanced: Learn the use of Core in the Java class library :: IO framework
From basic to advanced: Learn the use of Core in the Java class library :: IO framework
introduce:
In Java programming, IO (Input/Output) is a very important theme.The use of the IO framework can perform files and stream read and write operations, which is the basis for realizing data input and output.The Core :: IO framework in the Java class library provides us with a wealth of IO operation functions and classes, including file reading, writing, byte flow, and character stream. It is an indispensable part of Java programming.
This article will gradually introduce the Core :: IO framework in the Java class library from basic to advanced to help readers understand and master this important programming tool.
1. Basic knowledge:
1. Input and output stream: In Java, data input and output for data through inputStream and outputStream.InputStream is used to read data, OutputStream is used to write data.
2. Byte flow and character stream: IO operations in Java can be divided into byte flow and character stream.Byte Stream is suitable for reading and writing of binary data, and Character Stream is suitable for reading and writing of text data.
2. File read and write operation:
Java's IO framework provides rich file read and write operation functions and classes. The following are some common examples:
1. Create files:
import java.io.File;
import java.io.IOException;
public class FileExample {
public static void main(String[] args) {
try {
File file = new File("example.txt");
if (file.createNewFile()) {
System.out.println ("File creation is successful!");
} else {
System.out.println ("The file exists!");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
2. File writing:
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class FileWriterExample {
public static void main(String[] args) {
try {
File file = new File("example.txt");
FileWriter fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw);
bw.write("Hello, World!");
bw.newLine();
bw.write ("This is an example file.");
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
3. File read:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class FileReaderExample {
public static void main(String[] args) {
try {
File file = new File("example.txt");
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Third, byte flow and character flow:
1. Bytes flow reading operation:
You can use InputStream and OutputStream for reading and writing operations. The following is a simple example:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class ByteStreamExample {
public static void main(String[] args) {
try {
FileInputStream fis = new FileInputStream("input.txt");
FileOutputStream fos = new FileOutputStream("output.txt");
int data;
while ((data = fis.read()) != -1) {
fos.write(data);
}
fis.close();
fos.close();
System.out.println ("" Byte Stream Copy File successful! "););
} catch (IOException e) {
e.printStackTrace();
}
}
}
2. Character stream reading and writing operation:
You can use Reader and Writer for read and write operations. The following is a simple example:
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class CharacterStreamExample {
public static void main(String[] args) {
try {
FileReader reader = new FileReader("input.txt");
FileWriter writer = new FileWriter("output.txt");
int data;
while ((data = reader.read()) != -1) {
writer.write(data);
}
reader.close();
writer.close();
System.out.println ("character stream copy file is successful!"););
} catch (IOException e) {
e.printStackTrace();
}
}
}
in conclusion:
The Core :: IO framework in the Java class library is the basic tool for data input and output. Mastering the framework is very important for Java programming.Through the introduction of this article, readers can gradually learn and understand the core :: IO framework in the Java class library from basic to advanced, and practice according to the example code to enhance the skills of file reading and writing and flow operations.It is hoped that readers can master the use of Core :: IO framework in the java class library through the study of this article.