java的IO流和文件读写

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Wengwuhua/article/details/84450857

IO流

  1. IO流分类
  • 按数据流向分:输入流、输出流
  • 按数据类型分:字节流(x xInputStream 读,xxOutputStream写)、字符流(xxRead读,xxWrite写)

如果数据所在的文件通过windows自带的记事本打开并能读懂里面的内容,就用字符流。其他用字节流。

文件操作

  1. File类的概述和方法介绍
  • File类的概述
    文件和目录路径名的抽象表示形式
  • 构造方法
    File(String pathname): 根据一个路径得到File对象
    File(String parent, String child): 根据一个目录和一个子文件/目录得到File对象
    File(File parent, String child): 根据一个父File对象和一个子文件/目录得到File对象
  • 创建功能
    public boolean createNewFile(): 创建文件 如果存在这样的文件,就不创建了
    public boolean mkdir(): 创建文件夹 如果存在这样的文件夹,就不创建了
    public boolean mkdirs(): 创建文件夹,如果父文件夹不存在,会帮你创建出来

    注意事项:如果你创建文件或者文件夹忘了写盘符路径,那么,默认在项目路径下。
  • 删除功能
    public boolean delete(): 删除文件或者文件夹
    注意事项:Java中的删除不走回收站。要删除一个文件夹,请注意该文件夹内不能包含文件或者文件夹
  • 重命名功能
    public boolean renameTo(File dest): 把文件重命名为指定的文件路径
    注意事项:如果路径名相同,就是改名。如果路径名不同,就是改名并剪切。
  • 判断功能
    public boolean isDirectory(): 判断是否是目录
    public boolean isFile(): 判断是否是文件
    public boolean exists(): 判断是否存在

    public boolean canRead(): 判断是否可读
    public boolean canWrite(): 判断是否可写
    public boolean isHidden(): 判断是否隐藏
  • 获取功能
    public String getAbsolutePath(): 获取绝对路径
    public String getPath(): 获取相对路径
    public String getName(): 获取名称

    public long length(): 获取长度。字节数
    public long lastModified(): 获取最后一次的修改时间,毫秒值
    public String[] list(): 获取指定目录下的所有文件或者文件夹的名称数组
    public File[] listFiles(): 获取指定目录下的所有文件或者文件夹的File数组

IO流读写文件

字节流读写文件

  1. 普通输入输出字节流读写
  • FileOutputStream三个write()方法
    public void write(int b):写一个字节。
    public void write(byte[] b):写一个字节数组。
    public void write(byte[] b,int off,int len):写一个字节数组的一部分。
  • FileInputStream两种读取方式
    int read(): 一次读取一个字节。
    int read(bytes[] b) : 一次读取一个字节数组。
  • 使用示例(复制文件)
    字节流一次读一个字节
private static void copyFile_1() throws IOException {   
        // 复制文件使用的时间是:89352毫秒
        // 创建对象
        FileInputStream fis = new FileInputStream("C:\\waiting for you.mp3") ;
        FileOutputStream fos = new FileOutputStream("D:\\waiting for you.mp3") ;
        // 一次读取一个字节
        int by = 0 ;
        while((by = fis.read()) != -1){
            fos.write(by) ;
        }
        // 释放资源
        fos.close() ;
        fis.close() ;
    }

一次读一个字节数组

private static void copyFile_2() throws IOException {       
        // 复制文件使用的时间是:154毫秒
        // 创建对象
        FileInputStream fis = new FileInputStream("C:\\waiting for you.mp3") ;
        FileOutputStream fos = new FileOutputStream("D:\\waiting for you.mp3") ;

        // 一次读取一个字节数组
        byte[] bytes = new byte[1024] ;
        int len = 0 ;
        while((len = fis.read(bytes)) != -1){
            fos.write(bytes, 0, len) ;
        }
        // 释放资源
        fos.close() ;
        fis.close() ;
    }
  1. 缓冲输入输出字节流读写
  • 相同方式使用缓冲流读写效率较高,常用的文件读写操作使用这种方式。(一次读取一个字节数组的效率远高于一次读一个字节)
  • BufferedInputStream读取数据,BufferedOutputStream写出数据
  • 使用示例
    一次读取一个字节
public static void copyFile_3() throws IOException {       
        // 复制文件使用的时间是:920毫秒
        // 创建高效流对象
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("C:\\waiting for you.mp3")) ;
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\waiting for you.mp3")) ;
        // 一次读取一个字节
        int by = 0 ;
        while((by = bis.read()) != -1){
            bos.write(by) ;
        }
        // 释放资源
        bos.close() ;
        bis.close() ;
    }

一次读取一个字节数组

public static void copyFile_4() throws IOException {       
        // 复制文件使用的时间是:53毫秒
        // 创建高效流对象
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("C:\\waiting for you.mp3")) ;
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\waiting for you.mp3")) ;
        // 一次读取一个字节数组
        byte[] bytes = new byte[1024] ;
        int len = 0 ;
        while((len = bis.read(bytes)) != -1){
            bos.write(bytes, 0, len) ;
        }
        // 释放资源
        bos.close() ;
        bis.close() ;
    }

字符流读写文件

  1. 字符流出现的原因:
    由于字节流操作中文不是特别方便,所以,java就提供了字符流。
    字符流: 字符流 = 字节流 + 编码表。
  2. 转换流(OutputStreamWriter、InputStreamReader)
public static void main(String[] args) throws IOException {
		// 创建转换输入流对象
		InputStreamReader isr = new InputStreamReader(new FileInputStream("OutputStreamWriterDemo.java")) ;
		// 创建转换输出流对象
		OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("copyFile.java")) ;
		// 复制文件
		// 一次读取一个字符复制
//		int ch = 0 ;
//		while((ch = isr.read()) != -1){
//			osw.write(ch) ;
//		}
		// 一次读取一个字符数组复制文件
		char[] chs = new char[1024] ;
		int len = 0 ;
		while((len = isr.read(chs)) != -1){
			osw.write(chs, 0, len) ;
		}
		// 释放资源
		osw.close() ;
		isr.close() ;
	}
  1. 高效流(FileWriter,FileReader)
public static void main(String[] args) throws IOException {
		// 创建高效的字符输入流对象
		BufferedReader br = new BufferedReader(new FileReader("OutputStreamWriterDemo.java")) ;
		// 创建高效的字符输出流对象
		BufferedWriter bw = new BufferedWriter(new FileWriter("copyFile3.java")) ;
		// 一次读取一个字符数组复制文件
		char[] chs = new char[1024] ;
		int len = 0;
		while((len = br.read(chs)) != -1){
			bw.write(chs, 0, len) ;
		} 
		// 释放资源
		bw.close() ;
		br.close() ;
	}
  1. 缓冲流(BufferedWriter ,BufferedReader)
public static void main(String[] args) throws IOException {
		/**
		 * 需求: 使用高效的字符流中特有的功能复制文本文件
		 */
		// 创建高效的字符输入流对象
		BufferedReader br = new BufferedReader(new FileReader("OutputStreamWriterDemo.java")) ;
		
		// 高效的字符输出流对象
		BufferedWriter bw = new BufferedWriter(new FileWriter("copyFile4.java")) ;
		
		// 复制文件
		// 一次读取一行复制文件
		String line = null ;
		while((line = br.readLine()) != null) {
			bw.write(line) ;
			bw.newLine() ;
			bw.flush() ;
		}
		// 释放资源
		bw.close() ;
		br.close() ;
	}
  1. 复制文件工具类
public class IOUtils {
	
			public static void copyFolder(String srcPahtName , String destPathName , FilenameFilter filenameFilter) throws IOException {
				File srcFolder = new File(srcPahtName) ;
				File destFolder = new File(destPathName) ;
				if(!destFolder.exists()) {
					destFolder.mkdir() ;
				}
				copyFolder(srcFolder , destFolder , filenameFilter) ;
			}
	
	public static void copyFolder(File srcFolder , File destFolder , FilenameFilter filenameFilter) throws IOException {
		File[] files = null ;
		if(filenameFilter == null) {
			files = srcFolder.listFiles() ;
		}else {
			files = srcFolder.listFiles(filenameFilter) ;
		}
		// 遍历
		for(File f : files) {
			// 创建目标文件
			String destFileName = f.getName() ;
			File destFile = new File(destFolder , destFileName) ; 
			// 复制文件
			copyFile(f , destFile) ;
		}
	} 
	
	public static void copyFile(File srcFile , File destFile) throws IOException {
		// 创建流对象
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile)) ;
		BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile)) ;
		
		// 一次读取一个字节数组复制文件
		byte[] bytes = new byte[1024] ;
		int len = 0 ;
		while((len = bis.read(bytes)) != -1){
			bos.write(bytes, 0, len) ;
		}
		// 释放资源
		bos.close() ;
		bis.close() ;
	}
}

IO流分类

  1. 按操作方式分类结构图

在这里插入图片描述
2. 按操作对象分类结构图

在这里插入图片描述

参考文章:https://github.com/yangchong211/YCBlogs/blob/master/java/IO流知识

猜你喜欢

转载自blog.csdn.net/Wengwuhua/article/details/84450857