IO 输入输出流
来源:互联网 发布:遗传算法的基本原理 编辑:程序博客网 时间:2024/06/05 00:35
1. IO
1.1. d什么是IO?
I表示Input输入,O表示Output输出,IO表示输入输出。
相对于程序而言,输入是把数据从硬盘、内存、键盘等设备的数据读取到程序中,输出是把程序中的数据保存到硬盘或内存上。
工具包
import java.io.*;
1.2. 什么是IO流?
形象的比喻——水流 ,文件======程序 ,文件和程序之间连接一个管道,水流就在之间形成了,自然也就出现了方向:可以流进,也可以流出.便于理解,这么定义流: 流就是一个管道里面有流水,这个管道连接了文件和程序。
一个流,必有源端和目的端,它们可以是计算机内存的某些区域,也可以是磁盘文件或Internet上的某个URL。
常用输入、输出流
文件在磁盘上通过有两种存储方式:字节(图片、声音、视频,二进制,可以一切文件等等)、字符(文章、小说、一段话,文本文件,只能处理纯文本)。
根据操作对象的类型是字符还是字节可分为两大类: 字符流和字节流。
InputStream是所有字节输入流的祖先,而OutputStream是所有字节输出流的祖先。
Reader是所有读取字符串输入流的祖先,而writer是所有输出字符串的祖先。
字节流
字符流
输入流
InputStream
Reader
输出流
OutputStream
Writer
四个超类都是抽象类,应该使用具体的子类。
1.3. IO接口
基于磁盘操作的I/O接口:File
基于字节操作的I/O接口:InputStream和OutputStream
基于字符操作的I/O接口:Writer和Reader
1.4. File
流的本质也是对文件的处理。处理文件的类File,支持新建文件或文件夹、删除文件或文件夹、获取文件或文件夹的属性等。
1.5. 文件操作
新建,删除,绝对路径,相对路径
/**
* java.io.*;包下
* 讲解文件的基本操作
* 绝对路径:从盘符出发 某个硬盘下的某个文件夹中
* 相对路径:相对于当前工程而言
* @author Administrator
*
*/
public class Test01 {
public static void main(String[] args)throws IOException {
//创建文件对象 绝对路径/home/soft01/others/hello.txt
File file = new File("E:/others/hello.txt");
//File file = new File("E:\\others\\hello2.txt");
//相对路径
//File file = new File("hello.txt");
//File file = new File("src\\com\\njwb\\fileoperation\\hello.txt");
//File file = new File("src/com/njwb/fileoperation/hello.txt");
//解析属性
//parseFile(file);
//删除文件
delFile(file);
}
/**
* 解析文件的基本属性
* @param file
*/
public static void parseFile(File file){
//判断文件是否存在
if(file.exists()){
//判断是文件还是文件夹
if(file.isFile()){
System.out.println("这是一个文件");
//解析文件的属性
System.out.println("文件存在");
System.out.println("文件的名称:"+file.getName());
System.out.println("文件的大小:"+file.length()+"字节");
System.out.println("文件的父路径:"+file.getParent());
System.out.println("文件的相对路径:"+file.getPath());
System.out.println("文件的绝对路径:"+file.getAbsolutePath());
}else if(file.isDirectory()){
System.out.println("这是一个文件夹(目录)");
}
}else{
System.out.println("文件不存在");
}
}
/**
* 删除文件,如果不存在,则创建文件
* @param file
* @throws IOException
*/
public static void delFile(File file)throws IOException{
if(file.exists()){ //存在
if(file.delete()){
System.out.println("文件删除成功");
}else{
System.out.println("文件删除失败");
}
}else{
//文件不存在的时候,需要创建
if(file.createNewFile()){
System.out.println("文件创建成功");
}else{
System.out.println("文件创建失败");
}
}
}
}
1.6. 文件夹操作
绝对相对路径,判断是否是文件夹,判断是否存在,创建,删除,在文件夹中创建文件。
/**
* 创建文件对象
* @author Administrator
*
*/
public class Test02 {
public static void main(String[] args)throws IOException {
File path = new File("E:/others");
//在path表示的路径下建1个a1.txt (2个参数 父文件对象,子文件名称字符串)
File file = new File(path,"a1.txt");
//创建文件
if(file.createNewFile()){
System.out.println("a1.txt文件创建成功");
}else{
System.out.println("a1.txt文件创建失败");
}
//在path路径下创建a2.txt (2个参数 父文件的路径名称字符串,子文件名称字符串)
File file2 = new File("E:/others","a2.txt");
//创建文件
if(file2.createNewFile()){
System.out.println("a2.txt文件创建成功");
}else{
System.out.println("a2.txt文件创建失败");
}
//在path路径下,创建a3.txt
File file3 = new File("E:/others/a3.txt");
//创建文件
if(file3.createNewFile()){
System.out.println("a3.txt文件创建成功");
}else{
System.out.println("a3.txt文件创建失败");
}
}
}
/**
* 文件夹的基本操作
* 区别:
* mkdir 如果父路径不存在,那么不能创建父路径也不能创建子文件夹对象(这种创建文件夹,必须得在父路径存在 的情况下才能创建)
* mkdirs 如果父路径不存在,可以连同父路径一起创建(用这个)
* @author Administrator
*
*/
public class Test03 {
public static void main(String[] args) {
File dir = new File("E:/others/a/a2");
//创建目录
createDir(dir);
//删除目录
delDir(dir);
}
public static void delDir(File dir){
if(dir.exists()){
if(dir.delete()){
System.out.println("文件夹删除成功");
}else{
System.out.println("文件夹删除失败");
}
}else{
System.out.println("文件夹不存在");
}
}
/**
* 创建文件夹
* @param dir
*/
public static void createDir(File dir){
if(!dir.exists()){
if(dir.mkdirs()){
System.out.println("文件夹创建成功");
}else{
System.out.println("文件夹创建失败");
}
}else{
System.out.println("文件夹已存在,无需创建");
}
}
}
1.7. 文件、文件夹重命名移动
重命名
移动
/**
* 文件或者文件夹的重命名,移动
* @author Administrator
*
*/
public class Test04 {
public static void main(String[] args) {
//E:others/a/a1.txt 重命名为E:/others/a/m1.txt
//文件对象
File src = new File("E:/others/a/a1.txt");
File dest = new File("E:/others/a/m1.txt");
if(src.renameTo(dest)){
System.out.println("重命名成功");
}else{
System.out.println("重命名失败");
}
//E:/others/a/m1.txt 移动到D:/others/test1.txt
File dest2 = new File("D:/others/test1.txt");
//根据父路径构建文件对象,要保证父路径存在
File parent = new File(dest2.getParent()); //D:/others
if(!parent.exists()){
parent.mkdirs();
}
if(dest.renameTo(dest2)){
System.out.println("移动成功");
}else{
System.out.println("移动失败");
}
}
}
/**
* 文件或者文件夹的重命名,移动
* @author Administrator
*
*/
public class Test042 {
public static void main(String[] args) {
//E:others/a 重命名为E:/others/mm
//文件对象
File src = new File("E:/others/a");
File dest = new File("E:/others/mm");
if(src.renameTo(dest)){
System.out.println("重命名成功");
}else{
System.out.println("重命名失败");
}
//E:/others/mm 移动到D:/others/cc
File dest2 = new File("D:/others/cc");
//根据父路径构建文件对象,要保证父路径存在
File parent = new File(dest2.getParent()); //D:/others
if(!parent.exists()){
parent.mkdirs();
}
//发现windows下空的文件夹无法移动到目标位置
if(dest.renameTo(dest2)){
System.out.println("移动成功");
}else{
System.out.println("移动失败");
}
}
}
另想办法解决,自己写拷贝方法,拷贝所有的子文件夹
/**
* 拷贝所有的子文件夹
* @param src
* @param dest
*/
public static void copyAllDir2(File src,File dest){
if(src.exists()){
if(src.isDirectory()){
//如果建立和自身同名的父文件夹,提示出错
if(src.getAbsolutePath().equals(dest.getAbsolutePath())){
System.out.println("自身不能复制到自身,没有意义");
return;
}
//如果dest是src的子目录,提示出错
if(dest.getAbsolutePath().contains(src.getAbsolutePath()) && !dest.getParent().equals(src.getParent())){
System.out.println("不能将父文件夹复制到子文件夹中");
System.out.println("目标路径***"+dest.getAbsolutePath());
System.out.println("源路径***"+src.getAbsolutePath());
return;
}
//判断目标目录是否存在,不存在,则创建
if(!dest.exists()){
dest.mkdirs();
}
//获取所有的子文件对象
File[] arr = src.listFiles();
//遍历子文件对象
for(File f:arr){
//再判断子文件对象是否是目录,如果是,继续调用自身的方法
copyAllDir(f,new File(dest,f.getName()));
}
}
}
}
1.8. 文件遍历
把指定文件夹下,一级子文件中带指定名称的文件或文件夹删除
分析: 先获取所有子文件的名称,将每个名称转换成小写或大写,用contains(“copy”),如果包含,调用delete()
/**
* 把指定文件夹下,一级子文件中带指定名称"copy"的文件或文件夹删除
* @author Administrator
*
*/
public class Test03 {
public static void main(String[] args) {
File src = new File("E:/others/hh");
/*System.out.println("---------方式1-----------");
//获取子文件列表
File[] arr = src.listFiles();
//遍历子文件的过程中,判断每个子文件的名称是否包含指定字样
for(File f:arr){
//System.out.println(f.getName().toLowerCase());
if(f.getName().toLowerCase().contains("copy")){
f.delete();
}
}*/
System.out.println("---------方式2-----------");
//获取子文件名称
String[] namesArr = src.list();
for(String s:namesArr){
//System.out.println(s.toLowerCase());
if(s.toLowerCase().contains("copy")){
//删除对应的子文件对象
//构建子文件对象
File sub = new File(src,s);
sub.delete();
}
}
}
}
利用递归列出全部文件(什么是递归?在符合某种条件下,持续的调用自身的方法,直到条件不满足为止)
递归遍历 E:/others/mm文件夹,列出所有的子文件(子文件夹,子文件)
/**
* 将指定目录下的所有子文件列出
* @author Administrator
*
*/
public class Test04 {
public static void main(String[] args) {
showAllFiles(new File("E:/others/mm"));
}
public static void showAllFiles(File src){
if(src.exists()){
if(src.isDirectory()){
//获取子文件列表
File[] arr = src.listFiles();
//遍历子文件,继续调用自身的方法
for(File f:arr){
showAllFiles(f);
System.out.println(f.getAbsolutePath());
}
}
}
}
}
E:\others\mm\mma1\mmb1\mmc1.txt
E:\others\mm\mma1\mmb1
E:\others\mm\mma1\mmb2\mmc2.txt
E:\others\mm\mma1\mmb2
E:\others\mm\mma1
E:\others\mm\mma2
E:\others\mm\mma3.txt
1.9. 文件和文件夹操作练习
(1) 创建相对路径文件夹Parent/Child,在该文件夹中创建子文件new.txt
(2)在Parent文件夹下,创建文件名为Child10~Child20共20个子文件夹
(2) 在Parent文件夹下,创建Child100~Child200个子文件夹,并在每个子文件夹下创建new10.txt~new20.txt共11个文件。
(3) 把Parent文件夹下一级每个名包含3的文件夹重命名,在文件夹名称末尾追加字母A。
(4) 在Parent>Child下新建5个文件夹,Child5~10,在这些文件夹下,每个放5个文件Hello1.txt~Hello5.txt
(5) 利用递归删除Parent文件夹下所有的文件,但保留文件夹。
(6)新建FV215B文件夹,把Parent目录下所有子文件夹复制到FV215B。
(8)新建FV215B文件夹,把Parent目录下所有子文件和子文件夹复制到FV215B。
1.10. 目录拷贝
练习:完成复制所有的目录及其子目录下的所有文件
见FileUtil中的public static void copyAllFiles(File src,File dest)方法
1.11. 字节流
FileInputStream和FileOutputStream的构造方法允许通过文件来构造流。
FileInputStream infile = new FileInputStream("myfile.txt");
FileOutputStream outfile = new FileOutputStream("results.txt");
1.12. FileOutputStream字节流写文件
输出一段话到指定文件
“今天吃了10个包子 今天吃了10个馒头”分两行写出到E:/others/helloworld.txt中
/**
* 将 “今天吃了10个包子,今天吃了10个馒头”写出到E:/others/helloworld.txt中
* @author Administrator
*
*/
public class TestFileOutputStream {
public static void main(String[] args) {
String[] data = {"今天吃了10个包子","今天吃了10个馒头"};
//声明输出流对象
FileOutputStream fos = null;
try {
//建立输出流和目标文件之间的联系
fos = new FileOutputStream("E:/others/helloworld.txt");
for(String s:data){
//写出
byte[] buffer = (s+"\r\n").getBytes();
//fos.write(buffer, 0, buffer.length);
fos.write(buffer);
}
fos.flush(); //刷新
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(null!=fos){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
练习:
(1)创建一个数组,给100个元素,每个元素是随机数0~100,将100个元素分100行写入array.txt。
(2)创建一个用户类,至少包含用户名和密码,初始化10组账户存入ArrayList,最后写入文件user.txt,用户名和密码以逗号分隔。
1.13. FileInputStream字节流读文件
FileInputStream读取文本文件实现步骤:
1.引入相关的类
2.构造文件输入流 FileInputStream 对象
3.读取文本文件的数据
4.关闭文件流对象
练习:从指定路径下,读取文件内容到控制台
E:/others/a/hello1.txt ,里面“abcde” ,将其读取出来输出在控制台
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
/**
* E:/others/a/hello1.txt ,里面“abcde” ,将其读取出来输出在控制台
* int read()
从此输入流中读取一个数据字节。 ,1次读取1个,效率低
int read(byte[] b)
从此输入流中将最多 b.length个字节的数据读入一个byte数组中。
int read(byte[] b,int off,int len)
从此输入流中将最多len 个字节的数据读入一个 byte数组中。
* @author Administrator
*
*/
public class TestInputStream01 {
public static void main(String[] args) {
//声明输入流对象
InputStream ins = null;
try {
//建立输入流和源文件之间的联系
ins = new FileInputStream("E:/others/a/hello1.txt");
int len = ins.read();
while(len!=-1){
System.out.println((char)len);
len = ins.read();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class TestInputStream012 {
public static void main(String[] args) {
//声明输入流对象
InputStream ins = null;
try {
//建立输入流和源文件之间的联系
ins = new FileInputStream("E:/others/a/hello1.txt");
//声明byte[]数组,用于存储读取的内容--->运西瓜的手推车(1次可以运10个西瓜)
byte[] buffer = new byte[4];
int len = ins.read(buffer);
while(len!=-1){
System.out.println("实际读取的到字节数:len="+len);
//读取的内容存储在buffer中,需要将buffer数组--->转换成字符串String ,才能在控制台输出
//Stringstr = new String(buffer);
//读取多少,显示多少,显示出实际读的,new String(byte[]数组对象,索引0,len实际读取的字节数);
String str = new String(buffer,0,len);
System.out.println(str);
len = ins.read(buffer);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* byte[] arr = new byte[10];
* new String(arr)把arr数组转换成String
* new String(arr,offset,len) 把arr数组中从索引为offset的位置,截取len 个字符转换成字符串
* E:/others/a/hello1.txt ,里面“abcde” ,将其读取出来输出在控制台
* int read()
从此输入流中读取一个数据字节。 ,1次读取1个,效率低
int read(byte[] b) (推荐)
从此输入流中将最多 b.length个字节的数据读入一个byte数组中。
int read(byte[] b,int off,int len) off :读取的内容从索引为off的位置开始存储
注意: 0<=len<=b.length-off
从此输入流中将最多len 个字节的数据读入一个 byte数组中。
read(buffer,0,buffer.length) ---->read(buffer)
* @author Administrator
*
*/
public class TestInputStream013 {
public static void main(String[] args) {
//声明输入流对象
InputStream ins = null;
try {
//建立输入流和源文件之间的联系
ins = new FileInputStream("E:/others/a/hello1.txt");
//声明byte[]数组,用于存储读取的内容--->运西瓜的手推车(1次可以运10个西瓜)
byte[] buffer = new byte[10];
//此处表示在buffer中从索引为3的位置开始存储,读取的内容从头存储,总共读取了5个字节,会有索引0,1,2位置的3个元素的丢失
int len = ins.read(buffer,3,buffer.length-3);
while(len!=-1){
System.out.println("实际读取的到字节数:len="+len);
//读多少,显示多少,读到的是5,按5个字节显示,但是前3个位置没有存储内容,只显示了ab
//Stringstr = new String(buffer,0,len);
//如果显示的长度变长了,后面的 内容可以显示,看不出来丢失了
//Stringstr = new String(buffer,0,10);
String str = new String(buffer,3,len);
System.out.println(str);
len = ins.read(buffer,0,buffer.length);
System.out.println("len="+len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
E:/others/a/hello2.txt,里面“胖子都是潜力股” ,将其读取出来输出在控制台
1.14. InputStreamReader解决中文乱码
支持汉字的,常见的由3种编码 : gb2312 支持简体中文 ,gbk 支持简体中文和繁体中文 utf-8 支持全世界所有的字符
/**
* 解决中文乱码
* E:/others/a/hello3.txt,该文件的编码格式ANSI,当前程序中UTF-8,两边编码格式不一致,导致乱码,如何解决
* InputStreamReader(InputStream in, String charsetName)
创建使用指定字符集的 InputStreamReader。 第1个参数,包裹其他的流对象,第2个参数,指定编码格式
ANSI--->GBK
utf-8--->utf-8
* @author Administrator
*
*/
public class TestInputStream022 {
public static void main(String[] args) {
//声明输入流对象
InputStreamReader isr = null;
try {
//建立输入流和源文件之间的联系
isr = new InputStreamReader(new FileInputStream("E:/others/a/hello3.txt"),"gbk");
//声明byte[]数组,用于存储读取的内容
char[] buffer = new char[3];
int len = 0;
//读取的实际字节数存放到len中,判断len是否等于-1,不等于-1,接着读
while(-1!=(len=isr.read(buffer))){
System.out.println("实际读取的到字节数:len="+len);
String str = new String(buffer,0,len);
System.out.println(str);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
//关闭资源
if(null!=isr){
try {
isr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
1.15. FileInputStream,FileOutputStream拷贝文件
import java.io.*;
/**
* FileInputStream(File file)
通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中的File对象file指定。
FileOutputStream(File file)
创建一个向指定 File对象表示的文件中写入数据的文件输出流。
* 利用FileInputStream文件输入流读取内容,用FileOutputStream文件输出流往外写出内容,完成拷贝的功能
* 1.引入相关类 导包 java.io.*;
* 2.声明输入,输出流对象,建立输入流和源文件之间的联系,建立输出流和目标文件之间的联系
* 3.通过输入流读取read,输出流写出write
* 4.关闭流 (先打开的(先new的),后关闭)
* E:/others/mm/helloworld.txt,将其复制到D:/test/test.txt注意目标路径的父路径必须存在D:/test路径存在的
* E:/others/mm/a.png ,将其复制到D:/test/m.png
* @author Administrator
*
*/
public class TestCopy01 {
public static void main(String[] args) {
//源文件对象,目标文件对象
File src = new File("E:/others/mm/a.png");
File dest = new File("D:/test/m.png");
//声明输入流对象
FileInputStream fis = null;
//声明输出流对象
FileOutputStream fos = null;
try {
//建立输入流和源文件之间的联系
fis = new FileInputStream(src);
//建立输出流和目标文件之间的联系
fos = new FileOutputStream(dest);
//声明byte数组,用于存储读取的字节
byte[] buffer = new byte[1024];
//声明int变量,用于存储实际读取的字节数
int len = fis.read(buffer);
while(len!=-1){
//将读取的内容写出到目标文件
fos.write(buffer, 0, len);
//没有读取完,继续读取
len = fis.read(buffer);
}
//刷新一下
fos.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
//关闭流
if(null!=fos){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(null!=fis){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
1.16. FileUtil工具类
package com.njwb.fileoperation02;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* 拷贝所有的子文件夹
* 拷贝单个文件
* 拷贝所有的子文件,包括子文件夹,子文件
* 删除指定目录下的所有子文件,不包括最外层的文件夹
* 删除指定目录下的所有子文件,包括最外层的文件夹
* @author Administrator
*
*/
public class FileUtil {
/**
* 拷贝所有的子文件夹
* @param src
* @param dest
*/
public static void copyAllDir(File src,File dest){
if(src.exists()){
if(src.isDirectory()){
//如果dest是src的子目录,提示出错
if(dest.getAbsolutePath().contains(src.getAbsolutePath()) && !dest.getParent().equals(src.getParent())){
System.out.println("不能将父文件夹复制到子文件夹中");
System.out.println("目标路径***"+dest.getAbsolutePath());
System.out.println("源路径***"+src.getAbsolutePath());
return;
}
//判断目标目录是否存在,不存在,则创建
if(!dest.exists()){
dest.mkdirs();
}
//获取所有的子文件对象
File[] arr = src.listFiles();
//遍历子文件对象
for(File f:arr){
//再判断子文件对象是否是目录,如果是,继续调用自身的方法
copyAllDir(f,new File(dest,f.getName()));
}
}
}
}
/**
* 拷贝单个文件
* @param srcPath 源文件路径
* @param destPath 目标文件路径
*/
public static void copySingleFile(String srcPath,String destPath){
File src = new File(srcPath);
File dest = new File(destPath);
copySingleFile(src, dest);
}
/**
* 拷贝单个文件
* @param src
* @param dest
*/
public static void copySingleFile(File src,File dest){
//判断一下src是否是文件,不是的提示用户,只能拷贝文件
if(!src.isFile()){
System.out.println("只能拷贝单个文件");
return;
}
//声明输入流对象
FileInputStream fis = null;
//声明输出流对象
FileOutputStream fos = null;
try {
//建立输入流和源文件之间的联系
fis = new FileInputStream(src);
//建立输出流和目标文件之间的联系
fos = new FileOutputStream(dest);
//声明byte数组,用于存储读取的字节
byte[] buffer = new byte[1024];
//声明int变量,用于存储实际读取的字节数
int len = fis.read(buffer);
while(len!=-1){
//将读取的内容写出到目标文件
fos.write(buffer, 0, len);
//没有读取完,继续读取
len = fis.read(buffer);
}
//刷新一下
fos.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
//关闭流
if(null!=fos){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(null!=fis){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 拷贝所有的子文件,子文件夹
* @param src 源文件对象
* @param dest 目标文件对象
*/
public static void copyAllFiles(File src,File dest){
if(src.exists()){
//如果dest是src的子目录,提示出错
if(dest.getAbsolutePath().contains(src.getAbsolutePath()) && !dest.getParent().equals(src.getParent())){
System.out.println("不能将父文件夹复制到子文件夹中");
return;
}
if(src.isDirectory()){
//判断目标目录是否存在,不存在,则创建
if(!dest.exists()){
dest.mkdirs();
}
//获取所有的子文件对象
File[] arr = src.listFiles();
//遍历子文件对象
for(File f:arr){
//再判断子文件对象是否是目录,如果是,继续调用自身的方法
copyAllFiles(f,new File(dest,f.getName()));
}
}else if(src.isFile()){
copySingleFile(src, dest);
}
}
}
/**
* 拷贝所有的子文件(包括子文件,子文件夹)
* @param srcPath 源文件夹的路径
* @param destPath 目标文件夹的路径
*/
public static void copyAllFiles(String srcPath,String destPath){
File src = new File(srcPath);
File dest = new File(destPath);
copyAllFiles(src, dest);
}
/**
* 删除指定目录下的所有子文件,不包括最外层的
* @param src
*/
/*public static void delAllFileExceptOuter(File src){
if(src.exists()){
if(src.isDirectory()){
//获取子文件列表
File[] arr = src.listFiles();
//遍历子文件,如果子文件是目录的话,继续调用自身的方法
for(File f:arr){
if(f.isDirectory()){
delAllFileExceptOuter(f);
}
f.delete();
}
}
}
}*/
/**
* 删除指定目录下的所有子文件,不包括最外层的
* @param src
*/
public static void delAllFileExceptOuter(File src){
if(src.exists()){
if(src.isDirectory()){
//获取子文件列表
File[] arr = src.listFiles();
//遍历子文件,如果子文件是目录的话,继续调用自身的方法
for(File f:arr){
//为何此处省略的目录的判断,因为在外层的时候判断了一次是否是目录
delAllFileExceptOuter(f);
f.delete();
}
}
}
}
/**
* 删除所有的子文件,包括最外层的父文件夹
* @param src
*/
public static void delAllFile(File src){
if(src.exists()){
if(src.isDirectory()){
//获取子文件列表
File[] arr = src.listFiles();
//遍历子文件,如果子文件是目录的话,继续调用自身的方法
for(File f:arr){
//为何此处省略的目录的判断,因为在外层的时候判断了一次是否是目录
delAllFile(f);
f.delete();
}
}
src.delete();
}
}
}
作业
遍历作业文件夹,
把每个人的姓名保存为《姓名.txt》
- IO输入输出流
- IO输入输出流
- Java IO输入输出流
- 输入输出IO流
- java13-IO输入输出流
- IO输入输出流-2
- IO输入输出流-3
- java io 输入输出流
- IO流--输入输出
- IO(输入输出流)
- IO序列输入输出流
- IO输入输出流
- IO-输入输出流
- Java IO(输入输出流)
- Java输入输出流IO
- IO输入输出流
- JAVA-IO输入输出流
- IO输入输出流
- ObjectBox入门
- 基于Anaconda的tensorboard可视化
- 图片旋转
- Android中的串口通讯全解析
- CentOS 6.0/7.0关闭默认防火墙启用iptables防火墙
- IO 输入输出流
- java ==和equals区别
- Java BitSet使用场景和示例
- 关于this指针指向的知识回顾
- 数据结构复杂度与递归----C++初学之路
- myBatis框架
- Java Bitset类
- ADO数据库编程详解(C++)----初级入门篇
- Hibernate的搭建