IO流操作文件相关方法(一)

一.file类对文件的基本操作

文件的基本操作:
  • 1.创建功能:
    (1.)createNewFile():
    1.该方法会抛出一个IOException异常,该异常是一个编译时异常,必须处理
    2.该方法创建的是文件,而不是文件夹
    3.如果文件已经创建,就不会再创建了,返回false
File file = new File("D:\\JavaSE\\day\\resource\\a.txt");
System.out.println("createNewFile():" + file.createNewFile());

(2.)boolean mkdir():
1.该方法没有异常
2.该方法创建的是文件夹
3.如果文件已经创建,就不会再创建了,返回false

File file2 = new File("D:\\JavaSE\\day20\\resource\\Demo");
System.out.println("mkdir():" + file2.mkdir());

(3.) boolean mkdirs():
1.该方法没有异常
2.该方法创建的是连续的文件夹
3.如果文件已经创建,就不会再创建了,返回false

File file4 = new File(file2, "aaa\\bbb\\ccc");
System.out.println("mkdirs():" + file4.mkdirs());		
  • 2.删除功能
    boolean delete() :
    1.既可以删除文件,也可以删除文件夹
    2.删除文件夹,文件夹必须是空的
File file = new File("D:\\JavaSE\\day20\\resource\\a.txt");
		System.out.println("delete:" + file.delete());
  • 3.修改功能
    renameTo(File dest):重命名为dest
File file = new File("a.txt");
boolean result = file.renameTo(new File("b.txt"));
System.out.println(result);
  • 4.遍历功能
    (1.)public String[] list() 返回file下的文件和目录名称(字符串)
File file = new File("D:\\JavaSE\\CodeLibrary\\chapter12_iodemo\\src\\com\\sxt\\filedemo");
		String[] fileList = file.list();
		for (String fileName : fileList) {
			System.out.println(fileName);
		}

(2.)public File[] listFiles() 返回file下的文件和目录(文件)

File[] files = file.listFiles();
		for (File f : files) {
			System.out.println(f.getAbsolutePath());
			System.out.println(f.getName());
			System.out.println(f.length());
			System.out.println("-------------");
		}

(3.)public String[] list(FilenameFilter filter) 根据filter过滤返回file下文件和目录名称

File file = new File("D:\\21321312312313123\\JavaSE\\chapter03_分支语句\\键盘录入对象Scanner");
		String[] fileList = file.list(new FilenameFilter() {			
			@Override
			public boolean accept(File dir, String fileName) {
			return new File(dir, fileName).getName().endsWith(".class");
			}
		});//匿名内部类		
		for (String fileName : fileList) {
			System.out.println(fileName);
		}

(4.)public File[] listFiles(FileFilter filter) :根据filter过滤返回file下文件和目录

File file = new File("D:\\21321312312313123\\JavaSE\\chapter03_分支语句\\键盘录入对象Scanner");
		File[] files = file.listFiles(new FileFilter() {			
			@Override
			public boolean accept(File file) {
				return file.getName().endsWith(".java");
			}
		});		
		for (File f : files) {
			System.out.println(f.getAbsolutePath());
		}
  • 5.判断功能
    (1.)public boolean isFile() 是否文件
    (2.)public boolean isDirectory() 是否目录
    (3)public boolean canRead() 是否可读
    (4.)public boolean canWrite() 是否可写
    (5.)public boolean exists() 是否存在
    (6.)public boolean isHidden() 是否隐藏
    (7.)public long length() 长度
    (8.)public String getAbsolutePath() 绝对路径
    (9.)public String getPath() 定义的路径
    (10.)public String getName() file名字
    (11.)public long lastModified() 最后一次修改时间
递归实现复杂文件操作:

1.获取路径下所有文件

	private static void dir(File file) {
		if (!file.exists()) {
			throw new NullPointerException("文件不存在");
		} else {
			if (file.isFile()) {
				f.add(file);
			} else if (file.isDirectory()) {
				File[] list = file.listFiles();
				for (File file2 : list) {
					dir(file2);
				}
			}
		}
	}

2.统计文件大小

	private static void totalcount(File file2) {
		if (file2.isFile()) {
			size += file2.length();
		} else if (file2.isDirectory()) {
			File[] list5 = file2.listFiles();
			for (File file3 : list5) {
				totalcount(file3);
			}
		}
	}

3.统计后缀为.MP4的视频并输出

public static void search(File file) {
		String regex = ".mp4";
		if (file.isFile()&&file.getName().endsWith(regex)) {
			System.out.println(file.getName());
		}else if (file.isDirectory()) {
			File[] list = file.listFiles();
			if (list!=null) {
				for (File file2 : list) {
					search(file2);
				}
			}
		}
	}

二,字符流字节流实现对文件的拷贝方法:

1.字节流

//1.FileInputStream/FileOutputStream 一次性读取一个字节

	private static void copy1(String ol,String ne) {
		try(FileInputStream fis = new FileInputStream(ol);
				FileOutputStream fos = new FileOutputStream(ne);) {
			int len = 0;
			while ((len=fis.read())!=-1) {
				fos.write(len);
				fos.flush();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
	}

2.FileInputStream/FileOutputStream一次性读取一个字节数组

	private static void copy2(String ol,String ne) {
		try(FileInputStream fis = new FileInputStream(ol);
				FileOutputStream fos = new FileOutputStream(ne);) {
			int len = 0;
			byte[] by = new byte[1024];
			while ((len=fis.read(by))!=-1) {
				fos.write(by, 0, len);
				fos.flush();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		}	
	}

3.BufferedInputStream/BufferedOutputStream一次性读取一个字节

	private static void copy3(String ol,String ne) {
		try(BufferedInputStream fis = new BufferedInputStream(new FileInputStream(ol));
				BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(ne));) {
			int len = 0;
			while ((len=fis.read())!=-1) {
				fos.write(len);
				fos.flush();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
	}

4.BufferedInputStream/BufferedOutputStream一次性读取一个字节数组

	private static void copy4(String ol,String ne) {
		try(BufferedInputStream bis =new BufferedInputStream(new FileInputStream(ol));
				BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(ne));) {
			byte[] byt = new byte[1024];
			int len= 0;
			while ((len=bis.read(byt))!=-1) {
				bos.write(byt);
				bos.flush();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}catch (IOException e) {
			e.printStackTrace();
		}	
	}
2.字符流:

5.FileReader/FileWriter一次性读取一个字符

	private static void copy5(String ol,String ne) {
		try(FileReader fr = new FileReader(ol);
				FileWriter fW = new FileWriter(ne);) {
			int len = 0;
			while ((len=fr.read())!=-1) {
				fW.write(len);
				fW.flush();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
	}

6.FileReader/FileWriter一次性读取一个字符数组

	private static void copy6(String ol,String ne) {		
		try(FileReader fr = new FileReader(ol);
				FileWriter fw = new FileWriter(ne);) {	
			char[] cha= new char[1024];
			int len = 0;
			while ((len = fr.read(cha))!=-1) {
				fw.write(cha, 0, len);
				fw.flush();
			}
		}catch (FileNotFoundException e) {
			e.printStackTrace();
		}catch (IOException e) {
			e.printStackTrace();
		}		
	}

7.BufferedReader/BufferedWriter一次性读取一个字符

	private static void copy7(String ol,String ne) {
		try(BufferedReader br =new BufferedReader(new FileReader(ol));
				BufferedWriter bw = new BufferedWriter(new FileWriter(ne));) {
			int len = 0;
			while ((len = br.read())!=-1) {
				bw.write(len);
				bw.flush();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		}		
	}

8.BufferedReader/BufferedWriter一次性读取一个字符数组

	private static void copy8(String ol,String ne) {
		try(BufferedReader br =new BufferedReader(new FileReader(ol));
				BufferedWriter bw = new BufferedWriter(new FileWriter(ne));) {
			int len = 0;
			char[] cha =new char[1024];
			while ((len = br.read(cha))!=-1) {
				bw.write(cha, 0, len);				
				bw.flush();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		}	
	}

9.BufferedReader/BufferedWriter一次性读取一行

	private static void copy9(String ol,String ne) {
		try(BufferedReader br =new BufferedReader(new FileReader(ol));
				BufferedWriter bw = new BufferedWriter(new FileWriter(ne));) {
			String readLine =null;
			while ((readLine = br.readLine())!=null) {
				bw.write(readLine);
				bw.newLine();
				bw.flush();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		}		
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值