当前位置:网站首页>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();
}
}
边栏推荐
- Use the jvisualvm tool ----- tocmat to start JMX monitoring
- II. D3.js draw a simple figure -- circle
- The underlying mechanism of advertising on websites
- [day15] introduce the features, advantages and disadvantages of promise, and how to implement it internally. Implement promise by hand
- 专题 | 同步 异步
- LeetCode
- Jeecg data button permission settings
- 【已解决】SQLException: Invalid value for getInt() - ‘田鹏‘
- Advanced APL (realize group chat room)
- Arduino 软串口通信 的几点体会
猜你喜欢
Interfaces and related concepts
New stills of Lord of the rings: the ring of strength: the caster of the ring of strength appears
Interview questions about producers and consumers (important)
VMWare网络模式-桥接,Host-Only,NAT网络
C代码生产YUV420 planar格式文件
Docker builds MySQL: the specified path of version 5.7 cannot be mounted.
Store WordPress media content on 4everland to complete decentralized storage
Deep learning parameter initialization (I) Xavier initialization with code
4279. 笛卡尔树
[solved] unknown error 1146
随机推荐
Laravel框架 踩坑(一)
Interfaces and related concepts
2. E-commerce tool cefsharp autojs MySQL Alibaba cloud react C RPA automated script, open source log
Distributed lock
Setting up the development environment of dataworks custom function
萬卷書 - 價值投資者指南 [The Education of a Value Investor]
Store WordPress media content on 4everland to complete decentralized storage
VMware virtual machine installation
JUC forkjoinpool branch merge framework - work theft
Warehouse database fields_ Summary of SQL problems in kingbase8 migration of Jincang database
深度学习参数初始化(一)Xavier初始化 含代码
TypeScript let與var的區別
Basic components and intermediate components
PdfWriter. GetInstance throws system Nullreferenceexception [en] pdfwriter GetInstance throws System. NullRef
Common analysis with criteria method
Advanced API (byte stream & buffer stream)
CentOS php7.3 installing redis extensions
Flask Foundation
docker建立mysql:5.7版本指定路径挂载不上。
20220319