课程学习--JavaSE--后端(第5期):文件流

流是一组有顺序的,有起点和终点的数据序列,是对数据传输的总称或抽象。

文件和目录

File,Java中操作文件和目录的类。

构造方法
File f = new File("D:\\hello.txt");

在构造一个File对象时,即使传入的文件或目录不存在,代码也不会出错,因为构造一个File对象,并不会导致任何磁盘操作。只有当我们调用File对象的某些方法的时候,才真正进行磁盘操作。

创建和删除文件
File file = new File("/path/to/file");
if (file.createNewFile()) {
    // 文件创建成功:
    // TODO:
    if (file.delete()) {
        // 删除文件成功:
    }
}	
遍历文件和目录
public class Main {
    public static void main(String[] args) throws IOException {
        File f = new File("C:\\Windows");
        
        // 列出所有文件和子目录
        File[] fs1 = f.listFiles(); 
        printFiles(fs1);
        
        // 仅列出.exe文件
        File[] fs2 = f.listFiles(
            // 实例化过滤器
            new FilenameFilter() {
                // 重写accept方法
                public boolean accept(File dir, String name) {
                    // 返回true表示接受该文件
                    return name.endsWith(".exe");
                }
            }
        );
        printFiles(fs2);
    }

    static void printFiles(File[] files) {
        System.out.println("==========");
        if (files != null) {
            for (File f : files) {
                System.out.println(f);
            }
        }
        System.out.println("==========");
    }
}

MultipartFile

public interface MultipartFile 
/**
 * A representation of an uploaded file received in a multipart request.
 * 在混合请求中收到上传文件时的一种表示形式。
 
 * The file contents are either stored in memory or temporarily on disk.
 * 文件内容可以存储在内存中,也可以临时存储在磁盘上。
 *
 * In either case, the user is responsible for copying file contents to a
 * 在这两种情况下,用户负责将文件内容
 *
 * session-level or persistent store as and if desired. 
 * 按需要复制到会话级或持久存储。
 *
 * The temporary storage will be cleared at the end of request processing.
 * 临时存储将在请求处理结束时清除。
 */

// 获取文件名
String getOriginalFilename();
// 获取字节流
InputStream getInputStream() throws IOException;

img

字节流
System.in

控制台输入的字节流。

InputStream

InputStream是一个抽象类。

// 从输入流中读取一些字节并将它们存储到缓冲区数组b。返回值是读取字节数组的长度。此方法将阻塞,直到输入数据可用、检测到文件结束或引发异常。
public int read(byte b[]) throws IOException

FileInputStreamInputStream的实现类,从文件流中读取数据。

// 构造函数
public FileInputStream(String name) throws FileNotFoundException
public FileInputStream(File file) throws FileNotFoundException
// 返回下一个将要读取byte的int值,如果读完则返回-1
public int read() throws IOException

OutputStream也是抽象类。

// 将指定字节数组中的b.length个字节写入此输出流。
public void write(byte b[]) throws IOException

FileOutputStreamOutputStream的实现类,将数据写入文件流中。

// 构造函数
public FileOutputStream(String name) throws FileNotFoundException
public FileOutputStream(File file) throws FileNotFoundException
字符流

Reader是一个抽象类。

// 方法读取字符流的下一个字符,并返回字符表示的int,范围是0~65535。如果已读到末尾,返回-1。
public int read() throws IOException

InputStreamReader

// 构造函数
public InputStreamReader(InputStream in, String charsetName)

FileReader是InputStreamReader的子类。

// 构造函数
public FileReader(File file) throws FileNotFoundException

Writer是一个抽象类。

public void write(int c) throws IOException
public void write(char cbuf[]) throws IOException
public void write(String str) throws IOException

OutputStreamWriter

// 构造函数
public OutputStreamWriter(OutputStream out, String charsetName)

Filewriter是OutputStreamWriter的子类。

// 构造函数
public FileWriter(File file) throws IOException
缓冲流(线程安全)
public BufferedInputStream(InputStream in)
public int read() throws IOException
public BufferedOutputStream(OutputStream out)
public synchronized void write(int b) throws IOException
public BufferedReader(Reader in)
public int read() throws IOException
public BufferedWriter(Writer out)
public void write(String str) throws IOException
abstract public void flush() throws IOException;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

charliejohn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值