当前位置:网站首页>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();
}
}
边栏推荐
猜你喜欢
![[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)*

《指環王:力量之戒》新劇照 力量之戒鑄造者亮相

JS monitors empty objects and empty references

《指环王:力量之戒》新剧照 力量之戒铸造者亮相

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

带你全流程,全方位的了解属于测试的软件事故

Specified interval inversion in the linked list

Setting up the development environment of dataworks custom function

Mise en place d'un environnement de développement de fonctions personnalisées

Sorting, dichotomy
随机推荐
Upgrade CentOS php7.2.24 to php7.3
php artisan
JUC forkjoinpool branch merge framework - work theft
Laravel框架 踩坑(一)
SharePoint modification usage analysis report is more than 30 days
树莓派更新工具链
Dora (discover offer request recognition) process of obtaining IP address
Advanced API (character stream & net for beginners)
Setting up the development environment of dataworks custom function
[cmake] cmake link SQLite Library
Shim and Polyfill in [concept collection]
Inno Setup 制作安装包
Chrome 98 Private Network Access problem w/ disabled web security: Request had no target IP address
[untitled]
《指环王:力量之戒》新剧照 力量之戒铸造者亮相
Common analysis with criteria method
Distributed ID
High concurrency memory pool
Common APIs
Distributed transactions