java.io包中定义了多个流类型(类或者抽象类)实现输入输出功能,从不同的角度进行分类:
1.按数据流的方向分为输入流和输出流
2.按处理数据单位不同分为字节流和字符流(常用GBK中,一个字符为两个字节,汉字为一个字符)
字节流 inputStream outputStream(Stream结尾都是字节流,最原始的输入输出)
字符流 Reader Writer
3.按功能不同可以分为节点流和处理流(节点流是从一个特定数据源读写数据,如文件,内存。处理流是“连接”在已存在
的流之上,通过对数据的处理为程序提供更加强大的读写功能)
1. inputStream
read() 读取一个字节并以整数形式返回(0~255),如果返回-1则表示已到输入流末尾
read(byte[] buffer) 读取一系列字节并存储到一个数组buffer,返回实际读取的字节数,如果读取前已到输出流的末尾返回-1
read(byte[] buffer,int offset,int length) 读取length个字节,并存储到一个字节数组buffer,从length位置开始。返回实际读取的字节数,如果读取前已到输入流末尾返回-1
close()关闭流释放内存资源
输出该文件,但由于InputStream是字节流,无法完整读取字符,所以输出时文件中“记事本”会乱码
public abstract class TestInput {
public static void main(String[] args) {
int b = 0;
FileInputStream in = null;
try {
in = new FileInputStream("D:\\Java\\xml\\src\\com\\xml\\记事本notepad0816\\TestInput.java");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
while((b = in.read())!=-1) {
System.out.print((char)b);//转换为字符打印
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2.outputStream
write(int b)
write(byte[] b)
write(byte[] b,int off,int len)
close()
flush()将输出流中缓冲的数据全部写出到目的地。close直接关闭太霸道,因此要在close之前使用
public static void main(String[] args) {
int b = 0;
FileInputStream in = null;
FileOutputStream out = null;
try {//复制文件
in = new FileInputStream("这里是读取文件地址");
out = new FileOutputStream("这里是输出文件地址");
while((b=in.read())!=-1) {
out.write(b);
}
in.close();
out.flush();
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
3.reader(方法和inputStream一样,但是读取的都是字符char)
read()
read(char[] cbuf)
read(char[] cbuf,int offset,int length)
close()
输出该文件,但由于Reader是字符流,可以完整读取字符,所以输出时文件中“记事本”中文不会乱码
public static void main(String[] args) {
FileReader in = null;
int c = 0;
try {
in = new FileReader("D:\\Java\\xml\\src\\com\\xml\\记事本notepad0816\\TestOutput.java");
while((c=in.read())!=-1) {
System.out.print((char)c);
}
in.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
4.writer(方法和outputStream一样,但是读取的都是字符char)
write(int c)
write(char[] cbuf)
write(char[] cbuf,int offset,int length)
write(String string)将一个字符串写入到输出流
write(String string,int offset,int length)将一个字符串从offset开始的length哥哥字符写入到输出流
flush()
close()
public static void main(String[] args) throws IOException {
FileWriter out = new FileWriter("写入文件地址");
for(int i = 0;i<1000;i++) {
try {
out.write(i);
out.flush();
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
5.缓冲流(属于处理流)
1.有效减少对硬盘的读写次数保护硬盘
2.输入输出速度快
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new FileWriter("写入文件地址"));
BufferedReader br = new BufferedReader(new FileReader("被复制写出文件的地址"));
String b =。。。
bw.write(b);
bw.flush();
bw.close();
br.close();
}
以上均是单字节单字符输入输出(可以看到,输入流中都是定义的int而不是byte[ ]),可以尝试数组写入,速度更快。
public static void main(String[] args) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("文件输出地址"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("文件输入地址"));
byte[] by = new byte[bis.available()];//缓存区字节组写入
int len = 0;
while((len=bis.read(by))!=-1) {
bos.write(by);
}
bis.close();
bos.flush();
bos.close();
}