1.BufferedReader使用
java
package com.yundait.Demo05;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
public class BufferedReaderDemo01 {
public static void main(String[] args) throws Exception {
//1创建文件字符写入缓冲流
FileReader fr = new FileReader("C:\\Users\\Public\\Documents\\ggg1.txt");
BufferedReader br = new BufferedReader(fr);
//读取文件(方式1)
// char[] chars = new char[1024];
// int count;
// while ((count = br.read(chars))!=-1){
// System.out.println(new String(chars,0,count));
// }
//读取文件(方式2)
String s;
while ((s = br.readLine())!=null){
System.out.println(s);
}
//3关闭
br.close();
}
}
2.BufferedWriter使用
java
package com.yundait.Demo05;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class BufferedWriterDemo01 {
public static void main(String[] args) throws Exception {
//1创建文件字符写入缓冲流
FileWriter fw = new FileWriter("C:\\Users\\Public\\Documents\\ggg2.txt");
BufferedWriter bw = new BufferedWriter(fw);
//2写入文件
for (int i=0; i<10; i++){
bw.write("好好学习,天天向上");
bw.newLine();
bw.flush();
}
//关闭通道
bw.close();
System.out.println("执行完毕");
}
}
3.PrintWriter使用
java
package com.yundait.Demo05;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
public class PrintWriterDemo01 {
public static void main(String[] args) throws Exception {
//1.创建字符打印流
PrintWriter pw = new PrintWriter("C:\\Users\\Public\\Documents\\ggg3.txt");
//2打印
pw.println(97);
pw.println(true);
pw.println('张');
pw.println(3.14);
//关闭
pw.close();
System.out.println("执行完毕");
}
}