当前位置:网站首页>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();
}
}
边栏推荐
- Inno setup production and installation package
- Visit Google homepage to display this page, which cannot be displayed
- Advanced API (local simulation download file)
- C WinForm framework
- 树莓派更新工具链
- VMWare网络模式-桥接,Host-Only,NAT网络
- 7.2 brush two questions
- Win 2008 R2 crashed at the final installation stage
- 1. E-commerce tool cefsharp autojs MySQL Alibaba cloud react C RPA automated script, open source log
- Store WordPress media content on 4everland to complete decentralized storage
猜你喜欢
![[set theory] equivalence classes (concept of equivalence classes | examples of equivalence classes | properties of equivalence classes | quotient sets | examples of quotient sets)*](/img/1f/f579110a408c5b5a094733be57ed90.jpg)
[set theory] equivalence classes (concept of equivalence classes | examples of equivalence classes | properties of equivalence classes | quotient sets | examples of quotient sets)*

691. 立方体IV

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

专题 | 同步 异步
![PdfWriter. GetInstance throws system Nullreferenceexception [en] pdfwriter GetInstance throws System. NullRef](/img/65/1f28071fc15e76abb37f1b128e1d90.jpg)
PdfWriter. GetInstance throws system Nullreferenceexception [en] pdfwriter GetInstance throws System. NullRef

Interview questions about producers and consumers (important)

Final, override, polymorphism, abstraction, interface

Pat grade a real problem 1166

Win 10 find the port and close the port

Recursion, Fibonacci sequence
随机推荐
Book recommendation~
Operation and maintenance technical support personnel have hardware maintenance experience in Hong Kong
php安装composer
萬卷書 - 價值投資者指南 [The Education of a Value Investor]
Thoughts on project development
[solved] unknown error 1146
PHP install the spool extension
sharepoint 2007 versions
691. Cube IV
Resthighlevelclient gets the mapping of an index
Win 2008 R2 crashed at the final installation stage
Selenium key knowledge explanation
MySQL transaction rollback, error points record
Docker builds MySQL: the specified path of version 5.7 cannot be mounted.
[solved] sqlexception: invalid value for getint() - 'Tian Peng‘
《指环王:力量之戒》新剧照 力量之戒铸造者亮相
Jeecg request URL signature
【最详细】最新最全Redis面试大全(50道)
Interview questions about producers and consumers (important)
Common analysis with criteria method