IO概述
- 什么是IO?
- 通过IO可以完成对硬盘文件的读和写。
- I : Input
- O : Output

- IO流的分类?
- 有多种分类方式:
- 一种方式是按照流的方向进行分类:
- 以内存作为参照物,
- 往内存中去,叫做输入(input)。或者叫做读(Read)。
- 从内存中出来,叫做输出(Output)。或者叫做写(Write)。
- 另一种方式是按照数据方式不同进行分类:
- 有的流是按照字节的方式读取数据,一次读取1个字节byte,等同于一次读取8个二进制位。这种流是万能的,什么类型的文件都可以读取。包括:文本文件,图片,声音文件,视频等。
- 假设文件file1.txt,采用字节流的话是这样读的:
- a中国bc张三fe
- 第一次读:一个字节,正好读到’a’(注意:'a’在java中是2个字节,但在windows操作系统中是1个字节)
- 第二次读:一个字节,正好读到’中’字符的一半(注意:‘中’在java中是2个字节,在windows操作系统中也是2个字节)
- 第三次读:一个字节,正好读到’中’字符的另外一半。
- 有的流是按照字符的方式读取数据的,一次读取一个字符,这种流是为了方便读取普通文本文件而存在的,这种流不能读取:图片、声音、视频等文件。只能读取纯文本文件,连word文件都无法读取。
- 文件file1.txt,采用字符流的话是这样读的:
- a中国bc张三fe
- 第一次读:‘a’字符(‘a’字符在windows系统中占用1个字节。)
- 第二次读:‘中’字符(‘中’字符在windows系统中占用2个字节。)
- 综述所述,流的分类:
- Java中的IO流都已经写好了,我们程序员不需要关心,我们最主要该是掌握在java中已经提供了哪些流,每个流的特点是什么,每个流对象上的常用方法有哪些??
- java中所有的流都是在:java.io.*下
- java中主要还是研究:
- 怎么new流对象?
- 调用流对象的哪个方法是读,哪个方法是写。
- Java IO流这块有四大家族:
- java.io.InputStream 字节输入流
- java.io.OutputStream 字节输出流
- java.io.Reader 字符输入流
- java.io.Writer 字符输出流
- 四大家族的首领都是抽象类。(abstract class)
- 所有的流都实现了:
- java.io.Closeable接口,都是可关闭的,都有close()方法。
- 流毕竟是一个管道,这个是内存和硬盘之间的通道,用完之后一定要关闭,不然会耗费(占用)很多资源。养成好习惯,用完流一定要关闭。
- 所有的输出流都实现了:
- java.io.Flushable接口,都是可刷新的,都有flush()方法。养成一个好习惯,输出流在最终输出之后,一定要记得flush()刷新一下。这个刷新表示将通道/管道当中剩余未输出的数据强行输出完(清空管道!)刷新的作用就是清空管道。
- 注意:如果没有flush()可能会导致丢失数据。
- 注意:在java中只要“类名”以Stream结尾的都是字节流。以“Reader/Writer”结尾的都是字符流。
- java.io包下需要掌握的流有16个:
- 文件专属:
- java.io.FileInputStream(掌握)
- java.io.FileOutputStream(掌握)
- java.io.FileReader
- java.io.FileWriter
- 转换流:(将字节流转换成字符流)
- java.io.InputStreamReader
- java.io.OutputStreamWriter
- 缓冲流专属:
- java.io.BufferedReader
- java.io.BufferedWriter
- java.io.BufferedInputStream
- java.io.BufferedOutputStream
- 数据流专属:
- java.io.DataInputStream
- java.io.DataOutputStream
- 标准输出流:
- java.io.PrintWriter
- java.io.PrintStream(掌握)
- 对象专属流:
- java.io.ObjectInputStream(掌握)
- java.io.ObjectOutputStream(掌握)
FileInputStream的使用
package com.bjpowernode.javase.io;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class FileInputStreamTest01 {
public static void main(String[] args) {
FileInputStream fis = null;
try {
fis = new FileInputStream("D:/Java/Pros/IdeaPros/advanced_21/test.txt");
int readData = fis.read();
System.out.println(readData);
readData = fis.read();
System.out.println(readData);
readData = fis.read();
System.out.println(readData);
readData = fis.read();
System.out.println(readData);
readData = fis.read();
System.out.println(readData);
readData = fis.read();
System.out.println(readData);
readData = fis.read();
System.out.println(readData);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fis != null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
package com.bjpowernode.javase.io;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class FileInputStreamTest02 {
public static void main(String[] args) {
FileInputStream fis = null;
try {
fis = new FileInputStream("D:/Java/Pros/IdeaPros/advanced_21/test.txt");
int readData = 0;
while((readData = fis.read()) != -1){
System.out.println(readData);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fis != null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
package com.bjpowernode.javase.io;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class FileInputStreamTest03 {
public static void main(String[] args) {
FileInputStream fis = null;
try {
fis = new FileInputStream("io/src/com/bjpowernode/javase/io/test4.txt");
byte[] bytes = new byte[4];
int readCount = fis.read(bytes);
System.out.println(readCount);
System.out.println(new String(bytes, 0, readCount));
readCount = fis.read(bytes);
System.out.println(readCount);
System.out.println(new String(bytes, 0, readCount));
readCount = fis.read(bytes);
System.out.println(readCount);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fis != null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
package com.bjpowernode.javase.io;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class FileInputStreamTest04 {
public static void main(String[] args) {
FileInputStream fis = null;
try {
fis = new FileInputStream("io/src/com/bjpowernode/javase/io/temp.txt");
byte[] bytes = new byte[4];
int readCount = 0;
while((readCount = fis.read(bytes)) != -1){
System.out.print(new String(bytes, 0, readCount));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
if(fis != null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
package com.bjpowernode.javase.io;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class FileInputStreamTest05 {
public static void main(String[] args) {
FileInputStream fis = null;
try {
fis = new FileInputStream("io/src/com/bjpowernode/javase/io/test4.txt");
System.out.println("总字节数量:" + fis.available());
fis.skip(3);
System.out.println(fis.read());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
if(fis != null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
FileOutputStream使用
package com.bjpowernode.javase.io;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileOutputStreamTest01 {
public static void main(String[] args) {
FileOutputStream fos = null;
try {
fos = new FileOutputStream("io/src/com/bjpowernode/javase/io/temp.txt", true);
byte[] bytes = {97, 98, 99, 100};
fos.write(bytes);
fos.write(bytes, 0, 2);
String s = "我是一个中国人, 我骄傲!!!!!!";
byte[] bs = s.getBytes();
fos.write(bs);
fos.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fos != null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
文件复制
文件的复制原理

代码
package com.bjpowernode.javase.io;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyTest01 {
public static void main(String[] args) {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("D:\\Java\\Pros\\IdeaPros\\advanced_21\\videoRead\\秒剪辑.mp4");
fos = new FileOutputStream("D:\\Java\\Pros\\IdeaPros\\advanced_21\\videoWrite\\Copy秒剪辑.mp4");
byte[] bytes = new byte[1024 * 1024];
int readCount = 0;
while((readCount = fis.read(bytes)) != -1){
fos.write(bytes, 0, readCount);
}
fos.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
if(fos != null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fis != null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
FileReader
package com.bjpowernode.javase.io;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class FileReaderTest {
public static void main(String[] args) {
FileReader reader = null;
try {
reader = new FileReader("test.txt");
char[] chars = new char[4];
reader.read(chars);
for(char c : chars){
System.out.println(c);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(reader != null){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
FileWriter
package com.bjpowernode.javase.io;
import java.io.FileWriter;
import java.io.IOException;
public class FileWriterTest {
public static void main(String[] args) {
FileWriter fw = null;
try {
fw = new FileWriter("file", true);
char[] chars = {'我', '是', '中', '国', '人'};
fw.write(chars);
fw.write(chars, 2, 3);
fw.write("我是一名java软件工程师");
fw.write("\n");
fw.write("helloworld!");
fw.flush();
} catch (IOException e) {
e.printStackTrace();
} finally{
if (fw != null){
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
复制普通文本文件
package com.bjpowernode.javase.io;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class CopyTest02 {
public static void main(String[] args) {
FileReader in = null;
FileWriter out = null;
try {
in = new FileReader("io/src/com/bjpowernode/javase/io/CopyTest02.java");
out = new FileWriter("CopyTest2.java");
char[] chars = new char[1024 * 512];
int readCount = 0;
while((readCount = in.read(chars)) != -1){
out.write(chars, 0, readCount);
}
out.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(in != null){
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(out != null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
BufferedReader
package com.bjpowernode.javase.io;
import java.io.BufferedReader;
import java.io.FileReader;
public class BufferedReaderTest01 {
public static void main(String[] args) throws Exception{
FileReader reader = new FileReader("CopyTest2.java");
BufferedReader br = new BufferedReader(reader);
String s = null;
while((s = br.readLine()) != null){
System.out.println(s);
}
br.close();
}
}
节点流和包装流
package com.bjpowernode.javase.io;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
public class BufferedReaderTest02 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("CopyTest2.java")));
String line = null;
while((line = br.readLine()) != null){
System.out.println(line);
}
br.close();
}
}
BufferedWriter
package com.bjpowernode.javase.io;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class BufferedWriterTest {
public static void main(String[] args) throws Exception {
BufferedWriter out = new BufferedWriter(new FileWriter("copy", true));
out.write("hello world!");
out.write("\n");
out.write("hello kitty!");
out.flush();
out.close();
}
}