字符流
字符输出流
Writer是字符输出流,该类是一个抽象类,所以要用子类FileWriter类来操作文件
package IO;
import java.io.FileWriter;
import java.io.IOException;
public class WriterDemo {
public static void main(String[] args) throws IOException{
write();
}
public static void write()throws IOException {
FileWriter fw = new FileWriter("D:/hello.txt",true);
fw.write("几把飞");
fw.close();
}
字符输入流
Reader是一个字符输入流,但是它是一个抽象类,所以必须要由子类FileReader来实例化
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
public class ReadDemo {
public static void main(String[] args) throws IOException{
Read();
}
public static void Read() throws IOException {
Reader r = new FileReader("D:/hello.txt");
char[] buf =new char[1024];
int len = 0;
while ((len = r.read(buf)) != -1){
String s = new String(buf,0,len);
System.out.println(s);
}
r.close();
}
}