文件和IO流

【文件 】

 java.io.File类

 文件和目录路径名的抽象表示形式。

1.File:

可以指文件也可以指文件夹,其实是一个概念,要创建真实的文件,需要调用createNewFile方法,创建单级文件夹mkdir方法,多级文件夹mkdirs

2.delete和deleteOnExit区别

delete为直接删除,deleteOnExit:程序运行deleteOnExit成功后,File并没有直接删除,而是在虚拟机正常运行结束后才会删除。

3.renameTo(File f)

重命名 - 可以当作剪切使用

4.getTotalSpace获取当前分区总的空间大小,

getFreeSpace空闲空间大小,

getUseableSpace获取可用的,获取已用空间大小:

getTotalSpace减去getFreeSpace

5.exist方法

判断File对象是否存在

6.isFile和isDirectory

判断File对象是否为文件或者文件夹

7.list和listFiles区别

① list() 返回一个字符串数组,这些字符串指定此抽象路径名表示的目录中的文件和目录。

以C盘为例,返回的是c盘下文件夹名字的字符串数组,如[TEMP, Windows]

②listFiles() 返回一个抽象路径名数组,这些路径名表示此抽象路径名表示的目录中的文件。

以C盘为例返回的是C盘下文件夹目录地址,如[c:\TEMP, c:\Windows]

8.getPath和getAbslutePath方法

获取文件相对路径和获取绝对路径(全部)

 绝对路径: 从盘符开始的路径

 相对路径: 从当前路径开始的路径

 

9.getParent

获取父文件对象

10.canRead和canWrite

判断读写权限

11.lastMofied

获取上次修改时间

12.getName

获取File名

 

操作实例

 

遍历多级文件夹

package com.bwf.file;

import java.io.File;

public class Demo6 {

	public static void main(String[] args) {
		File dir = new File("E:\\kf23\\02 javaSE");
		
		printAllFile(dir, 0);
		
	}
	
	// 打印某个目录下所有的文件名 (一级)
	public static void printAllFile(File dir, int level){
		
		File[] files = dir.listFiles();
		
		for (int i = 0; i < files.length; i ++) {
			File f = files[i];
			
			// 先打印 --| 
			for(int j = 0; j < level; j ++) {
				System.out.print("--|");
			}
			System.out.println(f.getName());
			// 判断正在遍历的这个子文件是不是文件夹
			if(f.isDirectory()) {
				printAllFile(f, level + 1);
			}
		}
		
	}
	
}

 

删除多级目录

package com.bwf.file;

import java.io.File;

public class Demo9 {

	public static void main(String[] args) {
		File dir = new File("D:\\a");
//		delOneDir(dir);
		delete(dir);
	}
	
	
	// 删除一级目录
	public static void delOneDir(File dir) {
		
		File[] files = dir.listFiles();
		for (File f : files) {
			f.delete();
			System.out.println("删除了:" + f.getAbsolutePath());
		}
		
		dir.delete();
		System.out.println("删除了:" + dir.getAbsolutePath());
	}
	
	
	public static void delete(File f) {
		if(f.isDirectory()) {
			
			File[] files = f.listFiles();
			for (File file : files) {
				delete(file);
			}
			
		}
		// 删自己
		f.delete();
		System.out.println("删除了:" + f.getAbsolutePath());
	}
	
	
}

 

【IO流】

 

 用来进行硬盘和内存数据交换的对象

 根据方向分类:

 

 输入流: 方向是流进内存

 

 输出流: 方向是从内存中流出去的

 

 根据操作的数据类型分类:

 

 字符流: 只能处理字符类型(文本型)数据

 

 字节流: 任意类型数据

【字节流】

 

 1. 最大的抽象父类:

InputStream : 输入流

OutputStream : 输出流

 

 2. 用于读写文件的字节流 (具体的流)

FileInputStream: 文件输入流

FileOutputStream: 文件输出流

 

 3. 带缓冲的字节流

BufferedInputStream: 带缓冲的字节输入流

BufferedOutputStream:带缓冲的字节输出流

 

 * 带缓冲的流写完数据后必须 刷新! flush();

 

不带缓冲的流采用的是边读边写进硬盘,就会很慢,也伤硬盘。缓冲区就是内存里的一块区域,把数据先存内存里,然后一次性写入,类似数据库的批量操作,这样效率比较高。

 

【字符流】

 

 1. 只能处理字符类型数据

 

 2. 本质也是字节流(加了个码表)

 

 3. 最大的抽象父类

Reader: 字符输入流

Writer: 字符输出流

 

 3. 创建字符流的方式

a. 具体的字符流(FileReader FileWriter)

b. 字节流转成字符流(InputStreamReader OutputStreamWriter)

 

 

 

 

 

 

操作实例:

 

复制文件

package com.bwf.stream;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Demo3 {

	public static void main(String[] args) throws IOException {
		
		File src = new File("E:\\[Java参考文档].JDK_API_1_6_zh_CN.CHM");
		File dest = new File("D:\\[Java参考文档].JDK_API_1_6_zh_CN.CHM");
		
//		copy(src, dest);
//		copy2(src, dest);
		copy3(src, dest);
	}
	
	/**
	 * 复制文件
	 * @param src  源文件路径
	 * @param dest 目标路径
	 */
	public static void copy(File src, File dest) {
		
		InputStream is = null;
		OutputStream os = null;
		
		try {
			is = new FileInputStream(src);
			os = new FileOutputStream(dest);
			
			// 一边读 一边写
			int b = -1;
			while((b = is.read()) != -1) {
				os.write(b);
			}
			System.out.println("复制完毕.");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if(is != null) {
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(os != null) {
				try {
					os.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			
		}
		
	}
	
	/**
	 * 使用自定义缓冲区提高复制效率
	 * @param src
	 * @param dest
	 * @throws IOException 
	 */
	public static void copy2(File src, File dest) throws IOException {
		InputStream is = new FileInputStream(src);
		OutputStream os = new FileOutputStream(dest);
		// 准备缓冲区
		byte[] b = new byte[1024];
		// 读到的字节数
		int len = 0;
		// 循环读
		while((len = is.read(b)) != -1) {
			// 在循环中写
			os.write(b, 0 ,len);
		}
		System.out.println("复制完成!");
		is.close();
		os.close();
	}
	
	/**
	 * 带缓冲的流复制
	 * @param src
	 * @param dest
	 * @throws IOException
	 */
	public static void copy3(File src, File dest) throws IOException{
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src));
		BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(dest));
		// 缓冲区
		byte[] b = new byte[1024];
		// 长度
		int len = 0;
		// 读
		while((len = bis.read(b)) != -1) {
			bos.write(b, 0, len);
		}
		bos.flush();
		System.out.println("复制完毕");
		bis.close();
		bos.close();
	}
	
	
	
}

复制多级文件夹

package com.bwf.test;

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

public class Test2 {

	// 复制多级文件夹
	public static void main(String[] args) {
		
		File src = new File("D:\\11 IO流练习");
		
		File dest = new File("D:\\a");
		
		
		copyDir(src, dest);
		
	}
	
	/**
	 * 复制多级文件夹
	 * @param src 源路径
	 * @param dest 目标路径
	 */
	public static void copyDir(File src, File dest) {
		
		if(src.isDirectory()) {
			
			File dir = new File(dest, src.getName());
			if(!dir.exists()) {
				dir.mkdirs();
				System.out.println("创建了目录:" + dir.getAbsolutePath());
			}
			
			File[] files = src.listFiles();
			for (File f : files) {
				copyDir(f, dir);
			}
			
			
		} else {
			copy(src, new File(dest, src.getName()));
		}
		
		
	}

	/**
	 * 用IO流复制文件
	 * @param src
	 * @param dest
	 */
	private static void copy(File src, File dest) {
		
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		
		try {
			bis = new BufferedInputStream(new FileInputStream(src));
			bos = new BufferedOutputStream(new FileOutputStream(dest));
			
			byte[] b = new byte[1024];
			int len = 0;
			
			while((len = bis.read(b))  != -1) {
				bos.write(b, 0, len);
			}
			
			bos.flush();
			System.out.println(dest.getPath() + "复制完毕.");
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if(bis != null) {
				try {
					bis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(bos != null) {
				try {
					bos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
	}
	
}

猜你喜欢

转载自blog.csdn.net/qq_36194262/article/details/82962926