JAVA basics-Chapter 18 byte stream, character stream

main content

IO stream
Byte stream
Character stream
Exception handling
Properties

teaching objectives

Able to tell the classification and function of IO stream,
can use byte output stream to write data to file,
can use byte input stream to read data to program,
can understand the principle of read data read(byte[]) method
can use byte stream Completion of file copying
Ability to use FileWirter to write data to a file
Ability to tell the difference between the closing and refreshing methods in
FileWriter 5 methods
that can use FileWriter to write data Ability to use FileWriter to write data to achieve line breaks and additional writing
Ability to read data
using FileReader Ability to read data using FileReader The data is one character array at a time
. The configuration information in the file can be loaded using the load method of Properties

Chapter One IO Overview

1.1 What is IO

In your life, you must have experienced such a scene. When you edit a text file and forget ctrl+s, the file may be edited for nothing. When you
insert a USB flash drive into your computer , you can copy a video to your computer's hard drive. So which devices are the data on? Keyboard, memory, hard
disk, external devices, etc.

We regard this kind of data transmission as a kind of data flow. According to the direction of flow, based on memory, it is divided into input input and output
output, that is, the flow to the memory is the input stream, and the output flow out of the memory.

I/O operations in Java mainly refer to the use of contents under the java.io package for input and output operations. Input is also called reading data, and output is also called writing
data.

1.2 Classification of IO

Input stream output stream

Byte stream

Byte input stream

InputStream

Byte output stream

OutputStream

Character stream

Character input stream

Reader

Character output stream

Writer

According to the flow of data, it is divided into: input stream and output stream.

Input stream: A stream that reads data from other devices into memory.

Output stream: A stream that writes data out of memory to other devices.

There are two types of layout data: byte stream and character stream.

Byte stream: The stream of reading and writing data in bytes.

Character stream: A stream of reading and writing data in units of characters.

1.3 Illustration of the flow of IO

1.4 Top Parents

Chapter 2 Byte Stream

2.1 Everything is a byte

All file data (texts, pictures, videos, etc.) are stored in the form of binary numbers when they are stored, and they are all bytes one by one.

That's it. Therefore, the byte stream can transmit arbitrary file data. When manipulating streams, we must always be clear, no matter what stream object is used, the bottom

The data transmitted by the layer is always binary data.

2.2 Byte output stream [OutputStream]

The java.io.OutputStream abstract class is the superclass of all classes that represent byte output streams, and writes the specified byte information to the destination. It defines the
basic common function method of byte output stream.

public void close() :关闭此输出流并释放与此流相关联的任何系统资源。
public void flush() :刷新此输出流并强制任何缓冲的输出字节被写出。
public void write(byte[] b):将 b.length字节从指定的字节数组写入此输出流。
public void write(byte[] b, int off, int len) :从指定的字节数组写入 len字节,从偏移量 off开始输
出到此输出流。
public abstract void write(int b) :将指定的字节输出流。
小贴士:
close方法,当完成流的操作时,必须调用此方法,释放系统资源。

2.3 FileOutputStream类

OutputStream有很多子类,我们从最简单的一个子类开始。
java.io.FileOutputStream类是文件输出流,用于将数据写出到文件。

Construction method

public FileOutputStream(File file):创建文件输出流以写入由指定的 File对象表示的文件。
public FileOutputStream(String name): 创建文件输出流以指定的名称写入文件。

When you create a stream object, you must pass in a file path. If there is no such file in this path, it will be created. If there
is this file, the data in this file will be cleared.

构造举例,代码如下:

Write out byte data

1. 写出字节:write(int b) 方法,每次可以写出一个字节数据,代码使用演示:
public class FileOutputStreamConstructor throws IOException {
public static void main(String[] args) {
// 使用File对象创建流对象
File file = new File("a.txt");
FileOutputStream fos = new FileOutputStream(file);
// 使用文件名称创建流对象
FileOutputStream fos = new FileOutputStream("b.txt");
    }
}
public class FOSWrite {

Tips:

1. 虽然参数为int类型四个字节,但是只会保留一个字节的信息写出。
2. 流操作完毕后,必须释放系统资源,调用close方法,千万记得。
  1. Write out the byte array: write(byte[] b), you can write the data in the array each time, the code usage demonstration:

  2. Write a byte array of the specified length: write(byte[] b, int off, int len), each time you write out len ​​bytes from the off index, the code
    Demo:

public static void main(String[] args) throws IOException { // Use the file name to create a stream object FileOutputStream fos = new FileOutputStream("fos.txt"); // write out the data fos.write( 97 ); // write out The first byte fos.write( 98 ); // write the second byte fos.write( 99 ); // write the third byte // close the resource fos.close(); } } output Result: abc











public class FOSWrite { public static void main(String[] args) throws IOException { // Use the file name to create a stream object FileOutputStream fos = new FileOutputStream("fos.txt"); // Convert a string to a byte array byte[] b = "Dark Horse Programmer".getBytes(); // Write out the byte array data fos.write(b); // Close the resource fos.close(); } } Output result: Dark Horse Programmer












public class FOSWrite { public static void main(String[] args) throws IOException { // Use the file name to create a stream object FileOutputStream fos = new FileOutputStream("fos.txt"); // Convert a string to a byte array byte[] b = "abcde".getBytes(); // Write out 2 bytes starting from index 2. Index 2 is c, two bytes, which is cd. fos.write(b, 2, 2 ); // Close the resource fos.close(); } } Output result: cd












Data append and write

After the above demonstration, every time the program runs and the output stream object is created, the data in the target file will be cleared. How to keep the data in the target file, but also

Continue to add new data?

public FileOutputStream(File file, boolean append): 创建文件输出流以写入由指定的 File对象表示的
文件。
public FileOutputStream(String name, boolean append): 创建文件输出流以指定的名称写入文件。

For these two construction methods, a boolean type value needs to be passed in the parameters, true means additional data, and false means clear original data.
With the output stream object created in this way, you can specify whether to append or continue writing. The code usage demonstration:

Write newline

In Windows, the newline symbol is \r\n. Put

To specify whether to append or continue writing, the code usage demonstration:

public class FOSWrite {
public static void main(String[] args) throws IOException {
// 使用文件名称创建流对象
FileOutputStream fos = new FileOutputStream("fos.txt",true);    
// 字符串转换为字节数组
byte[] b = "abcde".getBytes();
// 写出从索引 2 开始, 2 个字节。索引 2 是c,两个字节,也就是cd。
fos.write(b);
// 关闭资源
fos.close();
    }
}
文件操作前:cd
文件操作后:cdabcde
public class FOSWrite {
public static void main(String[] args) throws IOException {
// 使用文件名称创建流对象
FileOutputStream fos = new FileOutputStream("fos.txt");  
// 定义字节数组
byte[] words = { 97 , 98 , 99 , 100 , 101 };
// 遍历数组
for (int i =  0 ; i < words.length; i++) {
// 写出一个字节
fos.write(words[i]);
// 写出一个换行, 换行符号转成数组写出
fos.write("\r\n".getBytes());
        }
// 关闭资源
fos.close();
    }
}
回车符\r和换行符\n :
回车符:回到一行的开头(return)。
换行符:下一行(newline)。
系统中的换行:
Windows系统里,每行结尾是 回车+换行 ,即\r\n;
Unix系统里,每行结尾只有 换行 ,即\n;
Mac系统里,每行结尾是 回车 ,即\r。从 Mac OS X开始与Linux统一。

2.4 Byte input stream [InputStream]

The java.io.InputStream abstract class is the superclass of all classes that represent byte input streams, and can read byte information into memory. It defines the
basic common functional methods of byte input streams.

public void close() :关闭此输入流并释放与此流相关联的任何系统资源。
public abstract int read(): 从输入流读取数据的下一个字节。
public int read(byte[] b): 从输入流中读取一些字节数,并将它们存储到字节数组 b中 。
小贴士:
close方法,当完成流的操作时,必须调用此方法,释放系统资源。

2.5 FileInputStream class

java.io.FileInputStream类是文件输入流,从文件中读取字节。

Construction method

FileInputStream(File file): 通过打开与实际文件的连接来创建一个 FileInputStream ,该文件由文件系
统中的 File对象 file命名。
FileInputStream(String name): 通过打开与实际文件的连接来创建一个 FileInputStream ,该文件由文件
系统中的路径名 name命名。

When you create a stream object, you must pass in a file path. If there is no such file in this path, FileNotFoundException will be thrown.

构造举例,代码如下:

Output result:

a
b
c
d
e

Read byte data

1. 读取字节:read方法,每次可以读取一个字节的数据,提升为int类型,读取到文件末尾,返回- 1 ,代码使
用演示:

Circulate to improve the reading method, code demonstration:

public class FileInputStreamConstructor throws IOException{
public static void main(String[] args) {
// 使用File对象创建流对象
File file = new File("a.txt");
FileInputStream fos = new FileInputStream(file);
// 使用文件名称创建流对象
FileInputStream fos = new FileInputStream("b.txt");
    }
}
public class FISRead {
public static void main(String[] args) throws IOException{
// 使用文件名称创建流对象
FileInputStream fis = new FileInputStream("read.txt");
// 读取数据,返回一个字节
int read = fis.read();
System.out.println((char) read);
read = fis.read();
System.out.println((char) read);
read = fis.read();
System.out.println((char) read);
read = fis.read();
System.out.println((char) read);
read = fis.read();
System.out.println((char) read);
// 读取到末尾,返回‐
read = fis.read();
System.out.println( read);
// 关闭资源
fis.close();
    }
}
输出结果:
a
b
c
d
e
‐ 1
public class FISRead {
public static void main(String[] args) throws IOException{
// 使用文件名称创建流对象
FileInputStream fis = new FileInputStream("read.txt");

Tips:

1. 虽然读取了一个字节,但是会自动提升为int类型。
2. 流操作完毕后,必须释放系统资源,调用close方法,千万记得。
2. 使用字节数组读取:read(byte[] b),每次读取b的长度个字节到数组中,返回读取到的有效字节个数,读
取到末尾时,返回- 1 ,代码使用演示:

The error data d is because only one byte e was read in the last read. In the array, the data read last time has not been completely replaced, so the
valid bytes must be obtained through len. The code usage demonstration:

FileInputStream fis = new FileInputStream("read.txt");
// 定义变量,保存数据
int b ;
// 循环读取
while ((b = fis.read())!=‐ 1 ) {
System.out.println((char)b);
        }
// 关闭资源
fis.close();
    }
}
输出结果:
a
b
c
d
e
public class FISRead {
public static void main(String[] args) throws IOException{
// 使用文件名称创建流对象.
FileInputStream fis = new FileInputStream("read.txt"); // 文件中为abcde
// 定义变量,作为有效个数
int len ;
// 定义字节数组,作为装字节数据的容器
byte[] b = new byte[ 2 ];
// 循环读取
while (( len= fis.read(b))!=‐ 1 ) {
// 每次读取后,把数组变成字符串打印
System.out.println(new String(b));
        }
// 关闭资源
fis.close();
    }
}
输出结果:
ab
cd
ed

Tips:

Use an array to read, read multiple bytes at a time, reduce the number of IO operations between systems, thereby improving the efficiency of reading and writing, it is recommended to use

use.

2.6 Byte Stream Exercise: Picture Copy

Copy principle diagram

Case realization

public class FISRead {
public static void main(String[] args) throws IOException{
// 使用文件名称创建流对象.
FileInputStream fis = new FileInputStream("read.txt"); // 文件中为abcde
// 定义变量,作为有效个数
int len ;
// 定义字节数组,作为装字节数据的容器
byte[] b = new byte[ 2 ];
// 循环读取
while (( len= fis.read(b))!=‐ 1 ) {
// 每次读取后,把数组的有效字节部分,变成字符串打印
System.out.println(new String(b, 0 ,len));//  len 每次读取的有效字节个数
        }
// 关闭资源
fis.close();
    }
}
输出结果:
ab
cd
e

Copy the picture file, the code usage demo:

Tips:

The principle of flow closing: open first and then close, then open and close.

Chapter 3 Character Stream

There may be a small problem when using byte streams to read text files. When encountering Chinese characters, the complete characters may not be displayed. That is because

A Chinese character may occupy multiple bytes of storage. Therefore, Java provides some character stream classes, which read and write data in units of characters, specifically for processing text
files.

3.1 Character input stream [Reader]

The java.io.Reader abstract class is a super class that represents all classes used to read character streams, and can read character information into memory. It defines the
basic common functional methods of character input streams.

public void close() :关闭此流并释放与此流相关联的任何系统资源。
public int read(): 从输入流读取一个字符。
public int read(char[] cbuf): 从输入流中读取一些字符,并将它们存储到字符数组 cbuf中 。

3.2 FileReader class

java.io.FileReader类是读取字符文件的便利类。构造时使用系统默认的字符编码和默认字节缓冲区。
小贴士:
public class Copy {
public static void main(String[] args) throws IOException {
// 1.创建流对象
// 1.1 指定数据源
FileInputStream fis = new FileInputStream("D:\\test.jpg");
// 1.2 指定目的地
FileOutputStream fos = new FileOutputStream("test_copy.jpg");
// 2.读写数据
// 2.1 定义数组
byte[] b = new byte[ 1024 ];
// 2.2 定义长度
int len;
// 2.3 循环读取
while ((len = fis.read(b))!=‐ 1 ) {
// 2.4 写出数据
fos.write(b,  0  , len);
        }
// 3.关闭资源
fos.close();
fis.close();
    }
}
1. 字符编码:字节与字符的对应规则。Windows系统的中文编码默认是GBK编码表。
idea中UTF-
2. 字节缓冲区:一个字节数组,用来临时存储字节数据。

Construction method

FileReader(File file): 创建一个新的 FileReader ,给定要读取的File对象。
FileReader(String fileName): 创建一个新的 FileReader ,给定要读取的文件的名称。

When you create a stream object, you must pass in a file path. Similar to FileInputStream.

构造举例,代码如下:

Read character data

1. 读取字符:read方法,每次可以读取一个字符的数据,提升为int类型,读取到文件末尾,返回- 1 ,循环读
取,代码使用演示:
小贴士:虽然读取了一个字符,但是会自动提升为int类型。
public class FileReaderConstructor throws IOException{
public static void main(String[] args) {
// 使用File对象创建流对象
File file = new File("a.txt");
FileReader fr = new FileReader(file);
// 使用文件名称创建流对象
FileReader fr = new FileReader("b.txt");
    }
}
public class FRRead {
public static void main(String[] args) throws IOException {
// 使用文件名称创建流对象
FileReader fr = new FileReader("read.txt");
// 定义变量,保存数据
int b ;
// 循环读取
while ((b = fr.read())!=‐ 1 ) {
System.out.println((char)b);
        }
// 关闭资源
fr.close();
    }
}
输出结果:
黑
马
程
序
员
2. 使用字符数组读取:read(char[] cbuf),每次读取b的长度个字符到数组中,返回读取到的有效字符个数,
读取到末尾时,返回- 1 ,代码使用演示:

Get effective character improvement, code usage demonstration:

3.3 Character output stream [Writer]

public class FRRead {
public static void main(String[] args) throws IOException {
// 使用文件名称创建流对象
FileReader fr = new FileReader("read.txt");
// 定义变量,保存有效字符个数
int len ;
// 定义字符数组,作为装字符数据的容器
char[] cbuf = new char[ 2 ];
// 循环读取
while ((len = fr.read(cbuf))!=‐ 1 ) {
System.out.println(new String(cbuf));
        }
// 关闭资源
fr.close();
    }
}
输出结果:
黑马
程序
员序
public class FISRead {
public static void main(String[] args) throws IOException {
// 使用文件名称创建流对象
FileReader fr = new FileReader("read.txt");
// 定义变量,保存有效字符个数
int len ;
// 定义字符数组,作为装字符数据的容器
char[] cbuf = new char[ 2 ];
// 循环读取
while ((len = fr.read(cbuf))!=‐ 1 ) {
System.out.println(new String(cbuf, 0 ,len));
        }
// 关闭资源
fr.close();
    }
}
输出结果:
黑马
程序
员

The java.io.Writer abstract class is a super class that represents all classes used to write character streams, and writes specified character information to the destination. It defines the
basic common functional methods of byte output streams.

void write(int c) 写入单个字符。
void write(char[] cbuf)写入字符数组。
abstract  void write(char[] cbuf, int off, int len)写入字符数组的某一部分,off数组的开始索引,len
写的字符个数。
void write(String str)写入字符串。
void write(String str, int off, int len) 写入字符串的某一部分,off字符串的开始索引,len写的字符个
数。
void flush()刷新该流的缓冲。
void close() 关闭此流,但要先刷新它。

3.4 FileWriter class

java.io.FileWriter类是写出字符到文件的便利类。构造时使用系统默认的字符编码和默认字节缓冲区。

Construction method

FileWriter(File file): 创建一个新的 FileWriter,给定要读取的File对象。
FileWriter(String fileName): 创建一个新的 FileWriter,给定要读取的文件的名称。

When you create a stream object, you must pass in a file path, similar to FileOutputStream.

构造举例,代码如下:

Basically write data

Write out characters: write(int b) method, you can write out one character data each time, the code usage demonstration:

public class FileWriterConstructor {
public static void main(String[] args) throws IOException {
// 使用File对象创建流对象
File file = new File("a.txt");
FileWriter fw = new FileWriter(file);
// 使用文件名称创建流对象
FileWriter fw = new FileWriter("b.txt");
    }
}
public class FWWrite {
public static void main(String[] args) throws IOException {
// 使用文件名称创建流对象
FileWriter fw = new FileWriter("fw.txt");    
// 写出数据
fw.write( 97 ); // 写出第 1 个字符
fw.write('b'); // 写出第 2 个字符
fw.write('C'); // 写出第 3 个字符
fw.write( 30000 ); // 写出第 4 个字符,中文编码表中 30000 对应一个汉字。

/*

Tips:

1. 虽然参数为int类型四个字节,但是只会保留一个字符的信息写出。
2. 未调用close方法,数据只是保存到了缓冲区,并未写出到文件中。

Close and refresh

Because of the built-in buffer, if the output stream is not closed, characters cannot be written to the file. But the closed stream object cannot continue to write data

of. If we want to write data, but also want to continue to use the stream, we need the flush method.

flush :刷新缓冲区,流对象可以继续使用。
close:先刷新缓冲区,然后通知系统释放资源。流对象不可以再被使用了。

Code demo:

小贴士:即便是flush方法写出了数据,操作的最后还是要调用close方法,释放系统资源。

Write out other data

1. 写出字符数组 :write(char[] cbuf) 和 write(char[] cbuf, int off, int len) ,每次可以写出字符数
组中的数据,用法类似FileOutputStream,代码使用演示:

/*

【注意】关闭资源时,与FileOutputStream不同。
如果不关闭,数据只是保存到缓冲区,并未保存到文件。
*/
// fw.close();
    }
}
输出结果:
abC田
public class FWWrite {
public static void main(String[] args) throws IOException {
// 使用文件名称创建流对象
FileWriter fw = new FileWriter("fw.txt");
// 写出数据,通过flush
fw.write('刷'); // 写出第 1 个字符
fw.flush();
fw.write('新'); // 继续写出第 2 个字符,写出成功
fw.flush();
// 写出数据,通过close
fw.write('关'); // 写出第 1 个字符
fw.close();
fw.write('闭'); // 继续写出第 2 个字符,【报错】java.io.IOException: Stream closed
fw.close();
    }
}
public class FWWrite {
public static void main(String[] args) throws IOException {
// 使用文件名称创建流对象
  1. Write the string: write(String str) and write(String str, int off, int len), each time you can write the
    data in the string , it is more convenient, the code demonstration:

  2. Continue writing and line feed: The operation is similar to FileOutputStream.

FileWriter fw = new FileWriter("fw.txt");
// convert a string to a byte array
char[] chars = "dark horse programmer".toCharArray();

// Write out the character array
fw.write(chars); // Dark horse programmer

// Write out 2 bytes starting from index 2. Index 2 is'program', two bytes, that is,'program'.
fw.write(b, 2, 2 ); // program

// Close the resource
fos.close();
}
}

public class FWWrite { public static void main(String[] args) throws IOException { // Use the file name to create a stream object FileWriter fw = new FileWriter("fw.txt"); // String msg = "Dark horse programmer" ;




// Write out the character array
fw.write(msg); //Dark horse programmer

// Write out 2 bytes starting from index 2. Index 2 is'program', two bytes, that is,'program'.
fw.write(msg, 2, 2 ); // program

// Close the resource
fos.close();
}
}

public class FWWrite { public static void main(String[] args) throws IOException { // Use the file name to create a stream object, you can continue to write data FileWriter fw = new FileWriter("fw.txt", true); // Write out characters String fw.write("dark horse"); // write out a newline fw.write("\r\n"); // write out a string fw.write("programmer"); // close the resource fw.close (); }











}

Tips: The character stream can only operate on text files, but not on non-text files such as pictures and videos.

When we simply read or write text files, we use character streams. Other situations use byte streams.

Chapter 4 IO exception handling

JDK7 pre-processing

In the previous introductory exercises, we have been throwing exceptions, but in actual development, we can’t handle it like this. It is recommended to use try...catch...finally code
blocks to handle the exception part and demonstrate the code usage:

JDK7 processing (expand knowledge points to understand the content)

You can also use the JDK7 optimized try-with-resource statement, which ensures that each resource is closed at the end of the statement. The so-called resource
(resource) refers to the object that must be closed after the program is completed.

format:

Output result:

Dark horse

programmer

public class HandleException1 {
public static void main(String[] args) {
// 声明变量
FileWriter fw = null;
try {
//创建流对象
fw = new FileWriter("fw.txt");
// 写出数据
fw.write("黑马程序员"); //黑马程序员
        } catch (IOException e) {
e.printStackTrace();
        } finally {
try {
if (fw != null) {
fw.close();
                }
            } catch (IOException e) {
e.printStackTrace();
            }
        }
    }
}
try (创建流对象语句,如果多个,使用';'隔开) {
// 读写数据
} catch (IOException e) {
e.printStackTrace();
}

Code demo:

Improvements in JDK9 (expand knowledge points to understand the content)

The improvement of try-with-resource in JDK9 provides more concise support for the way of introducing objects. The introduced object can also be closed automatically
without manual close. Let's take a look at the format.

Format before improvement:

Improved format:

After improvement, the code usage demonstration:

public class HandleException2 {
public static void main(String[] args) {
// 创建流对象
try ( FileWriter fw = new FileWriter("fw.txt"); ) {
// 写出数据
fw.write("黑马程序员"); //黑马程序员
        } catch (IOException e) {
e.printStackTrace();
        }
    }
}
// 被final修饰的对象
final Resource resource1 = new Resource("resource1");
// 普通对象
Resource resource2 = new Resource("resource2");
// 引入方式:创建新的变量保存
try (Resource r1 = resource1;
Resource r2 = resource2) {
// 使用对象
}
// 被final修饰的对象
final Resource resource1 = new Resource("resource1");
// 普通对象
Resource resource2 = new Resource("resource2");
// 引入方式:直接引入
try (resource1; resource2) {
// 使用对象
}
public class TryDemo {
public static void main(String[] args) throws IOException {
// 创建流对象
final FileReader fr = new FileReader("in.txt");
FileWriter fw = new FileWriter("out.txt");
// 引入到try中

Chapter 5 Attribute Set

5.1 Overview

java.util.Properties inherits from Hashtable to represent a persistent set of properties. It uses a key-value structure to store data, and each key and its
corresponding value is a string. This class is also used by many Java classes. For example, when obtaining system properties, the System.getProperties method returns
a Properties object.

5.2 Properties class

Construction method

public Properties() :创建一个空的属性列表。

Basic storage method

public Object setProperty(String key, String value) : 保存一对属性。
public String getProperty(String key) :使用此属性列表中指定的键搜索属性值。
public Set<String> stringPropertyNames() :所有键的名称的集合。
try (fr; fw) {
// 定义变量
int b;
// 读取数据
while ((b = fr.read())!=‐ 1 ) {
// 写出数据
fw.write(b);
}
        } catch (IOException e) {
e.printStackTrace();
        }
    }
}
public class ProDemo {
public static void main(String[] args) throws FileNotFoundException {
// 创建属性集对象
Properties properties = new Properties();
// 添加键值对元素
properties.setProperty("filename", "a.txt");
properties.setProperty("length", "209385038");
properties.setProperty("location", "D:\\a.txt");
// 打印属性集对象
System.out.println(properties);
// 通过键,获取属性值
System.out.println(properties.getProperty("filename"));
System.out.println(properties.getProperty("length"));
System.out.println(properties.getProperty("location"));

// Traverse the attribute set and get the set of all keys

Stream-related methods

public void load(InputStream inStream): 从字节输入流中读取键值对。

The byte input stream is used in the parameter. Through the stream object, it can be associated with a file, so that the data in the text can be loaded. Text data format:

Loading code demo:

Tips: The data in the text must be in the form of key-value pairs, and can be separated by spaces, equal signs, colons and other symbols.

Set<String> strings = properties.stringPropertyNames();
// 打印键值对
for (String key : strings ) {
System.out.println(key+" ‐‐ "+properties.getProperty(key));
        }
    }
}
输出结果:
{filename=a.txt, length= 209385038 , location=D:\a.txt}
a.txt
209385038
D:\a.txt
filename ‐‐ a.txt
length ‐‐  
location ‐‐ D:\a.txt
filename=a.txt
length=
location=D:\a.txt
public class ProDemo2 {
public static void main(String[] args) throws FileNotFoundException {
// 创建属性集对象
Properties pro = new Properties();
// 加载文本中信息到属性集
pro.load(new FileInputStream("read.txt"));
// 遍历集合并打印
Set<String> strings = pro.stringPropertyNames();
for (String key : strings ) {
System.out.println(key+" ‐‐ "+pro.getProperty(key));
        }
     }
}
输出结果:
filename ‐‐ a.txt
length ‐‐  
location ‐‐ D:\a.txt

Guess you like

Origin blog.csdn.net/weixin_43419256/article/details/108230745