【javaEE】Day10-打印流及其他流

1.复制整个文件夹

#### 1.3.1案例需求
  • 把“E:\itcast”这个文件夹复制到 F盘目录下
1.3.2分析步骤
  1. 创建数据源File对象,路径是E:\itcast

  2. 创建目的地File对象,路径是F:\

  3. 写方法实现文件夹的复制,参数为数据源File对象和目的地File对象

  4. 判断数据源File是否是文件

    是文件:直接复制,用字节流

    不是文件:

     	 在目的地下创建该目录
    
     	遍历获取该目录下的所有文件的File数组,得到每一个File对象
    
     	回到3继续(递归)
    

    1.3.3代码实现

public class CopyFoldersDemo {
    public static void main(String[] args) throws IOException {
        //创建数据源File对象,路径是E:\\itcast
        File srcFile = new File("E:\\itcast");
        //创建目的地File对象,路径是F:\\
        File destFile = new File("F:\\");

        //写方法实现文件夹的复制,参数为数据源File对象和目的地File对象
        copyFolder(srcFile,destFile);
    }

    //复制文件夹
    private static void copyFolder(File srcFile, File destFile) throws IOException {
        //判断数据源File是否是目录
        if(srcFile.isDirectory()) {
            //在目的地下创建和数据源File名称一样的目录
            String srcFileName = srcFile.getName();
            File newFolder = new File(destFile,srcFileName); //F:\\itcast
            if(!newFolder.exists()) {
                newFolder.mkdir();
            }

            //获取数据源File下所有文件或者目录的File数组
            File[] fileArray = srcFile.listFiles();

            //遍历该File数组,得到每一个File对象
            for(File file : fileArray) {
                //把该File作为数据源File对象,递归调用复制文件夹的方法
                copyFolder(file,newFolder);
            }
        } else {
            //说明是文件,直接复制,用字节流
            File newFile = new File(destFile,srcFile.getName());
            copyFile(srcFile,newFile);
        }
    }

    //字节缓冲流复制文件
    private static void copyFile(File srcFile, File destFile) throws IOException {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));

        byte[] bys = new byte[1024];
        int len;
        while ((len = bis.read(bys)) != -1) {
            bos.write(bys, 0, len);
        }

        bos.close();
        bis.close();
    }
}		
			
## 2.打印流(重点)
		为什么要学打印流?
			他是一条字节输出流。
	PrintStream-----> OutputStream  (多用于即时通信,既可以写字节数据,还可以写字符数据)

构造:
	PrintStream(File file);-----> 创建打印流指向file;
	PrintStream(String fileName);---------->创建打印流指向fileName指向的文件
	PrintStream(OuputStream out)--------->将普通的字节流包装成打印流,其实是为了使用PrintStream的特有的方法
方法:
	write(int by);
	write(byte[] arr);
	print(各种数据);
	println(各种数据);  使用最多,最好用的
	
	复制Java文件打印流改进版【应用】

- 案例需求

  - 把模块目录下的PrintStreamDemo.java 复制到模块目录下的 Copy.java

- 分析步骤

  - 根据数据源创建字符输入流对象
  - 根据目的地创建字符输出流对象
  - 读写数据,复制文件
  - 释放资源

- 代码实现

  ```java
  public class CopyJavaDemo {
      public static void main(String[] args) throws IOException {
          /*
          //根据数据源创建字符输入流对象
          BufferedReader br = new BufferedReader(new FileReader("myOtherStream\\PrintStreamDemo.java"));
          //根据目的地创建字符输出流对象
          BufferedWriter bw = new BufferedWriter(new FileWriter("myOtherStream\\Copy.java"));
  
          //读写数据,复制文件
          String line;
          while ((line=br.readLine())!=null) {
              bw.write(line);
              bw.newLine();
              bw.flush();
          }
  
          //释放资源
          bw.close();
          br.close();
          */
  
          //根据数据源创建字符输入流对象
          BufferedReader br = new BufferedReader(new FileReader("myOtherStream\\PrintStreamDemo.java"));
          //根据目的地创建字符输出流对象
          PrintWriter pw = new PrintWriter(new FileWriter("myOtherStream\\Copy.java"),true);
  
          //读写数据,复制文件
          String line;
          while ((line=br.readLine())!=null) {
              pw.println(line);
          }
  
          //释放资源
          pw.close();
          br.close();
      }
  }
	
## 3. 对象序列化流(了解)
	对象序列化:就是将对象保存到磁盘中,或者在网络中传输对象
	对象序列化流: ObjectOutputStream
	对象反序列化流: ObjectInputStream
	

## 4.serialVersionUID&transient (了解)

## 5.Properties集合
Properties介绍

  - 是一个Map体系的集合类
  - Properties可以保存到流中或从流中加载
  - 属性列表中的每个键及其对应的值都是一个字符串





	
			

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值