IO Stream
Byte stream: Any file can be read
java.io.OutputStream: Byte Output Stream
This abstract class is a superclass of all classes representing the output byte stream. An abstract class cannot be used directly and uses its subclasses
Some member methods that are common to subclasses are defined:
- public void close(): Close this output stream and release any system resources associated with it.
- public void flush(): Refresh this output stream and force any buffered output bytes to be written out.
- public void write(byte[] b): Writes b.length bytes from the specified byte array to this output stream.
- public void write(byte[] b, int off, int len): Writes len bytes from the specified byte array and outputs to this output stream starting with offset off.
- public abstract void write(int b): Outputs the specified byte stream.
java.io.FileOutputStream extends OutputStream
FileOutputStream: File Byte Output Stream
Role: Write in-memory data to a file on the hard disk
Construction method:
FileOutputStream(String name) creates an output file stream that writes data to a file with a specified name.
FileOutputStream(File file) creates a file output stream that writes data to the file represented by the specified File object.
Parameter: Purpose of writing data
String name: Destination is the path to a file
File file: Destination is a file
Role of construction methods:
1. Create a FileOutputStream object
2. An empty file will be created based on the file/file path passed in the construction method
3. Point the FileOutputStream object to the created file
Principle of Writing Data (Memory - Hard Disk)
java program -> JVM -> OS -> OS call method to write data -> Write data to file
Steps to use the byte output stream (emphasis):
1. Create a FileOutputStream object and pass the destination of the written data in the construction method
2. Call the method write in the FileOutputStream object to write data to a file
3. Release resources (Stream consumes a certain amount of memory, empty memory after use, provider efficiency)
Example:
public class Demo01OutPutStream { public static void main(String[] args) throws IOException { // 1. Create a FileOutputStream object and pass the destination of the written data in the construction method FileOutputStream fos = new FileOutputStream("C:\\Users\\45109\\IdeaProjects\\pro02\\src\\com\\itheima\\day09\\OutPutStream\\a.txt"); // 2. Call the method write in the FileOutputStream object to write data to a file // public abstract void write(int b): Outputs the specified byte stream. fos.write(97); // 3. Release resources (Stream consumes a certain amount of memory, empty memory after use, provider efficiency) fos.close(); } }
Principle:
Two other methods
/* Write more than one byte at a time: - public void write(byte[] b): Writes b.length bytes from the specified byte array to this output stream. - public void write(byte[] b, int off, int len) : Writes len bytes from the specified byte array, starting with offset off, to this output stream. */ public class Demo02OutPutStream { public static void main(String[] args) throws IOException { //Create a FileOutPutStream object and bind the destination to write data to in the construction method FileOutputStream fos = new FileOutputStream(new File("C:\\Users\\45109\\IdeaProjects\\pro02\\src\\com\\itheima\\day09\\OutPutStream\\b.txt")); //Call the method write in the FileOutPutStream object to write data to a file //Show 100 in file, write a few bytes fos.write(49); fos.write(48); fos.write(48); /* public void write(byte[] b): Writes b.length bytes from the specified byte array to this output stream. Write more than one byte at a time: If the first byte written is a positive number (0-127), the ASCII table is queried when displayed If the first byte written is a negative number, then the first byte and the second byte, which form a Chinese display, query the system default code table (GBK) */ byte[] bytes = {65,66,67,68,69};//ABCDE // byte[] bytes = {-65,66,-67,68,69};//Wipe E fos.write(bytes); /* public void write(byte[] b, int off, int len) : Write a part of the byte array to a file int off:Beginning index of array int len:Write a few bytes */ fos.write(bytes,1,2);///ABCDEBC /* How to write characters: You can use methods in the String class to convert strings to byte arrays byte[] getBytes() Convert string to byte array */ byte[] bytes1 = "Hello".getBytes(); System.out.println(Arrays.toString(bytes1));//[-28, -67, -96, -27, -91, -67] System.out.println(bytes1); fos.write((bytes1)); //Release Resources fos.close(); } }
Continuation and blank line
/* Append/Continue: A construction method using two parameters FileOutputStream(String name, boolean append)Creates an output file stream that writes data to a file with the specified name. FileOutputStream(File file, boolean append) Creates a file output stream that writes data to the file represented by the specified File object. Parameters: String name,File file:Destination to write data boolean append:Append Write Switch true:Creating objects will not overwrite the source file, continue appending data to the end of the file false:Create a new file to overwrite the source file Write Wrap: Write Wrap Symbol windows:\r\n linux:/n mac:/r */ import java.io.FileOutputStream; import java.io.IOException; public class Demo03OutPutStream { public static void main(String[] args) throws IOException { FileOutputStream fos = new FileOutputStream("C:\\Users\\45109\\IdeaProjects\\pro02\\src\\com\\itheima\\day09\\OutPutStream\\c.txt",true); for (int i = 0; i < 10; i++) { fos.write("Hello".getBytes());//Hello once per run, no override fos.write("\r\n".getBytes()); } fos.close(); } }
java.io.InputStream: Byte Input Stream
java.io.InputStream: Byte Input Stream
This abstract class is a superclass of all classes representing byte input streams.
A method that defines the commonality of all subclasses is defined:
int read() reads the next byte of data from the input stream.
int read(byte[] b) reads a certain number of bytes from the input stream and stores them in the buffer array B.
void close() closes this input stream and releases all system resources associated with it.
java.io.FileInputStream extends InputStream
FileInputStream: File Byte Input Stream
Role: Read data from hard disk files into memory for use
Construction method:
FileInputStream(String name)
FileInputStream(File file)
Parameter: Data source to read file
String name: Path to file
File file:
Role of construction methods:
1. A FileInputStream object is created
2. The FileInputStream object is specified as the file to be read in the construction method
How to read data (hard disk-memory)
java program ->JVM ->OS ->How the OS reads data ->Read files
Steps to use the byte input stream (emphasis):
1. Create a FileInputStream object and bind the data source to be read in the construction method
2. Read the file using the method read in the FileInputStream object
3. Release resources
Example:
public class Demo01InPutStream { public static void main(String[] args) throws IOException { // 1. Create a FileInputStream object and bind the data source to be read in the construction method FileInputStream fis = new FileInputStream("C:\\Users\\45109\\IdeaProjects\\pro02\\src\\com\\itheima\\day09\\OutPutStream\\a.txt"); // 2. Read the file using the method read in the FileInputStream object //int read() reads a byte in the file and returns it, and returns -1 when read to the end of the file /* int len = fis.read(); System.out.println(len);//97 a len = fis.read(); System.out.println(len);//98 b len = fis.read(); System.out.println(len);//99 c len = fis.read(); System.out.println(len);//-1*/ /* Finding that reading the file above is a duplicate process, so you can use circular optimization Don't know how many bytes there are in the file, use a while loop while Loop end condition, end when reading -1 Boolean expression (len = fis.read())!=-1 1.fis.read():Read a byte 2.len = fis.read():Assign read bytes to variable len 3.(len = fis.read())!=-1:Determine if the variable len is not equal to -1 */ int len =0; while ((len=fis.read())!=-1){ System.out.print(len);//97 98 99 System.out.print((char) len);//abc } // 3. Release resources fis.close(); }
principle
Method of printing more than one byte at a time
Byte input stream read multiple bytes at once:
int read(byte[] b) reads a certain number of bytes from the input stream and stores them in the buffer array B.
Make two things clear:
1. The function of the parameter byte[] of the method?
Buffer to store multiple bytes read at a time
The length of an array is generally defined as an integer multiple of 1024(1kb) or 1024
2. What is the return value int of the method?
Number of valid bytes per read
Construction method of String class
String(byte[] bytes): Converts a byte array to a string
String(byte[] bytes, int offset, int length) converts a portion of a byte array to a string offset: the starting index length of the array: the number of bytes to convert
Example:
public class Demo02InPutStream { public static void main(String[] args) throws IOException { //Create a FileInPutStream object and bind the data source to read in the construction method FileInputStream fis = new FileInputStream("C:\\Users\\45109\\IdeaProjects\\pro02\\src\\com\\itheima\\day09\\OutPutStream\\b.txt"); //Read a file using the method read in the FileInPutStream object // int read(byte[] b) reads a certain number of bytes from the input stream and stores them in the buffer array B. /* byte[] bytes = new byte[2]; int len = fis.read(bytes); System.out.println(len);//2 Number of bytes read per time // System.out.println(Arrays.toString(bytes));//[65, 66] System.out.println(new String(bytes));//AB len = fis.read(bytes); System.out.println(len);//2 System.out.println(new String(bytes));//CD len = fis.read(bytes); System.out.println(len);// System.out.println(new String(bytes));//ED len = fis.read(bytes); System.out.println(len);//-1 System.out.println(new String(bytes));//ED*/ /* Loop optimization can be used to find a duplicate process during the above read Don't know how many bytes there are in the file, so use a while loop while The condition under which the loop ends, reading to the end of -1 */ byte[] bytes = new byte[1024];//Store read bytes int len = 0; //Record the number of valid bytes per read while ((len= fis.read(bytes))!=-1){ // String(byte[] bytes, int offset, int length) converts a portion of a byte array to a string offset: the starting index length of the array: the number of bytes to convert System.out.println(new String(bytes,0,len)); } //Release Resources fis.close(); } }
Byte Stream Exercise: Picture Copy
Principle:
File copying exercise: read-write
To make clear:
Data source: D:\PTE\17.jpg
Destination of data: C:\111
Steps for copying files:
1. Create a byte input stream object that binds the data source to be read in the construction method
2. Create a byte output stream object that binds the destination to be written in the construction method
3. Read the file using the method read in the byte input stream object
4. Write the read bytes to the destination file using the method write in the byte output stream
5. Release resources
```java import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class Demo03CopyFile { public static void main(String[] args) throws IOException { long s = System.currentTimeMillis(); //1. Create a byte input stream object that binds the data source to be read in the construction method FileInputStream fis = new FileInputStream("D:\\PTE\\17.jpg"); //2. Create a byte output stream object that binds the destination to be written in the construction method FileOutputStream fos = new FileOutputStream("C:\\111\\17.jpg"); /* // 3.Read the file using the method read in the byte input stream object //How to read one byte at a time int len = 0; while ((len = fis.read())!= -1) { //4.Write the read bytes to the destination file using the method write in the byte output stream fos.write(len); } */ //Read and write multiple bytes using array buffer byte[] bytes = new byte[1024]; int len =0;//Number of valid bytes per read while ((len=fis.read(bytes))!=-1){ fos.write(bytes); } //5. Release resources (write first, then read; if you finish, you must finish reading) fos.close(); fis.close(); long e = System.currentTimeMillis(); System.out.println("Total time spent copying files" +(e-s) + "Millisecond"); } }
Using Byte Stream to Read Chinese Files
1 Chinese
GBK: Two bytes
UTF-8: occupies 3 bytes
import java.io.FileInputStream; import java.io.IOException; import java.util.Arrays; public class Demo01InputStream { public static void main(String[] args) throws IOException { FileInputStream fis = new FileInputStream("C:\\Users\\45109\\IdeaProjects\\pro02\\src\\com\\itheima\\day09\\OutPutStream\\c.txt"); int len = 0; byte[] bytes = new byte[6]; while ((len = fis.read())!=-1){ System.out.println((char) len);//There will be scrambling } fis.close(); } }
Character Stream
java.io.Reader: Character input stream, the top-most parent of character input stream, defines some common member methods and is an abstract class
Common member methods:
int read() reads a single character and returns it.
int read(char[] char) reads more than one character at a time and reads the characters into an array.
void close() closes the stream and releases all resources associated with it.
java.io.FileReader extends InputStreamReader extends Reader
FileReader: File character input stream
Role: Read data from hard disk files into memory as characters
Construction method:
FileReader(String fileName)
FileReader(File file)
Parameter: Data source to read file
String fileName: Path to file
File file: A file
Role of the FileReader constructor:
1. Create a FileReader object
2. Point the FileReader object to the file you want to read
Steps to use character input stream:
1. Create a FileReader object and bind the data source to be read in the construction method
2. Read the file using the method read in the FileReader object
3. Release resources
import java.io.FileReader; import java.io.IOException; public class Demo01Reader { public static void main(String[] args) throws IOException { //1. Create a FileReader object and bind the data source to be read in the construction method FileReader fr = new FileReader("C:\\Users\\45109\\IdeaProjects\\pro02\\src\\com\\itheima\\day09\\OutPutStream\\c.txt"); //2. Read the file using the method read in the FileReader object //int read() reads a single character and returns /* int len = 0; while ((len = fr.read())!=-1){ System.out.print((char) len);//Hello 123abc }*/ // int read(char[] char) reads more than one character at a time and reads the characters into an array. char[] cs = new char[1024];//Store read characters int len = 0;//Recorded is the number of valid characters per read while ((len=fr.read(cs))!=-1){ /* String Class construction methods String(char[] value) Convert Character Array to String String(char[] value, int offset, int count) Number of start index count conversions that convert part of a character array to a string offset array */ // System.out.println (new String (cs, 0, len);//Hello 123abc System.out.println(cs); } fr.close(); } }
Character Output Stream
java.io.Writer: Character output stream, the top-most parent of all character output streams, is an abstract class
Common member methods:
- void write(int c) writes a single character.
- void write(char[] cbuf) writes an array of characters.
- abstract void write(char[] cbuf, int off, int len) writes to a part of the character array, the start index of the off array, and the number of characters len writes.
- void write(String str) writes a string.
- void write(String str, int off, int len) writes to a part of the string, the start index of the off string, and the number of characters len writes.
- void flush() refreshes the buffer of the stream.
- void close() closes the stream, but refreshes it first.
java.io.FileWriter extends OutputStreamWriter extends Writer
FileWriter: File character output stream
Role: Write in-memory character data to a file
Construction method:
FileWriter(File file) constructs a FileWriter object from a given File object.
FileWriter(String fileName) constructs a FileWriter object from a given file name.
Parameter: Destination to write data
String fileName: Path to file
File file: is a file
Role of construction methods:
1. A FileWriter object is created
2. Files are created based on the path of the file/file passed in the construction method
3. Point the FileWriter object to the created file
Steps to use character output stream (emphasis):
1. Create a FileWriter object and bind the destination of the data to be written in the construction method
2. Write the data to the memory buffer using the method write in FileWriter (the process of converting characters to bytes)
3. Use the method flush in FileWriter to refresh the data in the memory buffer to the file
4. Release resources (the data in the memory buffer will be flushed to the file first)
import java.io.FileWriter; import java.io.IOException; public class Demo01Writer { public static void main(String[] args) throws IOException { //1. Create a FileWriter object and bind the destination of the data to be written in the construction method FileWriter fw = new FileWriter("C:\\Users\\45109\\IdeaProjects\\pro02\\src\\com\\itheima\\day09\\OutPutStream\\c.txt"); //2. Write the data to the memory buffer using the method write in FileWriter (the process of converting characters to bytes) fw.write("int c"); fw.write("97"); //3. Use the method flush in FileWriter to refresh the data in the memory buffer to the file fw.flush(); //4. Release resources (the data in the memory buffer will be flushed to the file first) fw.close(); } }
Differences between flush and close methods
Differences between flush and close methods
- flush: Refresh the buffer so that the stream object can continue to be used.
- close: First refresh the buffer, then notify the system to release the resources. Stream objects can no longer be used.
public class Demo02CloseFlush { public static void main(String[] args) throws IOException { //1. Create a FileWriter object and bind the destination of the data to be written in the construction method FileWriter fw = new FileWriter("C:\\Users\\45109\\IdeaProjects\\pro02\\src\\com\\itheima\\day09\\OutPutStream\\c.txt"); //2. Write the data to the memory buffer using the method write in FileWriter (the process of converting characters to bytes) fw.write("int c"); fw.write(98); //3. Use the method flush in FileWriter to refresh the data in the memory buffer to the file fw.flush(); //Resume usage after refresh fw.write(99); //4. Release resources (the data in the memory buffer will be flushed to the file first) fw.close(); //After the close method, the stream has been closed, has disappeared from memory, and can no longer be used //fw.write(99);//IOException: Stream closed } }
Other methods of character output streaming data
Other methods of character output streaming data
- void write(char[] char) writes an array of characters.
- abstract void write(char[] char, int off, int len) writes to a part of the character array, the start index of the off array, and the number of characters written by len.
- void write(String str) writes a string.
- void write(String str, int off, int len) writes to a part of the string, the start index of the off string, and the number of characters len writes.
public class Demo03Write { public static void main(String[] args) throws IOException { FileWriter fw = new FileWriter("C:\\Users\\45109\\IdeaProjects\\pro02\\src\\com\\itheima\\day09\\OutPutStream\\f.txt"); char[] cs ={'a','b','c'}; // void write(char[] char) writes an array of characters. fw.write(cs);//abc // Abstract void write (char[] char, int off, int len) Writes to a part of the character array, the start index of the off array, the number of characters written by Len fw.write(cs,1,2); // void write(String str) writes a string. fw.write("Eat eat eat c");//Eat Eat c // void write(String str, int off, int len) writes to a part of the string, the start index of the off string, and the number of characters written by len. fw.write("Programmer",0,2);//program fw.close();//abcbc } }
Continuation and Line Break of Character Stream
/* Rewrite and Line Break Continuation, append: construction using two parameters FileWriter(String fileName, boolean append) FileWriter(File file, boolean append) Parameters: String fileName,File file:Destination to write data boolean append:Rewrite switch true: no new file will be created to overwrite the source file, you can continue;false: Create a new file to overwrite the source file Line Break: Line Break Symbol windows:\r\n linux:/n mac:/r */ import java.io.FileWriter; import java.io.IOException; public class Demo04Write { public static void main(String[] args) throws IOException { FileWriter fw = new FileWriter ("C:\\Users\\45109\\IdeaProjects\\pro02\\src\\com\\itheima\\day09\\OutPutStream\\c.txt",true); for (int i = 0; i < 20; i++) { fw.write("Hello" + i + "\r\n"); } fw.close(); } }
IO exception handling
Use try...catch...finally to process
Use try catch finally to handle exceptions in streams before jdk1.7
Format:
try{
Code that may produce exceptions
}catch (variable name of exception class){
Exception handling logic
}finally{
Code that must be specified
Resource Release
}
import java.io.FileWriter; import java.io.IOException; public class Demo01TryCatch { public static void main(String[] args) { //Increase the scope of the variable fw so finally can use it //Variables can be defined without a value, but they must be used with a value //FW = new FileWriter (""C:\Users\4510...", true); execution failed, FW has no value, fw.close will error FileWriter fw = null; try { fw = new FileWriter ("C:\\Users\\45109\\IdeaProjects\\pro02\\src\\com\\itheima\\day09\\OutPutStream\\g.txt",true); for (int i = 0; i < 20; i++) { fw.write("Hello" + i + "\r\n"); } } catch (IOException e){ //Exception handling logic System.out.println(e); }finally { //Code that will be executed //Failed to create object, fw default value is null,null cannot call method, will throw NullPointerException, need to add a judgment, not null to release the resource if(fw!=null){ try { fw.close(); //The fw.close method declaration throws an IOException exception object, so we will either throws or try catch the exception object } catch (IOException e) { e.printStackTrace(); } } } } }
New features of JDK
New features of JDK7
You can add an () to the end of a try and define the stream object in parentheses
So the scope of this stream object is valid in try
When the code in try is executed, the stream object is automatically released without writing finally
Format:
Try (Define Stream Object; Define Stream Object...){
Code that may produce exceptions
}catch (variable name of exception class){
Exception handling logic
}
public class Demo02JDK7 { public static void main(String[] args) { try(//1. Create a byte input stream object that binds the data source to be read in the construction method FileInputStream fis = new FileInputStream("D:\\PTE\\17.jpg"); //2. Create a byte output stream object that binds the destination to be written in the construction method FileOutputStream fos = new FileOutputStream("C:\\111\\17.jpg"); ) { //3. Read the file using the method read in the byte input stream object //How to read one byte at a time int len = 0; while ((len = fis.read()) != -1) { //4. Write the read bytes to the destination file using the method write in the byte output stream fos.write(len); } }catch (IOException e){ System.out.println(e); } // } }
New features of JDK9
/* JDK9 New features try Flow objects can be defined on the front The name of the stream object (variable name) can be introduced directly in () after the try After the try code is executed, the stream object can also be freed without writing finally Format: A a = new A(); B b = new B(); try(a,b){ Code that may produce exceptions }catch(Exception Class Variable Name) { Exception handling logic } */ import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class Demo03JDK9 { public static void main(String[] args) throws FileNotFoundException { //1. Create a byte input stream object that binds the data source to be read in the construction method FileInputStream fis = new FileInputStream("D:\\PTE\\17.jpg"); //2. Create a byte output stream object that binds the destination to be written in the construction method FileOutputStream fos = new FileOutputStream("C:\\111\\17.jpg"); try(fis;fos) { //3. Read the file using the method read in the byte input stream object //How to read one byte at a time int len = 0; while ((len = fis.read()) != -1) { //4. Write the read bytes to the destination file using the method write in the byte output stream fos.write(len); } }catch (IOException e){ System.out.println(e); } //fos.write(1);//Stream Closed // } }
Properties Collection
java.util.Properties collection extends Hashtable<k, v> implements Map<k, v>
The Properties class represents a persistent set of properties. Properties can be saved in or loaded from the stream.
The Properties collection is a unique collection combined with IO streams
You can use the method store in the Properties collection to persist temporary data from the collection to hard disk storage
You can use the method load in the Properties collection to read files (key-value pairs) saved on your hard disk to use in the collection
Each key and its corresponding value in the list of attributes is a string.
The Properties collection is a two-column collection, and key and value are strings by default
The Properties collection has some special methods for manipulating strings
Example:
/* Store data using the Properties collection, traverse to retrieve data from the Properties collection Properties Collection is a two-column collection, key and value are strings by default Properties Collections have some special ways to manipulate strings Object setProperty(String key, String value) Call Hashtable's method put. String getProperty(String key) value values are found by key, which is equivalent to get(key) methods in a Map collection Set<String> stringPropertyNames() Returns the keyset in this list of attributes, where the key and its corresponding value are strings, equivalent to the keySet method in the Map collection */ private static void show01() { //Create Properties Collection Object Properties prop = new Properties(); //Add data to a collection using setProperty prop.setProperty("W","188"); prop.setProperty("z","198"); prop.setProperty("x","178"); //Use stringPropertyNames to remove keys from the Properties collection and store them in a Set collection Set<String> set = prop.stringPropertyNames(); //Traverse the Set collection, taking out each key of the Properties collection for (String key : set) { String value = prop.getProperty(key); System.out.println(key+value); } } }
You can use the method store in the Properties collection
You can use the method store in the Properties collection to persist temporary data from the collection to hard disk storage
void store(OutputStream out, String comments)
void store(Writer writer, String comments)
Parameters:
OutputStream out: Byte output stream, cannot write to Chinese
Writer writer: A character output stream that can be written in Chinese
String comments: Comments explaining what the saved file does
Can't use Chinese, will cause chaos, default is Unicode encoding
Empty string "" is commonly used
Steps to use:
1. Create Properties collection objects and add data
2. Create byte/character output stream objects and bind the destination to be output in the construction method
3. Use the method store in the Properties collection to write temporary data in the collection to the hard disk for persistence
4. Release resources
Example:
private static void show02() throws IOException { //1. Create Properties collection objects and add data Properties prop = new Properties(); prop.setProperty("Ze","188"); prop.setProperty("z","198"); prop.setProperty("x","178"); // 2. Create byte/character output stream objects and bind the destination to be output in the construction method // FileWriter fw = new FileWriter("C:\\Users\\45109\\IdeaProjects\\pro02\\src\\com\\itheima\\day09\\OutPutStream\\c.txt"); // 3. Use the method store in the Properties collection to write temporary data in the collection to the hard disk for persistence // prop.store(fw,"save data"); //Release Resources // fw.close(); //The Chinese language is scrambled, indicating that byte streams cannot be written at noon prop.store(new FileOutputStream("C:\\Users\\45109\\IdeaProjects\\pro02\\src\\com\\itheima\\day09\\OutPutStream\\f.txt"),""); }
/* You can use the method load in the Properties collection to read files (key-value pairs) saved on your hard disk to use in the collection void load(InputStream inStream) void load(Reader reader) Parameters: InputStream inStream:Byte input stream, cannot read key-value pairs containing Chinese Reader reader:Character input stream to read key-value pairs containing Chinese Steps to use: 1.Create Properties Collection Object 2.Read the file holding the key-value pair using the method load in the Properties collection object 3.Traversing through the Properties collection Be careful: 1.In a file that stores key-value pairs, the default connection symbol for keys and values can use =, spaces (other symbols) 2.In a file that stores key-value pairs, you can use # to comment, and the commented key-value pairs will no longer be read 3.In files that store key-value pairs, keys and values are strings by default, without quotation marks */ private static void show03() throws IOException { // 1. Create Properties Collection Object Properties prop = new Properties(); //2. Use the method load in the Properties collection object to read files that hold key-value pairs //You cannot read Chinese using byte streams, so character streams are commonly used. prop.load(new FileInputStream("C:\\Users\\45109\\IdeaProjects\\pro02\\src\\com\\itheima\\day09\\OutPutStream\\c.txt")); //3. Traverse the Properties collection Set<String> set = prop.stringPropertyNames(); for (String key : set) { String value = prop.getProperty(key); System.out.println(key + value); } }