IO流的使用

1 篇文章 0 订阅

本文主要练习了IO流用于打印文件目录层次,使用缓冲流(BufferedReader和BufferedWriter)和转换流(InputStreamReader和OutputStreamWriter)对纯文本文件进行拷贝,以及对文件夹的拷贝,该文件夹的拷贝涉及到文件的拷贝,文件目录的遍历

  1. 目录层次打印
package com.java.io;

import java.io.File;

/**
 * 文件的遍历,遇到文件,打印文件名,遇到文件夹则继续遍历
 * 思路:递归
 * @author kkquiet
 *
 */

public class ListFile {

    public static void main(String[] args) {
        String srcPath = "E:/开会ppt";
        File src = new File(srcPath);
        if(src.exists()){
            int level = 0;
            printFile(src,level);
        }else{
            System.out.println("the path is null");
        }

    }

    private static void printFile(File src,int level) {
        for(int i = 0;i<level;i++){
            System.out.print("->");
        }
        if(src.isFile()){
            System.out.println(src.getName());
        }else
        {
            System.out.println(src.getName());
            level++;
            for(File list:src.listFiles()){
                printFile(list,level);
            }
        }

    }

}

运行结果:

开会ppt
->SatelliteYuWang.pdf
->SCMA会议内容
->->417日开会报告
->->->417->->->->Discussion2.pptx
->->->->Presentation_Junyu_0417.pptx
->->->->SCMA4月17日报告.ppt
->->->->SCMA项目 43日会议纪要.doc
->->417日开会报告.zip
->->831日报告(SCMA组).ppt
->->SCM 58日报告.ppt
->->SCMA4月17日会议记录.doc
->->SCMA4月17日报告.ppt
->->SCMA4月3日报告内容.doc
->->SCMA发射端(一)11.ppt
->->SCMA租43开会报告
->->->SCMA_能效报告_V4.ppt
->->->SCMA发射端(一)new.ppt
->->SCMA租43开会报告.zip
->TLAR.pdf
->其他会议报告
->->任务及学习心得.doc
->->第一周报告.doc
->->第三周任务报告.doc
->->第二周任务报告.doc
->->论文
->->->3color_HamCycle.pdf
->->->ref.bib
->->->reference
->->->->MaxMin_EE_OFDMA_Double_yuzhouli.pdf
->->->->Resource Allocation for Maximizing the Device-to-Device Communications Underlaying LTE-Advanced Networks.pdf
->->->Resource Allocation for Maximizing the Device-to-Device Communications Underlaying LTE-Advanced Networks.pdf
->->->因子图4.eps
->->->因子图4.pdf
->->->网络场景图_v3.eps
->->->网络场景图_v3.pdf
  1. 纯文本拷贝
    主要用到了递归遍历
package com.java.io;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

/**
 * 
 * 文件的copy
 * 练习缓冲流和转换流
 * 只能处理纯文本
 * @author kkquiet
 *
 */
public class CopyFile {

    public static void main(String[] args) throws IOException {
        String srcPath = "D:/Java_code/一些算法的java实现/src/maxSumSubQue/maxSumSubQue.java";
        String desPath = "e:/aaa.java";
        copyFileDetail(srcPath,desPath);

    }

    private static void copyFileDetail(String srcPath, String desPath) throws IOException {
        //创建流
        BufferedReader br = new BufferedReader(
                                            new InputStreamReader(
                                                        new FileInputStream(
                                                                new File(srcPath)),"gbk"));
        BufferedWriter bw = new BufferedWriter(
                                                new OutputStreamWriter(
                                                        new FileOutputStream(
                                                                new File(desPath)),"gbk"));
        //文件拷贝
        String str = null;
        while(null != (str = br.readLine())){
            bw.write(str);
            bw.newLine();
        }
        bw.flush();
        bw.close();
        br.close();
    }

}
  1. 文件夹的拷贝
    主要用到了递归和文件的拷贝
package com.java.io;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 
 * 文件夹的拷贝
 * 思路:将文件拷贝,文件夹的创建,以及用递归的方法遍历文件夹目录结合起来
 *              a                                   A
 *           /    \                                 |
 *          b      c           ------->             a
 *        /   \                                   /    \
 *      d       e                                b       c  
 *                                             /   \    
 *                                            d     e
 * @author kkquiet
 *
 */
public class CopyDirs {

    public static void main(String[] args) throws IOException {
        String srcPath = "E:/开会ppt";
        String desPath = "d:/";
        //创建文件对象
        File src = new File(srcPath);
        File des = new File(desPath, src.getName());
        //检查源路径是否是文件夹
        if(!src.isDirectory()){
            System.out.println("this is not a dir");
        }else
            copyDir(src, des);

    }

    private static void copyDir(File src, File des) throws IOException {
        if(src.isFile()){
            //File desTemp = new File(des,src.getName());
            copyFile(src,des);
        }else if(src.isDirectory()){
            des.mkdirs();
            for(File list: src.listFiles()){
                copyDir(list, new File(des,list.getName()));
            }
        }

    }

    private static void copyFile(File src, File des) throws IOException {
        //创建字节缓冲流
        BufferedInputStream bis = new BufferedInputStream(
                                                    new FileInputStream(src));
        BufferedOutputStream bos = new BufferedOutputStream(
                                                    new FileOutputStream(des));
        //文件的拷贝
        int len = 0;
        byte[] bt = new byte[1024];
        while(-1 != (len = bis.read(bt))){
            bos.write(bt, 0, len);
        }
        bos.flush();
        //先打开的流后关闭
        bos.close();
        bis.close();

    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值