第5章 I/O流

5.2 字节流

inputstream

方法声明 功能描述
int read() 从输入流中读取数据的下一个字节
int read( byte[] b) 从输入流中读取一定数量的字节,并将其存储在缓冲区数组b
int read(byte[] b,int off,int len) 中从输入流中读取若干字节,把它们保存到参数b指定的字节数组中,off指定字节数组开始保存数据的起始下标, len表示读取的字节数目
void close() 关闭此输入流并释放与该流关联的所有系统资源

outstream

方法声明 功能描述
void write(int b) 将指定的字节写入此输出流
void write(byte[] b) 将b. length个字节从指定的byte数组写入此输出流
void write(byte[] b,int off,int len) 将指定byte数组中从偏移量off开始的len个字节写入此输出流
void flush() 刷新此输出流并强制写出所有缓冲的输出字节
void close() 关闭此输出流并释放与此流有关的所有系统资源

读文件

package com.itheima.example;

import java.io.*;

public class example01 {
    public static void main(String[] args) throws Exception {
        FileInputStream input = new FileInputStream("src/com/itheima/example/itheima.txt");
        int i = 0;
        while (true) {
            i = input.read();
            if (i == -1) {
                break;
            }
            System.out.println(i);
        }
        input.close();
    }
}

写文件

package com.itheima.example;

import java.io.FileOutputStream;
import java.io.IOException;

public class Example02 {
    public static void main(String[] args) throws Exception {
        FileOutputStream output = null;
        try {
            output = new FileOutputStream("test.txt");
            String s = "www.itheima.com";
            byte[] arr = s.getBytes();
            for (int i = 0; i < arr.length; i++) {
                output.write(arr[i]);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            output.close();
        }

    }
}

5.2.3 文件的复制

package com.itheima.example;

import java.io.*;

public class Example03 {
    public static void main(String[] args) throws Exception {
        FileInputStream input = new FileInputStream("source//IMG_7799.jpg");
        FileOutputStream output = new FileOutputStream("target//IMG_7799.jpg");
        int length;
        long startTime = System.currentTimeMillis();
        while ((length = input.read()) != -1) {
            output.write(length);
        }
        long endTime = System.currentTimeMillis();
        System.out.println("复制图片所消耗的时间是:" + (endTime - startTime) + "毫秒");
        input.close();
        output.close();
    }
}

其中System.currentTimeMillis();是获取当前的系统时间

5.2.4 字节流的缓冲区

package com.itheima.example;

import java.io.*;

public class Example04 {
    public static void main(String[] args) throws Exception {
        FileInputStream input = new FileInputStream("source//IMG_7799.jpg");
        FileOutputStream output = new FileOutputStream("target//IMG_7799.jpg");
        byte[] buffer = new byte[1024]; //定义一个字节数组作为缓冲区
        int length;
        long startTime = System.currentTimeMillis();
        while ((length = input.read(buffer)) != -1) {
            output.write(buffer, 0, length);
        }
        long endTime = System.currentTimeMillis();
        System.out.println("复制图片所消耗的时间是:" + (endTime - startTime) + "毫秒");
        input.close();
        output.close();
    }
}

字节数组作为缓冲区可以有效提高读写效率

5.2.5 字节缓冲流

package com.itheima.example;

import java.io.*;

public class Example05 {
    public static void main(String[] args) throws Exception {
        // 创建一个带缓冲区的输入流
        BufferedInputStream bufferInput = new BufferedInputStream(new FileInputStream("source\\IMG_7799.jpg"));
        //创建一个带缓冲区的输出流
        BufferedOutputStream bufferOutput = new BufferedOutputStream(new FileOutputStream("target\\IMG_7799.jpg"));
        long startTime = System.currentTimeMillis();
        int length;
        while ((length = bufferInput.read()) != -1) {
            bufferOutput.write(length);
        }
        long endTime = System.currentTimeMillis();
        System.out.println("复制图片所消耗的时间是:" + (endTime - startTime) + "毫秒");
        bufferInput.close();
        bufferOutput.close();
    }
}

5.3 字符流

5.3.2 字符流的读写操作

package com.itheima.example;

import java.io.*;

public class Example06 {
    public static void main(String[] args) throws Exception {
        FileReader fileReader = new FileReader("test.txt");
        int i;
        while ((i = fileReader.read()) != -1) {
            System.out.print((char) i);
        }
        fileReader.close();
    }
}
package com.itheima.example;

import java.io.*;

public class Example07 {
    public static void main(String[] args) throws Exception {
        FileWriter fileWriter = new FileWriter("write.txt");
        String s = "HelloWorld";
        fileWriter.write(s);
        fileWriter.close();
    }
}

如果要追加字符的话:new FileWriter("write.txt", true);

5.3.3 字符缓冲流

package com.itheima.example;

import java.io.*;

public class Example08 {
    public static void main(String[] args) throws Exception {
        BufferedReader bufferReader = new BufferedReader(new FileReader("read.txt"));
        BufferedWriter bufferWriter = new BufferedWriter(new FileWriter("dest.txt"));
        String s = null;
        while ((s = bufferReader.readLine()) != null) {
            bufferWriter.write(s);
            bufferWriter.newLine();
        }
        bufferReader.close();
        bufferWriter.close();
    }
}

5.3.4 转换流

package com.itheima.example;

import java.io.*;

public class Example09 {
    public static void main(String[] args) throws Exception {
        FileInputStream input = new FileInputStream("test.txt");
        InputStreamReader streamReader = new InputStreamReader(input);
        BufferedReader bufferReader = new BufferedReader(streamReader);
        FileOutputStream output = new FileOutputStream("dest2.txt");
        OutputStreamWriter streamWriter = new OutputStreamWriter(output);
        BufferedWriter bufferWriter = new BufferedWriter(streamWriter);
        String line = null;
        while ((line = bufferReader.readLine()) != null) {
            bufferWriter.write(line);
        }
        bufferReader.close();
        bufferWriter.close();
    }
}

5.4 File类

file类用于封装一个路径,可以指向文件或目录

常用的构造方法:

方法声明 功能描述
File(String pathname) 通过将给定路径名字符串转换为抽象路径名创建一个新 File实例
File(String parent,String child) 根据parent路径名字符串和child路径名字符串创建一个新File实例
File(File parent,String child) 根据parent抽象路径名和 child路径名字符串创建一个新File实例

常用方法:

操作方法声明 功能描述
boolean exists() 判断File对象对应的文件或目录是否存在
boolean delete() 删除File对象对应的文件或目录
boolean createNewFile() 当File对象对应的文件不存在时,将创建一个此 File对象指定的新文件
String getName() 返回由此抽象路径名表示的文件或目录的名称
String getPath() 将此抽象路径名转换为一个路径名字符串
String getParent() 返回File对象对应目录的父目录(即返回的目录不包含最后一级子目录)
boolean isDirectory() 测试此抽象路径名表示的文件是否是一个目录
long lastModified() 返回此抽象路径名表示的文件最后一次被修改的时间
long length() 返回由此抽象路径名表示的文件的长度
String[] list() 列出指定目录的全部内容,仅列出名称
File[] listFiles() 返回一个包含了File对象所有子文件和子目录的File数组

5.4.2 遍历目录下的文件

package com.itheima.example;

import java.io.File;

public class Example10 {
    public static void main(String[] args) throws Exception {
        File f = new File("src\\com\\itheima\\example");
        if (f.isDirectory()) {
            String[] ss = f.list();
            for (String s : ss) {
                System.out.println(s);
            }
        }
    }
}

f.isDirectory()判断该路径是否存在

5.4.3 删除文件及目录

package com.itheima.example;

import java.io.*;

public class Example11 {
    public static void main(String[] args) throws Exception {
        File folder = new File("d:/hello");
        File[] files = folder.listFiles();
        for (File file : files) {
            if (file.getName().endsWith(".java")) {
                file.delete();
            }
        }
    }
}


标题:Python
作者:肖祺彦
地址:url