character stream
Writer (output stream)
Construction method
FileWriter(File file) Write a FileWriter to File, use the platform's default charset
FileWriter(FileDescriptor fd) to construct a file descriptor given by FileWriter, and use the platform's default charset .
FileWriter(File file, boolean append) Constructs a File given the FileWriter to write, and constructs a boolean value indicating whether to append the written data using the platform's default charset.
FileWriter(File file, Charset charset) Constructs a FileWriter given File writing and charset.
FileWriter(File file, Charset charset, boolean append) Constructs a FileWriter given File to write, charset and a boolean indicating whether to append the written data.
FileWriter(String fileName) Constructs a FileWriter given the filename, using the platform's default charset
FileWriter(String fileName, boolean append) Constructs a FileWriter using the platform's default charset Given a filename and a boolean indicating whether to append writes The data.
FileWriter(String fileName, Charset charset) Constructs a FileWriter given the filename and charset.
FileWriter(String fileName, Charset charset, boolean append) Constructs a FileWriter given a filename, charset and a boolean value indicating whether to append the written data.
Implementation
package work.february.two.daily;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
/**
* @Author: 小浪
* @Description:
* @Date Created in 2021-02-02 19:46
* @Modified By:
*/
public class Demo7 {
public static void main(String[] args) throws IOException {
File file =new File("d://c.txt");
file.createNewFile();
Writer writer=new FileWriter("d://c.txt");
//写入数据
writer.write(65);
writer.write('c');
char [] arr={
'a','b','d','e'};
writer.write(arr);
writer.write(arr,1,2);
writer.write("我学会了!");
//追加
writer.append("爱了");
writer.close();
}
}
Refresh the cache (no refresh will not go to the file)
package work.february.two.daily;
import java.io.FileWriter;
import java.io.IOException;
/**
* @Author: 小浪
* @Description:
* @Date Created in 2021-02-02 20:26
* @Modified By:
*/
public class Demo10 {
public static void main(String[] args) throws IOException {
FileWriter fileWriter =new FileWriter("d://c.txt");
fileWriter.append("锄禾日当午").append("汗滴禾下土");
//刷新 缓存
fileWriter.flush();
fileWriter.close();
}
}
reader (input stream)
FileReader(File file) uses the platform FileReader, creating a new FileReader when the File is read.
FileReader(FileDescriptor fd) Creates a new FileReader with the platform default charset, given the FileDescriptor to read.
FileReader(File file, Charset charset) Creates a new FileReader, given the File read and charset.
FileReader(String fileName) Creates a new FileReader using the platform default charset, given the name of the file to read.
FileReader(String fileName, Charset charset) Creates a new FileReader given the name of the file to read and a FileReader.
Implementation:
package work.february.two.daily;
import java.io.FileReader;
import java.io.IOException;
/**
* @Author: 小浪
* @Description:
* @Date Created in 2021-02-02 20:02
* @Modified By:
*/
public class Demo8 {
public static void main(String[] args) throws IOException {
//字符输入流 一个一个读
FileReader fileReader=new FileReader("d:\\c.txt");
while (true){
int c= fileReader.read();
if (c == -1){
break;
} else{
System.out.println((char) c);
}
}
fileReader.close();
}
}
package work.february.two.daily;
import java.io.FileReader;
import java.io.IOException;
/**
* @Author: 小浪
* @Description:
* @Date Created in 2021-02-02 20:06
* @Modified By:
*/
public class Demo9 {
public static void main(String[] args) throws IOException {
FileReader fileReader =new FileReader("d.txt");
char [] chars=new char[100];
int len =fileReader.read();
String text = new String(chars,0,len);
System.out.println(text);
file
fileReader.close();
}
}
Bullshit time:
If the guest officer thinks it is suitable to eat, can you give a free like! Thank you! Slow down guest officer! It is recommended to pack and collect, and come back next time. The shop's second QQ: 309021573, welcome to harass!