File类

概述

  • java.io.File类:文件和目录路径名的抽象表示形式,与平台无关
  • File 能新建、删除、重命名文件和目录,但 File 不能访问文件内容本身。如果需要访问文件内容本身,则需要使用输入/输出流
  • File对象可以作为参数传递给流的构造函数

常见的构造方法

  • public File(String pathname):以pathname为路径创建File对象,可以是绝对路径或者相对路径,如果pathname是相对路径,则默认的当前路径在系统属性user.dir中存储
  • public File(String parent,String child):以parent为父路径,child为子路径创建File对象

​ File的静态属性String separator存储了当前系统的路径分隔符。在UNIX中,此字段为‘/’,在Windows中,为‘\’

常见的方法

  • 访问文件名

    • getName()
    • getPath()
    • getAbsoluteFile()
    • getAbsolutePath()
    • getParent()
    • renameTo(File newName)
      • file1.renameTo(file2):要求:file1一定存在,file2一定不存在
  • 文件检测

    • exists()
    • canWrite()
    • canRead()
    • isFile()
    • isDirectory()
  • 获取常规文件信息

    • lastModified():最近修改时间
    • length()
  • 文件操作相关

    • createNewFile()
    • delete()
  • 目录操作相关

    • mkDir():创建一个文件目录。只有在上层文件目录存在的情况下,才能返回true
    • mkDirs():创建一个文件目录,若上层文件目录不存在,就一并创建
    • list():以字符串数组返回
    • listFiles():以文件对象返回

Java IO

概述

​ IO流用来处理设备之间的数据传输。Java程序中,对于数据的输入/输出操作以”流(stream)” 的方式进行。java.io包下提供了各种“流”类和接口,用以获取不同种类的数据,并通过标准的方法输入或输出数据。

  • 输入input:读取外部数据(磁盘、光盘等存储设备的数据)到程序(内存)中
  • 输出output:将程序(内存)数据输出到磁盘、光盘等存储设备中

流的分类

  • 按操作数据单位不同分为:字节流(8 bit),字符流(16 bit)
  • 按数据流的流向不同分为:输入流,输出流
  • 按流的角色的不同分为:节点流,处理流

补充:节点流和处理流

  • 节点流可以从一个特定的数据源读写数据
  • 处理流是“连接”在已存在的流(节点流或处理流)之上,通过对数据的处理为程序提供更为强大的读写功能

Java的IO流共涉及40多个类,实际上非常规则,都是从如下4个抽象基类派生的。由这四个类派生出来的子类名称都是以其父类名作为子类名后缀。

IO流体系

节点流

字节流

抽象基类一: InputStream

​ InputStream 和 Reader 是所有输入流的基类。从硬盘中的一个文件(存在),读取内容到程序中,使用FileInputStream。

  • InputStream(典型实现:FileInputStream

    • int read():读取文件的一个字节,当执行到文件结尾,返回-1
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    // 从硬盘中的一个文件(存在),读取内容到程序中,使用FileInputStream
    public static void FileInputStream() throws Exception {
    // 1、创建一个File类对象
    File file = new File("HelloWorld");
    // 2、创建一个FileInputStream类的对象
    FileInputStream fileInputStream = new FileInputStream(file);
    // 3、调用FileInputStream的方法,实现file文件的读取
    int b;
    while ((b = fileInputStream.read()) != -1){
    System.out.print((char)b);
    }
    // 4、关闭相应的流
    fileInputStream.close();
    }
    • int read(byte[] b)
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    public static void FileInputStream2() throws Exception {
    // 1、创建一个File类对象
    File file = new File("HelloWorld");
    // 2、创建一个FileInputStream类的对象
    FileInputStream fileInputStream = new FileInputStream(file);
    // 3、创建数组,将读取到的数据写入数组
    byte[] b = new byte[3];
    int len; //每次读入到byte中的字节的长度
    while ((len = fileInputStream.read(b)) != -1){
    String str = new String(b,0,len);
    System.out.print(str);
    }
    // 4、关闭相应的流
    fileInputStream.close();
    }
    • int read(byte[] b, int off, int len)
  • 程序中打开的文件 IO 资源不属于内存里的资源,垃圾回收机制无法回收该资源,所以应该显式关闭文件 IO 资源

抽象基类二: OutputStream

  • OutputStream 和 Writer 也非常相似

    • void write(int b/int c)
    • void write(byte[] b/char[] cbuf)
    • void write(byte[] b/char[] buff, int off, int len)
    • void flush()
    • void close():需要先刷新,再关闭此流
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    // 将程序中的内容写入到文件中,使用FileOutputStream
    public static void FileOutputStream() throws Exception {
    // 1、创建一个File类对象
    File file = new File("HelloWorld2");
    // 2、创建一个FileOutputStream类的对象
    FileOutputStream fileOutputStream = new FileOutputStream(file);
    // 3、将byte数组类型的内容进行写入
    fileOutputStream.write(new String("I love China!").getBytes());
    // 4、关闭相应的流
    fileOutputStream.close();
    }

如果文件不存在,则会自动创建该文件并写入,如果文件已经存在,将会覆盖其原有的内容。

练习: 文件的复制

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class Copy {
public static void main(String[] args) {
copyFile("/Users/liuwq/Desktop/1.jpg","/Users/liuwq/Desktop/2.jpg");
}

//实现文件的复制:从硬盘读取一个文件,并写入到另一个位置(相当于复制)
public static void copyFile(String src, String dest){
// 1、提供读入,写出的文件
File file = new File(src);
File file2 = new File(dest);
// 2、提供相应的流
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
try{
fileInputStream = new FileInputStream(file);
fileOutputStream = new FileOutputStream(file2);
// 3、实现文件的复制
byte[] b = new byte[20];
int len;
while ((len = fileInputStream.read(b)) != -1){
fileOutputStream.write(b,0,len);
}
}catch (Exception e){
e.printStackTrace();
}finally {
if (fileOutputStream != null){
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (fileInputStream != null){
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}
}

字符流

抽象基类三: Reader

​ InputStream 和 Reader 是所有输入流的基类。

  • Reader(典型实现:FileReader

    • int read()
    • int read(char [] c)
    • int read(char [] c, int off, int len)
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    public static void TestFileReader(){
    FileReader fileReader = null;
    try {
    File file = new File("HelloWorld2");
    fileReader = new FileReader(file);
    char[] c = new char[24];
    int len;
    while ((len = fileReader.read(c)) != -1){
    String str = new String(c,0,len);
    System.out.print(str);
    }
    }catch (Exception e){
    e.printStackTrace();
    }finally {
    if (fileReader != null){
    try {
    fileReader.close();
    }catch (Exception e){
    e.printStackTrace();
    }
    }
    }
    }

​ 程序中打开的文件 IO 资源不属于内存里的资源,垃圾回收机制无法回收该资源,所以应该显式关闭文件 IO 资源

抽象基类四: Writer

​ 因为字符流直接以字符作为操作单位,所以 Writer 可以用字符串来替换字符数组,即以 String 对象作为参数

  • void write(String str);

  • void write(String str, int off, int len);

练习: 文本的复制

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

public class Copy {
public static void main(String[] args) {
copyFile("/Users/liuwq/Desktop/1.txt","/Users/liuwq/Desktop/2.txt");
}

public static void copyFile(String src, String dest){
FileReader fileReader = null;
FileWriter fileWriter = null;
try {
File file = new File(src);
File file2 = new File(dest);
fileReader = new FileReader(file);
fileWriter = new FileWriter(file2);
char[] c = new char[24];
int len;
while ((len = fileReader.read(c)) != -1){
fileWriter.write(c,0,len);
}
}catch (Exception e){
e.printStackTrace();
}finally {
if (fileWriter != null){
try {
fileWriter.close();
}catch (Exception e){
e.printStackTrace();
}
}
if (fileReader != null){
try {
fileReader.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
}
}

处理流

缓冲流

​ 为了提高数据读写的速度,Java API提供了带缓冲功能的流类,在使用这些流类时,会创建一个内部缓冲区数组。

  • 根据数据操作单位可以把缓冲流分为:
    • BufferedInputStream 和 BufferedOutputStream
    • BufferedReader 和 BufferedWriter

缓冲流要“套接”在相应的节点流之上,对读写的数据提供了缓冲的功能,提高了读写的效率,同时增加了一些新的方法。

​ 对于输出的缓冲流,写出的数据会先在内存中缓存,使用flush()将会使内存中的数据立刻写出。

练习:实现非文本文件文件的复制,实现了加速

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import java.io.*;

public class TestBuffered {
public static void main(String[] args) {
copyFile("1.jpg","2.jpg");
}

//使用BufferedInputStream和BufferedOutputStream实现非文本文件的复制
public static void copyFile(String src, String dest){
BufferedInputStream bufferedInputStream = null;
BufferedOutputStream bufferedOutputStream = null;
try {
// 1、提供读入,写出的文件
File file = new File(src);
File file2 = new File(dest);
// 2、创建相应的节点流:FileInputStream和FileOutputStream
FileInputStream fileInputStream = new FileInputStream(file);
FileOutputStream fileOutputStream = new FileOutputStream(file2);
// 3、将创建的节点流对象作为形参传递给缓冲流的构造器中
bufferedInputStream = new BufferedInputStream(fileInputStream);
bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
// 4、具体实现文件复制的操作
byte[] b = new byte[1024];
int len;
while ((len = bufferedInputStream.read(b)) != -1){
bufferedOutputStream.write(b,0,len);
bufferedOutputStream.flush();
}
}catch (Exception e){
e.printStackTrace();
}finally {
// 5、关闭相应的流
if (bufferedOutputStream != null){
try {
bufferedOutputStream.close();
}catch (Exception e){
e.printStackTrace();
}
}
if (bufferedInputStream != null){
try {
bufferedInputStream.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
}
}

转换流

​ 转换流提供了在字节流和字符流之间的转换

  • Java API提供了两个转换流:
    • InputStreamReader
    • OutputStreamWriter
  • 字节流中的数据都是字符时,转成字符流操作更高效。
  • 编码:字符串—>字节数组
  • 解码:字节数组—>字符串
  • 转换流的编码应用:
    • 可以将字符按指定编码格式存储。
    • 可以对文本数据按指定编码格式来解读。
    • 指定编码表的动作由构造器完成。

InputStreamReader

用于将字节流中读取到的字节按指定字符集解码成字符。需要和InputStream“套接”。

  • 构造方法

    • public InputStreamReader(InputStream in)
    • public InputSreamReader(InputStream in, String charsetName)
    1
    Reader isr = new InputStreamReader(System.in, "ISO5334_1");

OutputStreamWriter

用于将要写入到字节流中的字符按指定字符集编码成字节。需要和OutputStream“套接”。

  • 构造方法
    • public OutputStreamWriter(OutputStream out)
    • public OutputSreamWriter(OutputStream out, String charsetName)

补充:字符编码

  • 编码表的由来
    • 计算机只能识别二进制数据,早期由来是电信号。为了方便应用计算机,让它可以识别各个国家的文字。就将各个国家的文字用数字来表示,并一一对应,形成一张表。这就是编码表。
  • 常见的编码表
    • ASCII:美国标准信息交换码。用一个字节的7位可以表示
    • ISO8859-1:拉丁码表。欧洲码表。用一个字节的8位表示
    • GB2312:中国的中文编码表
    • GBK:中国的中文编码表升级,融合了更多的中文文字符号
    • Unicode:国际标准码,融合了多种文字。
    • 所有文字都用两个字节来表示,Java语言使用的就是unicode
    • UTF-8:最多用三个字节来表示一个字符

标准输入输出流

概述

  • System.in 和 System.out 分别代表了系统标准的输入和输出设备
  • 默认输入设备是键盘,输出设备是显示器
  • System.in 的类型是InputStream
  • System.out 的类型是PrintStream,是OutputStream的子类FilterOutputStream 的子类
  • 通过System类的setIn,setOut方法对默认设备进行改变:
    • public static void setIn(InputStream in)
    • public static void setOut(PrintStream out)

练习

​ 从键盘输入字符串,要求将读取到的整行字符串转成大写输出。然后继续进行输入操作,直至当输入“e”或者“exit”时,退出程序。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Locale;

public class TestSystemInOut {
public static void main(String[] args) {
BufferedReader bufferedReader = null;
try {
InputStream inputStream = System.in;
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
bufferedReader = new BufferedReader(inputStreamReader);
String str;
while (true){
System.out.println("请输入字符串:");
str = bufferedReader.readLine();
if (str.equalsIgnoreCase("e") || str.equalsIgnoreCase("exit")){
break;
}
String str2 = str.toUpperCase(Locale.ROOT);
System.out.println(str2);
}
}catch (Exception e){
e.printStackTrace();
}finally {
if (bufferedReader != null){
try {
bufferedReader.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
}
}

打印流

​ 在整个IO包中,打印流是输出信息最方便的类。

  • PrintStream(字节打印流) 和 PrintWriter(字符打印流)
    • 提供了一系列重载的print和println方法,用于多种数据类型的输出
    • PrintStream和PrintWriter的输出不会抛出异常
    • PrintStream和PrintWriter有自动flush功能
    • System.out返回的是PrintStream的实例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 FileOutputStream fos = null;
try {
fos = new FileOutputStream(new File("text.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//创建打印输出流,设置为自动刷新模式(写入换行符或字节 '\n' 时都会刷新输出缓冲区)
PrintStream ps = new PrintStream(fos,true);
if (ps != null) { // 把标准输出流(控制台输出)改成文件
System.setOut(ps);
}
for (int i = 0; i <= 255; i++) { //输出ASCII字符
System.out.print((char)i);
if (i % 50 == 0) { //每50个数据一行
System.out.println(); // 换行
}
}
ps.close();

数据流

​ 为了方便地操作Java语言的基本数据类型的数据,可以使用数据流。

  • 数据流有两个类:(用于读取和写出基本数据类型的数据)

    • DataInputStreamDataOutputStream
    • 分别”套接”在 InputStream OutputStream 节点流上
  • DataInputStream中的方法:

    • boolean readBoolean()
    • byte readByte()
    • char readChar()
    • float readFloat()
    • double readDouble()
    • short readShort()
    • long readLong()
    • int readInt()
    • String readUTF()
    • void readFully(byte[] b)
  • DataOutputStream中的方法:

    • 将上述的方法的read改为相应的write即可

对象流

概述

  • ObjectInputStream
  • OjbectOutputSteam

​ 用于存储和读取对象的处理流。它的强大之处就是可以把Java中的对象写入到数据源中,也能把对象从数据源中还原回来。

  • 序列化(Serialize):用ObjectOutputStream类将一个Java对象写入IO流中
  • 反序列化(Deserialize):用ObjectInputStream类从IO流中恢复该Java对象

注意:ObjectOutputStream和ObjectInputStream不能序列化static和transient修饰的成员变量

对象的序列化

概述

对象序列化机制允许把内存中的Java对象转换成平台无关的二进制流,从而允许把这种二进制流持久地保存在磁盘上,或通过网络将这种二进制流传输到另一个网络节点。当其它程序获取了这种二进制流,就可以恢复成原来的Java对象。

​ 序列化的好处在于可将任何实现了Serializable接口的对象转化为字节数据,使其在保存和传输时可被还原。

​ 序列化是 RMI(Remote Method Invoke – 远程方法调用)过程的参数和返回值都必须实现的机制,而 RMI 是 JavaEE 的基础。因此序列化机制是 JavaEE 平台的基础

​ 如果需要让某个对象支持序列化机制,则必须让其类是可序列化的,为了让某个类是可序列化的,该类必须实现如下两个接口之一:

  • Serializable
  • Externalizable

​ 凡是实现Serializable接口的类都有一个表示序列化版本标识符的静态变量:

  • private static final long serialVersionUID;
  • serialVersionUID用来表明类的不同版本间的兼容性
  • 如果类没有显示定义这个静态变量,它的值是Java运行时环境根据类的内部细节自动生成的。若类的源代码作了修改,serialVersionUID 可能发生变化。故建议,显示声明

​ 显示定义serialVersionUID的用途:

  • 希望类的不同版本对序列化兼容,因此需确保类的不同版本具有相同的serialVersionUID
  • 不希望类的不同版本对序列化兼容,因此需确保类的不同版本具有不同的serialVersionUID

序列化过程示例

​ 若某个类实现了 Serializable 接口,该类的对象就是可序列化的:

  • 创建一个 ObjectOutputStream
  • 调用 ObjectOutputStream 对象的 writeObject(对象) 方法输出可序列化对象。注意写出一次,操作flush()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class TestObjectInputOutputStream {
public static void main(String[] args) {
Person person = new Person("小新",22);
ObjectOutputStream objectOutputStream = null;
try{
objectOutputStream = new ObjectOutputStream(new FileOutputStream("person.txt"));
objectOutputStream.writeObject(person);
objectOutputStream.flush();
}catch (Exception e){
e.printStackTrace();
}finally {
if (objectOutputStream != null){
try {
objectOutputStream.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
}
}

class Person implements Serializable {
String name;
Integer age;

public Person(String name, Integer age) {
this.name = name;
this.age = age;
}

@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}

反序列化过程示例

  • 创建一个 ObjectInputStream
  • 调用 readObject() 方法读取流中的对象
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import java.io.*;

public class TestObjectInputOutputStream {
public static void main(String[] args) {
Person person = new Person("小新",22);
// Serialization(person,"person.txt");
Deserialize("person.txt");
}

// 反序列化过程
public static void Deserialize(String src){
ObjectInputStream objectInputStream = null;
try {
objectInputStream = new ObjectInputStream(new FileInputStream(src));
Person p = (Person) objectInputStream.readObject();
System.out.println(p);
}catch (Exception e){
e.printStackTrace();
}finally {
if (objectInputStream != null){
try {
objectInputStream.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
}
}

随机存取文件流

RandomAccessFile

  • RandomAccessFile 类支持 “随机访问” 的方式,程序可以直接跳到文件的任意地方来读、写文件

    • 支持只访问文件的部分内容
    • 可以向已存在的文件后追加内容
  • RandomAccessFile 对象包含一个记录指针,用以标示当前读写处的位置。RandomAccessFile 类对象可以自由移动记录指针:

    • long getFilePointer():获取文件记录指针的当前位置
    • void seek(long pos):将文件记录指针定位到 pos 位置
  • 构造器

    • public RandomAccessFile(File file, String mode)
    • public RandomAccessFile(String name, String mode)
  • 创建 RandomAccessFile 类实例需要指定一个 mode 参数,该参数指定 RandomAccessFile 的访问模式:

    • r: 以只读方式打开
    • rw:打开以便读取和写入
    • rwd:打开以便读取和写入;同步文件内容的更新
    • rws:打开以便读取和写入;同步文件内容和元数据的更新

文件读写

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public static void RandomAccessFile(){
RandomAccessFile randomAccessFile = null;
RandomAccessFile randomAccessFile2 = null;
try {
randomAccessFile = new RandomAccessFile(new File("HelloWorld"),"r");
randomAccessFile2 = new RandomAccessFile(new File("HelloWorld1"),"rw");
byte[] b = new byte[20];
int len;
while ((len = randomAccessFile.read(b)) != -1){
randomAccessFile2.write(b,0,len);
}
}catch (Exception e){
e.printStackTrace();
}finally {
if (randomAccessFile2 != null){
try {
randomAccessFile2.close();
}catch (Exception e){
e.printStackTrace();
}
}
if (randomAccessFile != null){
try {
randomAccessFile.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
}

写入(覆盖)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// 任意位置写入,覆盖的效果
public static void Write(){
RandomAccessFile randomAccessFile = null;
try {
randomAccessFile = new RandomAccessFile(new File("HelloWorld"),"rw");
randomAccessFile.seek(3);
randomAccessFile.write("world!".getBytes());
}catch (Exception e){
e.printStackTrace();
}finally {
if (randomAccessFile != null){
try {
randomAccessFile.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
}

插入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
    // 实现插入的效果
public static void Write2(){
RandomAccessFile randomAccessFile = null;
try {
randomAccessFile = new RandomAccessFile(new File("HelloWorld"),"rw");
randomAccessFile.seek(1);
String str = randomAccessFile.readLine(); // llo
// long l = randomAccessFile.getFilePointer();
// System.out.println(l);
randomAccessFile.seek(1);
randomAccessFile.write("123".getBytes());
randomAccessFile.write(str.getBytes());
}catch (Exception e){
e.printStackTrace();
}finally {
if (randomAccessFile != null){
try {
randomAccessFile.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
}
  • 更具通用性:比如多行文本
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// 实现插入的效果(更具通用性)
public static void Write3(){
RandomAccessFile randomAccessFile = null;
try {
randomAccessFile = new RandomAccessFile(new File("HelloWorld"),"rw");
randomAccessFile.seek(1);
byte[] b = new byte[10];
int len;
StringBuffer sb = new StringBuffer();
while ((len = randomAccessFile.read(b)) != -1){
sb.append(new String(b,0,len));
}
randomAccessFile.seek(1);
randomAccessFile.write("123".getBytes());
randomAccessFile.write(sb.toString().getBytes());
}catch (Exception e){
e.printStackTrace();
}finally {
if (randomAccessFile != null){
try {
randomAccessFile.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
}

总结

​ 流是用来处理数据的。

​ 处理数据时,一定要先明确数据源,与数据目的地

  • 数据源可以是文件,可以是键盘。

  • 数据目的地可以是文件、显示器或者其他设备。

​ 而流只是在帮助数据进行传输,并对传输的数据进行处理,比如过滤处理、转换处理等。

  • 字节流—缓冲流(重点)

    • 输入流 InputStream—FileInputStream—BufferedInputStream
    • 输出流 OutputStream—FileOutputStream—BufferedOutputStream
  • 字符流—缓冲流(重点)

    • 输入流 Reader—FileReader—BufferedReader
    • 输出流 Writer—FileWriter—BufferedWriter
  • 转换流

  • InputSteamReader 和 OutputStreamWriter

  • 对象流 ObjectInputStream 和 ObjectOutputStream(难点)

    • 序列化
    • 反序列化
  • 随机存取流 RandomAccessFile(掌握读取、写入)

面试题

  • java中有几种类型的流?JDK为每种类型的流提供了一些抽象类以供继承,请说出他们分别是哪些类?
    答:字节流,字符流。
    字节流继承于InputStream OutputStream,字符流继承于Reader Writer。在java.io包中还有许多其他的流,主要是为了提高性能和使用方便。

  • 什么是java序列化,如何实现java序列化?
    答:序列化就是一种用来处理对象流的机制,所谓对象流也就是将对象的内容进行流化。可以对流化后的对象进行读写操作,也可将流化后的对象传输于网络之间。序列化是为了解决在对对象流进行读写操作时所引发的问题。
    序列化的实现:将需要被序列化的类实现Serializable接口,该接口没有需要实现的方法,implements Serializable只是为了标注该对象是可被序列化的,然后使用一个输出流(如:FileOutputStream)来构造一个ObjectOutputStream(对象流)对象,接着,使用ObjectOutputStream对象的writeObject(Object obj)方法就可以将参数为obj的对象写出(即保存其状态),要恢复的话则用输入流。

  • 在Java中,输入输出的处理需要引入的包是java.io,
    面向字节的输入输出类的基类是Inputstream和Outputstream。
    面向字符的输入输出类的基类是Reader和Writer。

  • 使用处理流的优势有哪些?如何识别所使用的流是处理流还是节点流?
    【优势】对开发人员来说,使用处理流进行输入/输出操作更简单;使用处理流的执行效率更高。
    【判别】处理流的构造器的参数不是一个物理节点,而是已经存在的流。而节点流都是直接以物理IO及节点作为构造器参数的。

  • Java中有几种类型的流?JDK为每种类型的流提供了一些抽象类以供继承,请指出它们分别是哪些类?
    【答案】Java中按所操作的数据单元的不同,分为字节流和字符流。字节流继承于InputStream和OutputStream类,字符流继承于Reader和Writer。
    按流的流向的不同,分为输入流和输出流
    按流的角色来分,可分为节点流和处理流。缓冲流、转换流、对象流和打印流等都属于处理流,使得输入/输出更简单,执行效率更高。

  • 什么是标准的I/O流?
    在java语言中,用stdin表示键盘,用stdout表示监视器。他们均被封装在System类的类变量 in 和 out 中,对应于系统调用System.in和System.out。这样的两个流加上System.err统称为标准流,它们是在System类中声明的3个类变量:
    public static InputStream in
    public static PrintStream out
    public static PrintStream err

  • 计算机处理的数据最终分解为▁▁的组合。
    A 0
    B 数据包
    C 字母
    D 1
  • 计算机处理的最小数据单元称为▁▁。
    A 位
    B 字节
    C 兆
    D 文件
  • 字母、数字和特殊符号称为▁▁。
    A 位
    B 字节
    C 字符
    D 文件
  • ▁▁文件流类的 close 方法可用于关闭文件。
    A FileOutputStream
    B FileInputStream
    C RandomAccessFile
    D FileWrite
  • RandomAccessFile 类的▁▁方法可用于从指定流上读取整数。
    A readInt
    B readLine
    C seek
    D close
  • RandomAccessFile 类的▁▁方法可用于从指定流上读取字符串。
    A readInt
    B readLine
    C seek
    D close
  • RandomAccessFile 类的▁▁方法可用于设置文件定位指针在文件中的位置。
    A readInt
    B readLiIne
    C seek
    D close
  • 在FilterOutputStream类的构造方法中,下面哪个类是合法:
    A File
    B InputStream
    C OutputStream
    D FileOutputStream