Java基础知识学习(八)

IO操作

5个重要的类分别是:InputStream、OutStream、Reader、Writer和File类

面向字符的输入输出流

输入流都是Reader的子类,

CharArrayReader 从字符数组读取的输入流
BufferedReader 缓冲输入字符流
PipedReader 输入管道
InputStreamReader 将字节转换到字符的输入流
FilterReader 过滤输入流
StringReader 从字符串读取的输入流
LineNumberReader 为输入数据附加行号
PushbackReader 返回一个字符并把此字节放回输入流
FileReader 从文件读取的输入流

 

使用 FileReader 类读取文件时,必须先调用 FileReader()构造方法创建 FileReader 类的对象,再调用 read()方法。FileReader 构造方法的格式为:
public FileReader(String name);  //根据文件名创建一个可读取的输入流对象

public static void main(String args[]) throws IOException{
        char a[] = new char[1000]; //创建可容纳 1000 个字符的数组
        FileReader fr = new FileReader(filePath);
        int num = fr.read(a); //将数据读入到数组 a 中,并返回字符数
        String str = new String(a,0,num); //将字符串数组转换成字符串
        System.out.println("读取的字符个数为:"+num+",内容为:
");
        System.out.println(str);
        fr.close();          //流需要关闭
}

BufferedReader 类是用来读取缓冲区中的数据。使用时必须创建 FileReader 类对象

    public static void main(String args[]) throws IOException {
        String line;
        try {
            FileReader fr = new FileReader(filePath);
            BufferedReader br = new BufferedReader(fr);
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
            br.close();
        } catch (IOException e) {
            System.out.println(e);
        }
    }

面向字符的输出流都是类 Writer 的子类

Reader类都有对应的Writer类

在使用 FileWriter 类写入文件时,必须先调用 FileWriter()构造方法创建 FileWriter 类的对象,再调用 writer()方法。

try {
    FileWriter a = new FileWriter(filePath, true);
    for (int i = 0; i < 100; i++) {
        a.write(i);
    }
    a.close();
} catch (IOException e) {
}

BufferedWriter 类是用来将数据写入到缓冲区。使用时必须创建 FileWriter 类对象,再以该对象为参数创建 BufferedWriter 类的对象,最后需要用 flush()方法将缓冲区清空。

调用 out 对象的 write()方法写入数据时,不会写入回车,因此需要使用newLine()方法在每行数据后加入回车,进行换行

public static void main(String args[]) throws IOException {
    String str = new String();
    try {
        BufferedReader in = new BufferedReader(new FileReader(filePath));
        BufferedWriter out = new BufferedWriter(new FileWriter(targetFile));
        while ((str = in.readLine()) != null) {
            System.out.println(str);
            out.write(str); // 将读取到的 1 行数据写入输出流
            out.newLine(); // 写入换行符
        }
        out.flush();
        in.close();
        out.close();
    } catch (IOException e) { }
}

面向字节的输入输出流

InputStream类和OutputStream类是所有字节流的父类

使用类似Reader和Writer类,区别是需要写入或读出字节

public static void main(String args[]) throws IOException {
    try {
        FileInputStream fis = new FileInputStream(new File(filePath));

        byte[] b = new byte[fis.available()];// 新建一个字节数组
        fis.read(b);// 将文件中的内容读取到字节数组中
        fis.close();
        String str2 = new String(b);// 再将字节数组中的内容转化成字符串形式输出
        System.out.println(str2);

        FileOutputStream fos = new FileOutputStream(new File(targetFile));
        fos.write(b);
        fos.close();
    } catch (Exception e) {
    }
}

标准输入输出

  1. 标准输入:标准输入 System.in 是 BufferedInputStream 类的对象,当程序需要从键盘上读入数据时,只需要调用 System.in 的 read()方法即可,该方法从键盘缓冲区读入一个字节的二进制数据,返回以此字节为低位字节,高位字节为 0 的整型数据。
  2. 标准输出:标准输出 System.out 是打印输出流 PrintStream 类的对象。PrintStream 类是过滤输出流类 FilterOutputStream 的一个子类,其中定义了向屏幕输出不同类型数据的方法print()和 println()。
  3. 标准错误输出:System.err 用于为用户显示错误信息,也是由 PrintStream 类派生出来的错误流。Err 流的作用是使 print()和 println()将信息输出到 err 流并显示在屏幕上,以方便用户使用和调试程序。

文件和目录

File类

每个 File 类对象表示一个磁盘文件或目录,其对象属性中包含了文件或目录的相关信息。通过调用 File 类提供的各种方法,能够创建、删除、重名名文件、判断文件的读写权限以及是否存在,设置和查询文件的最近修改时间等。

构造方法以及类方法列表:http://www.weixueyuan.net/view/6047.html

RandomAccessFile 类

使用这个类,可以跳转到文件的任意位置读写数据。程序可以在随机文件中插入数据,而不会破坏该文件的其他数据。此外,程序也可以更新或删除先前存储的数据,而不用重写整个文件。

long length() 返回文件长度
void seek(long pos) 移动文件位置指示器,pos 指定从文件开头的偏离字节数
int skipBytes(int n) 跳过 n 个字节,返回数为实际跳过的字节数
int read() 从文件中读取一个字节,字节的高 24 位为 0,若遇到文件结尾,返回-1
final byte readByte() 从文件中读取带符号的字节值
final char readChar() 从文件中读取一个 Unicode 字符
final void writeChar(inte c) 写入一个字符,两个字节

文件压缩

 Java.util.zip 包中提供了可对文件的压缩和解压缩进行处理的类,它们继承自字节流类OutputSteam 和 InputStream。其中 GZIPOutputStream 和 ZipOutputStream 可分别把数据压缩成 GZIP 和 Zip 格式,GZIPInpputStream 和 ZipInputStream 又可将压缩的数据进行还原。

つづけ

原文地址:https://www.cnblogs.com/alex09/p/4887864.html