Exercise 1: Byte output stream writes byte data
- Use the byte output stream to write one byte at a time to output the character'a' to the a.txt file on Disk D.
Steps:
- Create a byte output stream FileOutputStream object and specify the file path.
- Call the write(int byte) method of the byte output stream to write out the data
Code:
public class Test01_01 {
public static void main(String[] args) throws IOException {
// 1.创建字节输出流FileOutputStream对象并指定文件路径。
FileOutputStream fos = new FileOutputStream("d:/a.txt");
// 2.调用字节输出流的write(int byte)方法写出数据
fos.write(97);
// 3.关闭流
fos.close();
}
}
Exercise 2: Byte output stream writes byte array data
- Use the byte output stream to write one byte array at a time to the b.txt file on Disk D to output content: "i love java".
Steps:
- Create a byte output stream FileOutputStream object and specify the file path.
- Call the write(byte[] buf) method of the byte output stream to write out the data.
Code:
public class Test01_02 {
public static void main(String[] args) throws IOException {
// 1.创建字节输出流FileOutputStream对象并指定文件路径。
FileOutputStream fos = new FileOutputStream("d:/b.txt");
// 2.调用字节输出流的write(byte[] buf)方法写出数据。
byte[] buf = "i love java".getBytes();
fos.write(buf);
// 3.关闭资源
fos.close();
}
}
Exercise 3: File continuation and newline output
- In Disk D, there is a c.txt file with the content: HelloWorld,
on the basis of the original content of the c.txt file, add five sentences I love java, and implement one sentence by one operation (note: the original text cannot be overwritten).
Use the byte output stream object to output 5 sentences to the c.txt file under C drive: "i love java"
Steps:
- Use the construction method of two parameters to create a byte output stream object, the first parameter specifies the file path, and the second parameter specifies true
- Call the write() method of the byte output stream to write data, and add a newline character after each line: "\r\n"
Code:
public class Test01_03 {
public static void main(String[] args) throws IOException{
// 1.创建字节输出流FileOutputStream对象并指定文件路径,并追加方式
FileOutputStream fos = new FileOutputStream("c:/c.txt",true);
// 2.调用字节输出流的write方法写出数据
// 2.1 要输出的字符串
String content = "i love java \r\n";
for (int i = 0; i< 5; i++) {
fos.write(content.getBytes());
}
// 3.关闭流
fos.close();
}
}
Exercise 4: The byte input stream reads one byte of data at a time
- Use the byte input stream to read the contents of the D disk file a.txt. The contents of the file are determined to be pure ASCII characters
. Use circular reading to read one byte at a time until the end of the file is read. Output the read bytes to the console
Steps:
- Create a byte input stream object to specify the file path.
- Call the read(byte b) method to read the data in the file cyclically
- End reading until it reaches -1
Code:
public class Test01_04 {
public static void main(String[] args) throws IOException{
// 创建字节输入流对象并关联文件
FileInputStream fis = new FileInputStream("d:/a.txt");
// 定义变量接收读取的字节
int len = -1;
// 循环从流中读取数据
while((len = fis.read()) != -1) {
System.out.print((char)len);
}
// 关闭流
fis.close();
}
}
Exercise 5: Byte input stream reads one byte array data at a time
- Use the byte input stream to read the contents of the D disk file b.txt. The contents of the file are determined to be pure ASCII characters. Use circular reading to read one byte array at a time until the end of the file is read. The byte array is converted into a string and output to the console.
Steps:
- Create a byte input stream object to specify the file path.
- Define a byte number array to store the number of bytes read
- Call the read(byte[] buf) method to pass in the byte array, and read the data in the file in a loop
- End reading until it reaches -1
Code:
public class Test01_05 {
public static void main(String[] args) throws IOException{
// 创建字节输入流对象并关联文件
FileInputStream fis = new FileInputStream("d:/b.txt");
// 定义字节数组存放读取的字节数
byte[] buffer = new byte[1024];
// 定义变量接收读取的字节
int len = -1;
// 循环从流中读取数据
while((len = fis.read(buffer)) != -1) {
System.out.print(new String(buffer,0,len));
}
// 关闭流
fis.close();
}
}
Exercise 6: Copying files with byte stream
- Use the byte stream to copy the a.png picture under the E drive to the D drive (the file name is the same)
Steps:
- Create the associated file path of the byte input stream object: a.png in Disk E
- Create the associated file path of the byte output stream object: a.png under Disk D
- Use a loop to continuously read a byte from the byte input stream, and use the output stream to write a byte every time a byte is read.
- Close the stream, release resources
Code:
public class Test01_06 {
public static void main(String[] args) throws IOException {
// 创建字节输入流对象并关联文件
FileInputStream fis = new FileInputStream("e:/a.png");
// 创建字节输出流对象并关联文件
FileOutputStream fos = new FileOutputStream("d:/a.png");
// 定义变量接收读取的字节数
int len = -1;
// 循环读取图片数据
while((len = fis.read()) != -1) {
// 每读取一个字节的数据就写出到目标文件中
fos.write(len);
}
// 关闭流
fis.close();
fos.close();
}
}
Exercise 7: Combine IO object Properties and set properties file
- I have a text file score.txt. I know that the data is in the form of key-value pairs, but I don't know what the content is. Please write a program to determine whether there is a key like "lisi", if there is, change it to "100"
The content of the score.txt file is as follows:
zhangsan = 90
lisi = 80
wangwu = 85
Steps:
- Create an empty Properties collection
- Read data into the collection
- Traverse the collection and get each key
- Determine whether the current key is "lisi", if so, set the value of "lisi" to 100
- Restore all the information in the collection to the file
Prompt information:
To store the information in the collection in a file, you can use the following methods.
java.util class Properties
store(OutputStream out, String comments)
以适合使用load(InputStream)方法加载
到Properties表中的格式,将此Properties
表中的属性列表(键和元素对)写入输出流。
out - 输出流。 comments - 属性列表的描述。
Code :
public class Test02_06 {
public static void main(String[] args) throws IOException {
//1:创建一个空的集合
Properties prop = new Properties();
//2:读取数据到集合中
prop.load(new FileInputStream("score.txt"));
//3:遍历集合,获取到每一个key
Set<String> keys = prop.stringPropertyNames();
//获取到每一个key
for (String key : keys) {
//4:判断当前的key 是否为 "lisi"
if ("lisi".equals(key)) {
//把"lisi"的值设置为100
prop.setProperty(key, "100");
}
}
//把集合中所有的信息,重新存储到文件中
prop.store(new FileOutputStream("score.txt"), "haha");
}
}
Exercise 8: Byte input stream usage
- There is a text file test.txt (the content is composed of numbers and letters) under the D drive to
define a method to count the number of occurrences of the character'a' in the test.txt file.
For example, if the a character appears 10 times in the file, the method is called and passed into a, the internal output of the method: a appears 10 times
Steps:
- Create a byte input stream object and read a byte from the file in a loop
- Define an integer variable to count the number of occurrences of characters.
- The read byte conversion character is compared with the passed character, and the count is increased by one if they are the same.
- Output the result.
Code:
import java.io.FileInputStream;
import java.io.IOException;
public class Test02_01{
public static void main(String[] args) throws IOException {
// 调用方法
calcuteCount('a');
}
/*
* 统计字符在文件中出现的次数
*/
public static void calcuteCount(char ch) throws IOException {
// 创建字节输入流
final FileInputStream fis = new FileInputStream("d:/test.txt");
try (fis) {
// 定义一个计数变量,统计字符出现的次数
int count = 0;
// 循环读取数据
int len = -1;
while ((len = fis.read()) != -1) {
// 判断字符c和外界传入的字符是否相同
if (len == ch) {
count++;
}
}
System.out.println(ch + "出现了" + count + "次");
} catch (IOExceptione) {
e.printStackTrace();
}
}
}