IO流的使用
2016-06-30 16:17
381 查看
本文主要练习了IO流用于打印文件目录层次,使用缓冲流(BufferedReader和BufferedWriter)和转换流(InputStreamReader和OutputStreamWriter)对纯文本文件进行拷贝,以及对文件夹的拷贝,该文件夹的拷贝涉及到文件的拷贝,文件目录的遍历
目录层次打印
运行结果:
纯文本拷贝
主要用到了递归遍历
文件夹的拷贝
主要用到了递归和文件的拷贝
目录层次打印
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会议内容 ->->4月17日开会报告 ->->->4月17日 ->->->->Discussion2.pptx ->->->->Presentation_Junyu_0417.pptx ->->->->SCMA4月17日报告.ppt ->->->->SCMA项目 4月3日会议纪要.doc ->->4月17日开会报告.zip ->->8月31日报告(SCMA组).ppt ->->SCM 5月8日报告.ppt ->->SCMA4月17日会议记录.doc ->->SCMA4月17日报告.ppt ->->SCMA4月3日报告内容.doc ->->SCMA发射端(一)11.ppt ->->SCMA租4月3开会报告 ->->->SCMA_能效报告_V4.ppt ->->->SCMA发射端(一)new.ppt ->->SCMA租4月3开会报告.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
纯文本拷贝
主要用到了递归遍历
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(); } }
文件夹的拷贝
主要用到了递归和文件的拷贝
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(); } }
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树