java基础46 IO流技术(输出字符流/缓冲输出字符流)

一、输出字符流

1.1、输出字符流体系

  --------| Writer:输出字符流的基类(抽象类)
  ----------| FileWriter向文件输出数据输出字符流(把程序中的数据写到硬盘中)
  ----------| BufferedWriter:缓冲输出字符流    缓冲输出字符流的作用:提高了FileWiter的写数据效率和拓展了FileWiter的功能。内部提供了一个8192长度的字符数组作为缓冲区而已,拓展了FileWiter。

注意:所有缓冲流都不具备读写文件的能力(比如BufferedWriter他要借助FileWriter的写功能来写文件)

1.2 、FileWriter的步骤

    1.找到目标文件
    2.建立数据输出通道
    3.写出数据(把程序中的数据写到硬盘中)
    4.关闭资源

1.3、 FileWriter要注意的事项

    1.FileWrite内部是维护了一个1024个字符的数组,使用FileWriter写数据的时候,会先写入到它内部维护的字符数组中,如果需要把数据真正的写到硬盘上,需要调用flush或者close方法或者填满内部的字符数组。
    2.使用FileWrite的时候,如果目标文件不存在,那么会自动创建目标文件。

1.4、实例

 1 package com.dhb.file;
 2 
 3 import java.io.File;
 4 import java.io.FileWriter;
 5 import java.io.IOException;
 6 
 7 /**
 8  * @author DSHORE / 2018-7-6
 9  *
10  */
11 public class Demo15 {
12     public static void main(String[] args) throws IOException {
13         writeTest();
14     }
15     //输出字符流
16     public static void writeTest() throws IOException{
17         //找到目标文件
18         File file=new File("F:\a.txt");
19         //建立数据输出通道
20         FileWriter fw=new FileWriter(file,true);//加个true,表示在原文件的原数据后面追加当前数据;不加true,则原来的数据全部被替换成现在追加的数据
21         //准备数据,把数据写出(即:写数据;把程序中的数据写到硬盘中)
22         String data="
今天天气非常好,如果明天也好,我们就一起去锻炼吧!!!";// “
”表示换行
23         fw.write(data);//字符流具备解码功能.
24         //刷新字符流
25         //fw.flush();
26         //关闭资源
27         fw.close();//其实close()里面有个flush()方法
28     }
29 }

运行结果图(运行两次的结果

二、缓冲输出字符流

2.1、BufferedWriter注意事项

    1.缓冲输出字符流的作用:提高了FileWiter的写数据效率和拓展了FileWiter的功能。内部提供了一个8192长度的字符数组作为缓冲区而已,拓展了FileWiter。
    2.凡是缓冲流都不具备读写文件的能力

2.2、实例

 1 package com.dhb.file;
 2 
 3 import java.io.BufferedWriter;
 4 import java.io.File;
 5 import java.io.FileWriter;
 6 import java.io.IOException;
 7 
 8 /**
 9  * @author DSHORE / 2018-7-11
10  *
11  */
12 public class Demo17 {
13 
14     public static void main(String[] args) throws IOException {
15         //找到目标文件
16         File file=new File("F:\a.txt");
17         //建立数据输出通道
18         FileWriter fw=new FileWriter(file);
19         BufferedWriter bw=new BufferedWriter(fw);
20         //bw.newLine();//换行
21         bw.write("
");//换行
22         //写出数据
23         bw.write("你若安好,便是晴天!");
24         //关闭资源
25         bw.close();
26     }
27 }

结果图:

附录1

 1 package com.dhb.file;
 2 
 3 import java.io.BufferedInputStream;
 4 import java.io.BufferedOutputStream;
 5 import java.io.File;
 6 import java.io.FileInputStream;
 7 import java.io.FileNotFoundException;
 8 import java.io.FileOutputStream;
 9 import java.io.IOException;
10 
11 /**
12  * @author DSHORE / 2018-7-11
13  *
14  */
15 /*
16  * 需求:将源目录拷贝到指定目录中
17  * */
18 //拷贝文件
19 public class DirCopy {
20                         //源目录            ,         目标文件
21     public void copy(File sourceDir,File targetFile){
22         //根据源目录名和目标目录构建一个新的目录对象
23                       //这个是磁盘名(目标文件),源文件名
24         targetFile = new File(targetFile,sourceDir.getName());
25         //判断该目录是否存在,若不存在则创建
26         if(!targetFile.exists()){ //exists:存在
27             boolean b = targetFile.mkdirs(); //mkdirs:建立一个新的子目录
28             if(b)System.out.println("创建目录:"+targetFile.getAbsolutePath());//输出已创建目录(文件夹)的绝对路径
29         }
30         //获取源目录中的所有子文件(或子目录)
31         File[] files = sourceDir.listFiles();
32         if(files != null){
33             for (int i = 0; i < files.length; i++) {
34                 //判断当前File对象是文件还是目录
35                 if(files[i].isDirectory()){//判断是文件
36                     //如果是目录,则递归调用
37                     copy(files[i],targetFile);//重新调用copy()方法,在遍历目录里面的文件
38                 }else{
39                     //如果是标准文件则进行文件拷贝
40                     copyFile(files[i],targetFile);
41                 }
42             }
43         }
44     }
45     //实现标准文件的拷贝       原文件    目标文件
46     private void copyFile(File source, File target) {
47         //根据要拷贝的文件名和目录构建新的文件
48         target = new File(target, source.getName());
49         
50         BufferedInputStream bis = null;
51         BufferedOutputStream bos = null;
52         try {
53             //获取源文件的输入流
54             bis = new BufferedInputStream(new FileInputStream(source));//作用于:读
55             //获取目标文件的输出流
56             bos = new BufferedOutputStream(new FileOutputStream(target));//作用于:写
57             byte[] b = new byte[1024];
58             int length=0;
59             System.out.println("开始拷贝:"+source.getAbsolutePath()+"---------->"+target.getAbsolutePath());
60             while((length = bis.read(b)) != -1){//
61                 bos.write(b, 0, length);//
62                 bos.flush();
63             }
64             System.out.println("拷贝完成:"+source.getAbsolutePath()+"---------->"+target.getAbsolutePath());
65         } catch (FileNotFoundException e) {
66             e.printStackTrace();
67         } catch (IOException e) {
68             e.printStackTrace();
69         }finally {
70             try {
71                 bos.close();
72                 bis.close();
73             } catch (IOException e) {
74                 e.printStackTrace();
75             }
76         }    
77     }
78 
79     public static void main(String[] args) {
80         //                     要拷贝的文件(原文件)            ,要拷贝到那个盘符下(目标文件)
81         new DirCopy().copy(new File("F:\MyJavaCode"),new File("E:\"));
82     }
83 }

运行结果图:

      

附录2

 1 package com.dhb.file;
 2 
 3 import java.io.BufferedReader;
 4 import java.io.BufferedWriter;
 5 import java.io.File;
 6 import java.io.FileReader;
 7 import java.io.FileWriter;
 8 import java.io.IOException;
 9 import java.util.Scanner;
10 
11 /**
12  * @author DSHORE / 2018-7-11
13  *
14  */
15 /*
16  * 需求:请使用缓冲输入输出字符流实现用户的登录注册功能.....
17  * 张三      123
18  * 李四      456
19  * 
20  * */
21 public class Demo18 {
22     static Scanner sc=new Scanner(System.in);
23     public static void main(String[] args) throws IOException {
24         while(true){
25             System.out.println("请选择功能:   A(注册)     B(登录)");
26             String option=sc.next();
27             if("a".equalsIgnoreCase(option)){
28                 //注册
29                 reg();
30             }else if("b".equalsIgnoreCase(option)){
31                 //登录
32                 login();
33             
34             }else{
35             System.out.println("输入有误,请从新输入.....");
36             }
37         }
38     }
39     //登录
40     private static void login() throws IOException {
41         System.out.println("请输入用户名");
42         String userName = sc.next();
43         System.out.println("请输入密码");
44         String passWord = sc.next();
45         String info = userName+" "+passWord;
46         //读取文件信息是否存在该用户,如果有则登录成功
47         //建立数据的输入通道
48         //缓冲输入字符流
49         BufferedReader br = new BufferedReader(new FileReader(new File("F:\a.txt")));
50         boolean isLogin = false;
51         String line = null;
52         while((line = br.readLine())!=null){ //readLine:一行一行的读取
53             if(info.equals(line)){
54                 isLogin=true;
55                 break;
56             }
57         }
58         if(isLogin){
59             System.out.println("登录成功欢迎"+userName);
60         }else {
61             System.out.println("用户或密码错误!如未注册,请先注册再登录");
62         }
63     }
64     //注册
65     private static void reg() throws IOException {
66         System.out.println("请输入用户名");
67         String userName = sc.next();
68         System.out.println("请输入密码");
69         String passWord = sc.next();
70         String info = userName+" "+passWord;
71         //把用户信息写到文件上
72         File file = new File("F:\a.txt");
73         //建立通道
74         FileWriter fw = new FileWriter(file,true);
75         //建立缓冲输出字符流
76         BufferedWriter bw = new BufferedWriter(fw);
77         //把用户信息写出
78         bw.write(info);
79         bw.newLine();//换行
80         //关闭资源
81         bw.close();
82     }
83 }

原创作者:DSHORE

作者主页:http://www.cnblogs.com/dshore123/

原文出自:https://www.cnblogs.com/dshore123/p/9274643.html

欢迎转载,转载务必说明出处。(如果本文对您有帮助,可以点击一下右下角的 推荐,或评论,谢谢!

原文地址:https://www.cnblogs.com/dshore123/p/9274643.html