Java-9.14-next

概览

Java 的 I/O 大概可以分成以下几类:

  • 磁盘操作:File
  • 字节操作:InputStream 和 OutputStream
  • 字符操作:Reader 和 Writer
  • 对象操作:Serializable
  • 网络操作:Socket
  • 新的输入/输出:NIO

磁盘操作

File 类可以用于表示文件和目录的信息,但是它不表示文件的内容
递归地列出一个目录下所有文件:

public static void listAllFiles(File dir) {
    
    
    if (dir == null || !dir.exists()) {
    
    
        return;
    }
    if (dir.isFile()) {
    
    
        System.out.println(dir.getName());
        return;
    }
    for (File file : dir.listFiles()) {
    
    
        listAllFiles(file);
    }
}

从 Java7 开始,可以使用 Paths 和 Files 代替 File。

字节操作

实现文件复制

public static void copyFile(String src, String dist) throws IOException {
    
    
    FileInputStream in = new FileInputStream(src);
    FileOutputStream out = new FileOutputStream(dist);

    byte[] buffer = new byte[20 * 1024];
    int cnt;

    // read() 最多读取 buffer.length 个字节
    // 返回的是实际读取的个数
    // 返回 -1 的时候表示读到 eof,即文件尾
    while ((cnt = in.read(buffer, 0, buffer.length)) != -1) {
    
    
        out.write(buffer, 0, cnt);
    }

    in.close();
    out.close();
}

实验代码如下:


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

public class test_2 {
    
    
	public static void copyFile(String src, String dist){
    
    
//	    FileInputStream in = new FileInputStream(src);
//	    FileOutputStream out = new FileOutputStream(dist);
		
		File srcFile=new File(src);
		File distFile=new File(dist);
		FileInputStream in=null;
		FileOutputStream out=null;
		
		try {
    
    
			in=new FileInputStream(srcFile);
			out=new FileOutputStream(distFile);
			
			byte[] buffer = new byte[20 * 1024];
		    int cnt=-1;

		    // read() 最多读取 buffer.length 个字节
		    // 返回的是实际读取的个数
		    // 返回 -1 的时候表示读到 eof,即文件尾
		    while ((cnt = in.read(buffer, 0, buffer.length)) != -1) {
    
    
		        out.write(buffer, 0, cnt);
		    }
		    
		} catch (FileNotFoundException e) {
    
    
			e.printStackTrace();
		} catch (IOException e) {
    
    
			e.printStackTrace();
		}finally {
    
    
			try {
    
    
				if (in != null)
					in.close();
				if (out != null)
					out.close();
			} catch (IOException e) {
    
    
				e.printStackTrace();
			}
		}
	}
	
	public static void main(String[] args) {
    
    
		String src="E:\\证件照\\gh.jpg";
		String dist="E:\\新建文件夹\\gh.jpg";
		copyFile(src, dist);
		System.out.println("copy success!");
	}
}

需要说明的是,FileInputStream和FileOutputStream有两种初始化方法,查看源码如下:

public FileInputStream(String name) throws FileNotFoundException {
    
    
        this(name != null ? new File(name) : null);
    }
public FileInputStream(File file) throws FileNotFoundException {
    
    
        String name = (file != null ? file.getPath() : null);
        SecurityManager security = System.getSecurityManager();
        if (security != null) {
    
    
            security.checkRead(name);
        }
        if (name == null) {
    
    
            throw new NullPointerException();
        }
        if (file.isInvalid()) {
    
    
            throw new FileNotFoundException("Invalid file path");
        }
        fd = new FileDescriptor();
        fd.attach(this);
        path = name;
        open(name);
    }
public FileOutputStream(String name) throws FileNotFoundException {
    
    
        this(name != null ? new File(name) : null, false);
    }
public FileOutputStream(File file) throws FileNotFoundException {
    
    
        this(file, false);
    }

建议使用实验代码中使用的初始化函数,不使用注释的初始化函数。
另外,实验代码中的代码是读写字节的标准操作流程。

装饰者模式
Java I/O 使用了装饰者模式来实现。以 InputStream 为例,

  • InputStream 是抽象组件;
  • FileInputStream 是 InputStream 的子类,属于具体组件,提供了字节流的输入操作;
  • FilterInputStream 属于抽象装饰者,装饰者用于装饰组件,为组件提供额外的功能。例如 BufferedInputStream 为 FileInputStream 提供缓存的功能。

在这里插入图片描述
实例化一个具有缓存功能的字节流对象时,只需要在 FileInputStream 对象上再套一层 BufferedInputStream 对象即可。

FileInputStream fileInputStream = new FileInputStream(filePath);
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);

DataInputStream 装饰者提供了对更多数据类型进行输入的操作,比如 int、double 等基本类型。

猜你喜欢

转载自blog.csdn.net/Desperate_gh/article/details/109075774
今日推荐