接着上一篇的 “Java 的 File 类” 的随笔,在File类的基础上,咱们就走进Java的IO流吧。java
流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象。即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输特性将流抽象为各类类,方便更直观的进行数据操做。数组
(1)、按照数据流向的不一样分为:输入流(从磁盘、网络等读取到内存,只能读,不能写)、输出流(从内存写出到磁盘、网络,只能写,不能读)网络
(2)、按照处理数据的单位不一样分为:字节流(以字节为基本操做单位)、字符流(以字符为基本操做单位)学习
(3)、按照角色的不一样分为:节点流(向某个IO设备/节点(磁盘文件、网络等)直接读写数据,也称为低级流)、处理流(用于包装一个已存在的流,经过这个已存在的流来进行读写操做,并不直接操做IO节点,也称为高级流、包装流)spa
分类code |
字节输入流视频 |
字节输出流对象 |
字符输入流blog |
字符输出流图片 |
抽象基类 |
InputStream |
OutputStream |
Reader |
Writer |
操做文件 |
FileInputStream |
FileOutputStream |
FileReader |
FileWriter |
操做数组 |
ByteArrayInputStream |
ByteArrayOutputStream |
CharArrayReader |
CharArrayWriter |
操做字符串 |
StringReader |
StringWriter |
||
缓冲流 |
BufferedInputStream |
BufferedOutputStream |
BufferedReader |
BufferedWriter |
转换流 |
InputStreamReader |
OutputStreamWriter |
||
对象流(用于序列化) |
ObjectInputStream |
ObjectOutputStream |
||
抽象基类(用于过滤) |
FilterInputStream |
FilterOutputStream |
FilterReader |
FilterWriter |
打印流(输出功能极其大) |
PrintStream |
PrintWriter |
InputStream是字节输入流的顶级父类,经常使用方法:
//一、建立一个File类的对象 File file = new File("hello.txt"); //二、建立一个FileInputStream类的对象 FileInputStream fis = new FileInputStream(file); //三、调用FileInputStream的方法,实现file文件的读取 int b = fis.read(); while(b != -1){ System.out.print((char)b+" "); b = fis.read(); } fis.close();
OutputStream是字节输出流的顶级父类,经常使用方法:
File file = new File("hello1.txt"); FileOutputStream fos = null; try{ fos = new FileOutputStream(file); fos.write(new String("\nI Love CYT!").getBytes()); }catch(Exception e){ e.printStackTrace(); }finally{ if(fos != null){ try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } }
Reader时字符输入流的顶级父类,经常使用方法:
File file = new File("1.txt"); FileReader fr = null; try{ fr = new FileReader(file); char[] c = new char[24]; int len; while((len = fr.read(c)) != -1){ for(int i=0;i<len;i++){ System.out.print(c[i]); } } }catch(Exception e){ e.printStackTrace(); }finally{ if(fr != null){ try { fr.close(); } catch (IOException e) { e.printStackTrace(); } } } }
Writer是字符输出流的顶级父类,经常使用方法:
能够用String代替char[],因此Writer还具备如下2个方法:
FileWriter fw = null; try{ fw = new FileWriter(new File("1.txt")); String str = "我喜欢Java,我要成为Java工程师。"; fw.write(str); }catch(Exception e){ e.printStackTrace(); }finally{ if(fw != null){ try { fw.close(); } catch (IOException e) { e.printStackTrace(); } } } }
缓冲流是和4级顶级父类对应的:加前缀Buffered
InputStream BufferedInputStream 字节输入缓冲流,可做为全部字节输入流类的缓冲流
OutputStream BufferedOutputStream 字节输出缓冲流
Reader BufferedReader 字符输入缓冲流
Writer BufferedWriter 字符输出缓冲流
//用 BufferedReader 把文件读进来, 再用 BufferedWriter 把内容写出去 File file = new File("hello.txt"); File file1 = new File("hello3.txt"); FileReader fr = null; BufferedReader br = null; FileWriter fw = null; BufferedWriter bw = null; try{ fr = new FileReader(file); fw = new FileWriter(file1); br = new BufferedReader(fr); bw = new BufferedWriter(fw); String str; while((str = br.readLine()) != null){ // System.out.println(str); bw.write(str); bw.newLine(); bw.flush(); } }catch(Exception e){ e.printStackTrace(); }finally{ if(bw != null){ try { bw.close(); } catch (IOException e) { e.printStackTrace(); } } if(br != null){ try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } }
其余方法我就不一一说明了。有兴趣的同窗能够本身再深刻研究。
最后我就提供文件复制的通用方法吧。本身写的通用类。让本身更好的去了解Java的IO流的使用。
package io; /* * 实现文件的复制 */ import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class CopyFile { // 字节文件的复制方法 public static void copyFileByte(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) { fos.write(b, 0, len); } } catch (Exception 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(); } } } } /* * 使用FileReader 、 FileWriter 能够实现文本文件的复制 * 对于非文本文件(视频文件、音频文件、图片),只能使用字节流复制。 */ public static void copyFileChar(String src, String dest) { // 一、提供读入、写出的文件 File file1 = new File(src); File file2 = new File(dest); // 二、提供相应的流 FileReader fr = null; FileWriter fw = null; try { fr = new FileReader(file1); fw = new FileWriter(file2); // 三、实现文件的复制 char[] c = new char[24]; int len; while ((len = fr.read(c)) != -1) { fw.write(c, 0, len); } } catch (Exception e) { e.printStackTrace(); } finally { if (fw != null) { try { fw.close(); } catch (IOException e) { e.printStackTrace(); } } if (fr != null) { try { fr.close(); } catch (IOException e) { e.printStackTrace(); } } } } //使用BufferedInputStream和BufferedOutputStream实现非文本文件的复制 public static void copyFileBuffered1(String src, String dest) { //一、提供读入、写出的文件 File file1 = new File(src); File file2 = new File(dest); //二、先建立相应的节点流:FileInputStream、 FileOutputStream FileInputStream fis = null; FileOutputStream fos = null; //三、再建立缓冲流:BufferedInputStream 、 BufferedOutputStream BufferedInputStream bis = null; BufferedOutputStream bos = null; try { fis = new FileInputStream(file1); fos = new FileOutputStream(file2); bis = new BufferedInputStream(fis); bos = new BufferedOutputStream(fos); //四、具体实现文件复制的操做 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 { if(bos != null){ try { bos.close(); } catch (IOException e) { e.printStackTrace(); } }if(bis != null){ try { bis.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
如今本身也准备大四了,因此本身要捉紧时间去学习,博客里面有什么问题的能够跟我说一下,你们一块儿交流一下学习的经验。