流的概念
Java中所有的I/O都是用流来实现的,可以将流理解为连接到数据目标或源的管道,可以通过连接到源的流从源中读取数据,或通过连接到目标的流流向目标中写入数据。
Java中的流分为两种,一种字节流,另一种字符流,分别由四个抽象类来表示(每种流包括输入和输出两种):
1、InputStream
2、OutputStream
3、Reader
4、Writer
Java中其他多种多样的流都是由上述的流派生出来的。


Java IO包
Java IO包总结为五个类一个接口。
1、File(文件类)
2、InputStream(字节输入流的超类,抽象类)
3、OutputStream(字节输出流的超类,抽象类)
4、Reader(字符流输入流的超类,抽象类)
5、Writer(字符流输出流的超类,抽象类)
6、Serializable接口(实现序列化用的)
## File类的常用方法 ##
import java.io.File;
import java.io.IOException;
/*方法: File(String pathname)
* =》通过将给定路径名字符串转换为抽象路径名来创建一个新 File 实例。
* 提醒:文件和目录不能同时创建
* */
public class FileDemo1 {
public static void main(String[] args) {
File file1 = new File("F:\\filedemo\\file");
//单层目录用 mkdir()
//生成多层目录 mkdirs()
file1.mkdirs(); //此处创建了多层目录
//生成文件操作
file1 = new File("F:\\filedemo\\file\\hehe.txt");
try {
file1.createNewFile();//仅仅用于创建文件,不能创建目录
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.File;
import java.io.IOException;
/*方法: File(File parent, String child)
* =》根据 parent 抽象路径名和 child 路径名字符串创建一个新 File 实例。
* 提醒:文件和目录不能同时创建
* */
public class FileDemo2 {
public static void main(String[] args) {
//创建 F:\\filedemo\\hehe.txt
//首先创建目录 F:\\filedemo
//最后创建文件 hehe.txt
File parent = new File("F:\\filedemo\\file");
File child = new File(parent,"hehe.txt");
try{
if(!parent.exists()){ //判断是否父目录是否存在
parent.mkdirs(); //一次性创建多个目录
child.createNewFile(); //创建子文件
System.out.println("文件创建成功!");
}else{
child.createNewFile();
System.out.println("文件创建成功!");
}
}catch (IOException e) {
e.printStackTrace();
}
}
}
File其它方法不在概述,均在API文档中可以查到!
字节流
概述:字节流是以字节为单位,包括所有的可以存放的信息都是以字节为单位。字节流可以处理任何任意的文件类型,包括字符文件。
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/*
*文件流的读取拷贝操作
*/
public class FileDemo1 {
public static void main(String[] args) throws IOException {
FileInputStream in = null;
FileOutputStream out = null;
try {
//构造输入流
in = new FileInputStream("F:\\filedemo\\file\\haha.txt");
//构造输出流
out = new FileOutputStream("F:\\filedemo\\file\\heihei.txt");
//复制字节流操作
byte[] b = new byte[512];
int len = 0;
// read()只读单字节中文无法复制 ; read(byte[])可以正常读取中文
while((len=in.read(b))!=-1){
out.write(b, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
}finally{
//关闭输入流
if(in!=null){
in.close();
}
//关闭输出流
if(out!=null){
out.close();
}
}
}
}
其中带缓冲区的 BufferedInputStream 和 BufferedOutputStream 与上述方法类似。
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(File));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(File));
字符流
着重列一下常用的字符流的几种使用方式
import java.io.*;
public class TestStream {
public static void main(String[] args) {
TestStream test = new TestStream();
test.demo1();
test.demo2();
test.demo3();
}
public void demo1() {
InputStreamReader isr = null;
OutputStreamWriter osw = null;
try {
isr = new InputStreamReader(new FileInputStream("F:\\filedemo\\file\\haha.txt"));
osw = new OutputStreamWriter(new FileOutputStream("F:\\filedemo\\file\\hhhh.txt"));
char[] ch = new char[512]; //缓冲区
int len = 0;
while ((len = isr.read(ch)) != -1) {
System.out.println(new String(ch));
osw.write(ch, 0, len);
}
System.out.println("成功输入输出");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (isr != null)
isr.close();
if (osw != null)
osw.close();
} catch (IOException e2) {
e2.printStackTrace();
}
}
}
public void demo2() {
try (BufferedReader br = new BufferedReader(
new InputStreamReader(new FileInputStream("F:\\filedemo\\file\\haha.txt")));
BufferedWriter bw = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream("F:\\filedemo\\file\\jjjj.txt")))) {
char[] ch = new char[512]; //缓冲区
int len = 0;
while ((len = br.read(ch)) != -1) {
System.out.println(new String(ch));
bw.write(len);
}
System.out.println("成功输入输出");
bw.flush(); //刷新该流的缓冲
} catch (IOException e) {
}
}
public void demo3() {
try (BufferedWriter bw = new BufferedWriter(new FileWriter("F:\\filedemo\\file\\kkkk.txt"))) {
String str = "你好,世界!";
bw.write(str);
System.out.println("成功输入输出");
bw.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
字节流与字符流的区别(理解这个很重要)
(1)字节流在操作时本身不会用到缓冲区(内存),是文件本身直接操作的,而字符流在操作时使用了缓冲区,通过缓冲区再操作文件。
(2)字符流只能处理字符文件,而字节流既可以处理字节文件也可以处理字符文件。