流 IO
- 流与管道是抽象处理方便理解的概念,并不是真实存在的
- 流是有方向的,什么流就做什么事
in/输入流/读取流,方向:磁盘到内存,也就是"数据进到程序中"
out/输出流/写出流,方向:内存到磁盘,也就是"程序生成的数据存储到文件中" - 流的分类
根据方向:输入流 输出流
根据数据类型:字节流 字符流
组合:
1)字节输入流 InputStream–抽象父类,不可实例化
FileInputStream – 操作文件的字节输入流–可以传文件/路径
BufferedInputStream – 缓冲/高效字节输入流–需要传InputStream–FIS
例题:
package cn.tedu.file;
import java.io.*;
//测试字节流的读取
public class TestIn {
public static void main(String[] args) {
method();//普通字节流的读取
method2();//高效
}
private static void method2() {
BufferedInputStream in=null;
try {
in = new BufferedInputStream(new FileInputStream("D:\1.txt"));
int b;
while((b=in.read())!=-1){
System.out.println(b);
}
}catch (FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static void method() {
//InputStream in=new InputStream();抽象父类不可以创建对象,创建的是子类FIS的对象
//IO操作可能会发生异常,所以需要try——catch
InputStream in=null;
try {
//InputStream in=new FileInputStream("D:\\1.txt");
in=new FileInputStream(new File("D:\\1.txt"));
//read()一次读取一个字节,如果没有数据了,返回-1
System.out.println(in.read());
System.out.println(in.read());
System.out.println(in.read());
System.out.println(in.read());
int b;
while((b=in.read())!=-1){
System.out.println(b);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
2)字节输出流 OutputStream--抽象父类,不可实例化
FileOutputStream -- 操作文件的字节输出流--可以传文件/路径
BufferedOutputStream -- 缓冲/高效字节输出流--需要传OutputStream--FOS
例题:
package cn.tedu.file;
import java.io.*;
public class TestOut {
public static void main(String[] args) {
method();
method2();
}
private static void method() {
OutputStream out=null;
try{
//out=new FileOutputStream(new File("D:\\1.txt"));
out=new FileOutputStream("D:\\1.txt",true);
out.write(100);
out.write(100);
out.write(100);
out.write(100);
}catch (Exception e){
e.printStackTrace();
}finally{
try {
out.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
private static void method2() {
BufferedOutputStream out=null;
try{
out=new BufferedOutputStream(new FileOutputStream("D:\\1.txt",true));
out.write(103);
out.write(103);
out.write(103);
out.write(103);
}catch (Exception e){
e.printStackTrace();
}finally {
try{
out.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
}
3)字符输入流 Reader--抽象父类,不可实例化
FileReader -- 操作文件的字符输入流--可以传文件/路径
BufferedReader -- 缓冲/高效字符输入流--需要传Reader--FR
例题:
package cn.tedu.file;
import java.io.*;
//字符流的读取
public class TestIn2 {
public static void main(String[] args) {
method();//低效字符读取
method2();//高效字符读取
}
private static void method2() {
Reader in=null;
try{
in=new BufferedReader(new FileReader("D:\\1.txt"));
int b;
while((b=in.read())!=-1){
System.out.println(b);}
}catch (Exception e){
e.printStackTrace();
}finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static void method() {
//reader作为字符输入流的抽象父类,不可以实例化
//Reader in2=new Reader();
Reader in=null;
try{
//in=new FileReader("D:\\1.txt");
in=new FileReader(new File("D:\\1.txt"));
int b;
while((b=in.read())!=-1){
System.out.println(b);
}
}catch (Exception e){
e.printStackTrace();
}finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
4)字符输出流 Writer--抽象父类,不可以实例化
FileWriter --操作文件的字符输出流--可以传文件/路径
BufferedWriter --缓冲/高效字符输出流--需要传Writer--FW
例题:
package cn.tedu.file;
import java.io.*;
public class TestOut2 {
public static void main(String[] args) {
method();
method2();
}
private static void method2() {
BufferedWriter out=null;
try{
out=new BufferedWriter(new FileWriter("D:\\2.txt",true));
out.write(101);
out.write(101);
out.write(101);
out.write(101);
}catch (Exception e){
e.printStackTrace();
}finally {
try{
out.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
private static void method() {
Writer out=null;
try{
out=new FileWriter("D:\\2.txt",true);
out.write(100);
out.write(100);
out.write(100);
out.write(100);
}catch (Exception e){
e.printStackTrace();
}finally {
try{
out.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
}
- File–文件–封装的是路径:文件路径/文件夹路径/不存在的路径
注意:路径中不能出现单个\,是转义字符,必须成对出现
File相关的API–详见笔记中速查表与API手册