按照数据流向的不一样:输入流和输出流数组
按照处理数据的单位不一样:字节流((非文本文件)视频、音频、图像)、字符流(文本文件)缓存
按照角色的不一样:节点流和处理流oop
抽象基类 节点流 缓冲流(处理流的一种,能够提升文本操做的效率)视频
InputStream FileInputStream BufferedInputStream对象
OutputStream FileOutputStream BufferedOutputStream资源
Reader FileReader BufferedReaderget
Writer FileWriter BufferedWriterit
public class FileInputOutputStream {
// 从硬盘中存在的文件,读取内容加载到程序中,FileInputStream
// 要读取的文件必定要存在,不然抛一个异常FileNotFoundException
@Test
public void testFileInputStream1() throws Exception{
// 一、建立一个File类的对象
File file = new File("hello.txt");
// 二、建立一个FileInputStream类的对象
FileInputStream fis = new FileInputStream(file);
// 三、调用FileInputStream中的方法,实现file文件的读取
// read()方法:能够读取文件中的一个字节 当执行到文件结尾时,返回-1
/* int b = fis.read();
// 判断有没有读取到文件结尾
while(b!=-1){
System.out.println((char)b);
b = fis.read();
}*/
int b;
while((b=fis.read())!=-1){
System.out.println((char)b);
}
// 四、关闭相应的流
fis.close();
}
// 使用try-catch-finally处理异常:保证流的关闭必定能够执行
@Test
public void testFileIntputStream2(){
File file = new File("hello.txt");
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
int b;
while((b=fis.read())!=-1){
System.out.println((char)b);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
// 无论是否发生异常,finally中的代码必定会执行
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 将读取到的数据填充到字节数组中
@Test
public void testFileInputStream3(){
FileInputStream fis = null;
try {
File file = new File("hello.txt");
// 将file对象做为FileInputStream的形参传进来
fis = new FileInputStream(file);
// 读取的数据要写入的数组
byte[] b = new byte[5];
// 每次读入到byte中字节的长度
int len;
while ((len=fis.read(b))!=-1) {
for (int i = 0; i < len; i++) {
System.out.print((char)b[i]);
}
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// FileOutputStream
@Test
public void testFileOutputStream(){
// 一、建立一个File对象,指定要写入的文件位置
// 输出的物理文件也能够不存在,当执行过程当中,若是不存在,则自动建立,若存在,则将原来的文件覆盖
File file = new File("hello2.txt");
// 二、建立一个FileOutputStream对象,将file对象做为形参传递给FileOutputStream的构造器。
FileOutputStream fos = null;
try {
fos=new FileOutputStream(file);
// 三、执行写入操做
fos.write(new String("hello").getBytes());
} catch (Exception e) {
e.printStackTrace();
}finally {
// 四、关闭输出流(释放资源)
if (fos!=null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
// 从硬盘读取一个文件并写入到另外一个位置(文件的复制)
@Test
public void testFileInputOutputStream(){
// 一、提供读入、写出的文件
File file1 = new File("hello.txt");
File file2 = new File("hello3.txt");
// 二、提供相应的输入流和输出流
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(file1);
fos = new FileOutputStream(file2);
byte[] b = new byte[20];
int len;
// 三、实现文件的复制
while ((len=fis.read(b))!=-1) {
System.out.println(len);
// 从头开始写 写的长度是len
fos.write(b, 0, len);
}
} 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();
}
}
}
}
// 实现文件复制的方法
public static void copyFile(String src,String dest){
// 一、提供读入、写出的文件
File file1 = new File(src);
File file2 = new File(dest);
// 二、提供相应的输入流和输出流
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(file1);
fos = new FileOutputStream(file2);
byte[] b = new byte[20];
int len;
// 三、实现文件的复制
while ((len=fis.read(b))!=-1) {
// 从头开始写 写的长度是len
fos.write(b, 0, len);
}
} 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();
}
}
}
}
@Test
public void testCopyFile(){
copyFile("1.jpg", "2.jpg");
}
}io
public class TestBuffered {
// 使用BufferedInputStream和BufferedOutputStream实现非文本复制
@Test
public void testBufferedInputOutputStream(){
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
// 1.提供读写的文件
File file1 = new File("1.jpg");
File file2 = new File("3.jpg");
// 2.建立响应的节点流 FileInputStream FileOutputStream
FileInputStream fis = new FileInputStream(file1);
FileOutputStream fos = new FileOutputStream(file2);
// 3.将建立的节点流对象做为形参传递给缓冲流的构造器
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
// 4.具体实现文件复制的操做
byte[] b = new byte[1024];
int len;
while ((len=bis.read(b))!=-1) {
// 写数据
bos.write(b, 0, len);
// 清除缓存
bos.flush();
}
} catch (Exception e) {
e.printStackTrace();
}finally {
// 5.释放资源
if (bis!=null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bos!=null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
public void testBufferReader(){
BufferedReader br = null;
BufferedWriter bw = null;
try {
File file = new File("oop.txt");
File file1 = new File("oop2.txt");
FileReader fr = new FileReader(file);
FileWriter fw = new FileWriter(file1);
br = new BufferedReader(fr);
bw = new BufferedWriter(fw);
/*char[] c = new char[1024];
int len;
while ((len=br.read(c))!=-1) {
String str = new String(c, 0, len);
bw.write(str);
bw.flush();
}*/
String str;
while ((str=br.readLine())!=null) {
bw.write(str);
bw.newLine();// 自动换行
bw.flush();
}
} catch (Exception e) {
e.printStackTrace();
}finally {
if (bw!=null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (br!=null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}class