Java-I/O框架08:BufferedReader、BufferedWriter、PrintWriter使用
视频链接:16.22 PrintWriter的使用_哔哩哔哩_bilibilihttps://www.bilibili.com/video/BV1Tz4y1X7H7?spm_id_from=333.788.videopod.episodes&vd_source=b5775c3a4ea16a5306db9c7c1c1486b5&p=22
1.BufferedReader使用
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使用
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使用
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("执行完毕");}
}