当前位置:网站首页>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();
}
}
边栏推荐
- Flask Foundation
- php artisan
- 《指環王:力量之戒》新劇照 力量之戒鑄造者亮相
- 带你全流程,全方位的了解属于测试的软件事故
- Win 10 find the port and close the port
- twenty million two hundred and twenty thousand three hundred and nineteen
- 7.2 brush two questions
- 4EVERLAND:IPFS 上的 Web3 开发者中心,部署了超过 30,000 个 Dapp!
- Advanced API (use of file class)
- Laravel Web框架
猜你喜欢

Homology policy / cross domain and cross domain solutions /web security attacks CSRF and XSS

Docker builds MySQL: the specified path of version 5.7 cannot be mounted.

New stills of Lord of the rings: the ring of strength: the caster of the ring of strength appears

高并发内存池

Pat grade a real problem 1166

深度学习参数初始化(一)Xavier初始化 含代码

Take you through the whole process and comprehensively understand the software accidents that belong to testing
![[vscode - vehicle plug-in reports an error] cannot find module 'xxx' or its corresponding type declarations Vetur(2307)](/img/7f/3d6b6ea5319f7165e07baf0a78d318.jpg)
[vscode - vehicle plug-in reports an error] cannot find module 'xxx' or its corresponding type declarations Vetur(2307)

691. 立方体IV

Use the jvisualvm tool ----- tocmat to start JMX monitoring
随机推荐
【最詳細】最新最全Redis面試大全(50道)
带你全流程,全方位的了解属于测试的软件事故
Advanced API (multithreading)
1. E-commerce tool cefsharp autojs MySQL Alibaba cloud react C RPA automated script, open source log
【无标题】
Circuit, packet and message exchange
在 4EVERLAND 上存储 WordPress 媒体内容,完成去中心化存储
[HCAI] learning summary OSI model
SharePoint modification usage analysis report is more than 30 days
Take you through the whole process and comprehensively understand the software accidents that belong to testing
LeetCode
php artisan
Margin left: -100% understanding in the Grail layout
《指環王:力量之戒》新劇照 力量之戒鑄造者亮相
深度学习参数初始化(一)Xavier初始化 含代码
Realize the reuse of components with different routing parameters and monitor the changes of routing parameters
[cmake] cmake link SQLite Library
SecureCRT取消Session记录的密码
Talk about floating
SecureCRT password to cancel session recording