当前位置:网站首页>IO stream system and FileReader, filewriter
IO stream system and FileReader, filewriter
2022-07-03 07:21:00 【four thousand three hundred and forty-three】


package IOStream;
import org.junit.Test;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class FileReadWriter {
//
@Test
public void testFileReader() {
// fr It needs to be declared in advance
FileReader fr = null;
// 1. Instantiation File Class object , Indicates the file to be operated on
try {
File file1 = new File("hello.txt");
// 2. Provide specific flow :( Find the pipe )
fr = new FileReader(file1);
// 3. Reading in data
// read() Returns a character read in , use int When accepting ASCII code , If it reaches the end, it returns -1
// The way 1:
// int data = fr.read();
// while(data != -1){
// System.out.print((char) data);
// data = fr.read();
// }
// The way 2:
int data ;
while((data = fr.read())!= -1){
System.out.print((char)data);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(fr!=null){
fr.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
// 4. Stream closing operation
}
}
read() Overload method of :read(char cbuffer[]) Returns the number of characters read
// Yes read() An upgrade of the operation method : Use read Overloading methods
// read(char[]) Returns the number of characters read in each time , such as helloworld123!, Returns the 5 5 4 -1
@Test
public void testFileReader2() {
// 1.File Class instantiation
FileReader fr = null;
try {
File file = new File("hello.txt");
// 2. Instantiation of a stream
fr = new FileReader(file);
// 3. Read in operation
// read(char[]) Returns the number of characters read in each time , such as helloworld123!, Returns the 5 5 4 -1
char[] cbuffer = new char[5];
int len ;
while((len = fr.read(cbuffer))!=-1){
for(int i = 0 ; i < len; i++){
System.out.print(cbuffer[i]);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
// 4. Resources to shut down
if (fr != null) {
fr.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}FileWriter Explanation :
package IOStream;
/*
Write data from memory to hard disk
explain :
1. Output operation File The object may not exist , This file will be created automatically
If exist , In the constructor append: choice true To continue adding ,false To overwrite the original file
FileWriter(file,true/false)
2.
*/
import org.junit.Test;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class FileWriterTest {
@Test
public void testFileWriter() throws IOException {
// 1. Provide File object , Indicates the file written out
File file1 = new File("hello1.txt");
// 2. Provide FileWriter object , For writing data
FileWriter fw = new FileWriter(file1,false);
// 3. Written operations
fw.write("i have a dream!\n");
fw.write("you need to have a dream!");
// 4. The closure of streaming resources
fw.close();
}
}
边栏推荐
- Interview questions about producers and consumers (important)
- [untitled]
- Thoughts on project development
- 【已解决】SQLException: Invalid value for getInt() - ‘田鹏‘
- Docker builds MySQL: the specified path of version 5.7 cannot be mounted.
- JS date comparison
- Pits encountered in the use of El checkbox group
- 4everland: the Web3 Developer Center on IPFs has deployed more than 30000 dapps!
- Common problems in io streams
- JMeter test result output
猜你喜欢

Pat grade a real problem 1166

dataworks自定义函数开发环境搭建

C代码生产YUV420 planar格式文件

Flask Foundation

Dora (discover offer request recognition) process of obtaining IP address

【已解决】Unknown error 1146

Distributed transactions

专题 | 同步 异步

JMeter JSON extractor extracts two parameters at the same time

Summary of abnormal mechanism of interview
随机推荐
Win 10 find the port and close the port
Shim and Polyfill in [concept collection]
The education of a value investor
【已解决】Unknown error 1146
Jeecg data button permission settings
[solved] win10 cannot find a solution to the local group policy editor
3311. 最长算术
7.2刷题两个
Basic knowledge about SQL database
POI excel percentage
JS monitors empty objects and empty references
Advanced API (use of file class)
10 000 volumes - Guide de l'investisseur en valeur [l'éducation d'un investisseur en valeur]
PAT甲级真题1166
[untitled]
[day15] introduce the features, advantages and disadvantages of promise, and how to implement it internally. Implement promise by hand
TypeScript let与var的区别
TCP cumulative acknowledgement and window value update
How to specify the execution order for multiple global exception handling classes
php artisan