IO流
什么是IO流
一个流可以理解为一个数据的序列,IO流(输入输出流)就是一种用来处理设备之间数据传输的流,Java中对数据的操作就是通过流来实现的。
IO流的分类
字节流
InputStream 输入流 OutPutStream 输出流
- 继承关系:
字节输出流
构造方法 | 功能 |
---|
FileOutputStream(File file) | 创建一个向指定File文件中写入数据的文件输出流 |
FileOutputStream(String name) | 创建一个向具有指定名称的文件中写入数据的文件输出流 |
File file = new File("a.txt");
FileOutputStream out = new FileOutputStream(file);
FileOutputStream fileOutputStream = new FileOutputStream("b.txt");
out.close();
fileOutputStream.close();
字节输入流
构造方法 | 功能 |
---|
FileInputStream(String name) | 通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中的路径名 name 指定 |
FileInputStream(File file) | 通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中的 File 对象 file 指定 |
File file = new File("e.txt");
FileInputStream in = new FileInputStream(file);
FileInputStream in2 = new FileInputStream("e.txt");
out.close();
fileOutputStream.close();
高效字节流
构造方法 | 功能 |
---|
BufferedInputStream(InputStream in) | 创建一个BufferedInputStream并保存其参数 |
BufferedOutputStream(OutputStream out) | 创建一个新的缓冲输出流,以将数据写入指定的底层输出流 |
字节流复制文件
- 通过字节流一次读写一个字符:
public class 字节流复制 {
public static void main(String[] args) throws IOException {
FileInputStream in = new FileInputStream(new File("d:\\\\video01.avi"));
FileOutputStream out = new FileOutputStream(new File("d:\\\\video02.avi"));
int len=0;
while ((len=in.read())!=-1){
out.write(len);
out.flush();
}
in.close();
out.close();
}
}
- 通过字节流一次读写一个字符数组:
public class 字节流复制 {
public static void main(String[] args) throws IOException {
FileInputStream in = new FileInputStream(new File("d:\\\\video01.avi"));
FileOutputStream out = new FileOutputStream(new File("d:\\\\video02.avi"));
int len=0;
byte[] bytes = new byte[1024 * 8];
while ((len=in.read(bytes))!=-1){
out.write(bytes,0,len);
out.flush();
}
in.close();
out.close();
}
}
- 通过高效字节流:
public class 一次读写一个字符 {
public static void main(String[] args) throws IOException {
BufferedInputStream in = new BufferedInputStream(new FileInputStream(new File("d:\\\\video01.avi")));
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File("d:\\\\video02.avi")));
int len=0;
while ((len=in.read())!=-1){
out.write(len);
out.flush();
}
in.close();
out.close();
}
}
public class 一次读写一个字符数组 {
public static void main(String[] args) throws IOException {
BufferedInputStream in = new BufferedInputStream(new FileInputStream(new File("d:\\\\video01.avi")));
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File("d:\\\\video02.avi")));
int len=0;
byte[] bytes = new byte[1024 * 8];
while ((len=in.read(bytes))!=-1){
out.write(bytes,0,len);
out.flush();
}
in.close();
out.close();
}
}
字符流
Reader 输入流 Writer 输出流
- 继承关系:
字符转换流
OutputStreamWriter
: 是字符流通向字节流的桥梁,可使用指定的charset将要写入流中的字符编码成字节。
InputStreamReader
:是字节流通向字符流的桥梁:它使用指定的charset 读取字节并将其解码为字符。
构造方法 | 功能 |
---|
InputStreamReader(InputStream in) | 创建一个使用默认字符集的 InputStreamReader |
OutputStreamWriter(OutputStream out) | 创建使用默认字符编码的 OutputStreamWriter |
public class 字符流复制 {
public static void main(String[] args) throws IOException {
InputStreamReader in = new InputStreamReader(new FileInputStream("D:\\IO流.txt"));
OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream("E:\\IO流.txt"));
int len=0;
char[] chars = new char[1024];
while ((len=in.read(chars))!=-1){
out.write(chars,0,len);
out.flush();
}
in.close();
out.close();
}
}
高效字符流
构造方法 | 功能 |
---|
BufferedWriter(Writer out) | 创建一个使用默认大小输出缓冲区的缓冲字符输出流 |
BufferedReader(Reader in) | 创建一个使用默认大小输入缓冲区的缓冲字符输入流 |
特有方法 | 功能 |
---|
void newLine() | 写入一个行换行符 |
String readLine() | 一次读取一行数据 |
public class 高效字符流复制 {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader("Homy.java"));
BufferedWriter writer = new BufferedWriter(new FileWriter("Homy1.java"));
String line=null;
while ((line=reader.readLine())!=null){
writer.write(line);
writer.newLine();
writer.flush();
}
reader.close();
writer.close();
}
}