- 1.概念
- IO流用来处理设备之间的数据传输
- Java对数据的操作是通过流的方式
- Java用于操作流的类都在IO包中
- 流按流向分为两种:输入流,输出流。
- 流按操作类型分为两种:
- 字节流 : 字节流可以操作任何数据,因为在计算机中任何数据都是以字节的形式存储的
- 字符流 : 字符流只能操作纯字符数据,比较方便。
对于文件内容的操作主要分为两大类,其中,字符流有两个抽象类:Writer Reader.
其对应子类FileWriter和FileReader可实现文件的读写操作
BufferedWriter和BufferedReader能够提供缓冲区功能,用以提高效率
同样,字节流也有两个抽象类:InputStream OutputStream
其对应子类有FileInputStream和FileOutputStream实现文件读写
BufferedInputStream和BufferedOutputStream提供缓冲区功能
字符流
-
字符流的写入
1 import java.io.File; 2 import java.io.FileWriter; 3 import java.io.IOException; 4 5 public class Demo { 6 public static void main(String[] args ) { 7 8 //创建要操作的文件路径和名称 9 //其中,File.separator表示系统相关的分隔符,Linux下为:/ Windows下为:\ 10 String path = File.separator + "home" + File.separator + "siu" + 11 File.separator + "work" + File.separator + "demo.txt"; 12 13 //由于IO操作会抛出异常,因此在try语句块的外部定义FileWriter的引用 14 FileWriter w = null; 15 try { 16 //以path为路径创建一个新的FileWriter对象 17 //如果需要追加数据,而不是覆盖,则使用FileWriter(path,true)构造方法 18 w = new FileWriter(path); 19 20 //将字符串写入到流中, 表示换行想有好的 21 w.write("Nerxious is a good boy "); 22 //如果想马上看到写入效果,则需要调用w.flush()方法 23 w.flush(); 24 } catch (IOException e) { 25 e.printStackTrace(); 26 } finally { 27 //如果前面发生异常,那么是无法产生w对象的 28 //因此要做出判断,以免发生空指针异常 29 if(w != null) { 30 try { 31 //关闭流资源,需要再次捕捉异常 32 w.close(); 33 } catch (IOException e) { 34 e.printStackTrace(); 35 } 36 } 37 } 38 } 39 }
-
利用字符流的缓冲区来进行文本文件的复制
1 import java.io.BufferedReader; 2 import java.io.BufferedWriter; 3 import java.io.File; 4 import java.io.FileReader; 5 import java.io.FileWriter; 6 import java.io.IOException; 7 8 public class Demo { 9 public static void main(String[] args ) { 10 11 String doc = File.separator + "home" + File.separator + "siu" + 12 File.separator + "work" + File.separator + "demo.txt"; 13 14 String copy = File.separator + "home" + File.separator + "siu" + 15 File.separator + "life" + File.separator + "lrc.txt"; 16 17 FileReader r = null; 18 FileWriter w = null; 19 //创建缓冲区的引用 20 BufferedReader br = null; 21 BufferedWriter bw = null; 22 try { 23 r = new FileReader(doc); 24 w = new FileWriter(copy); 25 //创建缓冲区对象 26 //将需要提高效率的FileReader和FileWriter对象放入其构造函数内 27 //当然,也可以使用匿名对象的方式 br = new BufferedReader(new FileReader(doc)); 28 br = new BufferedReader(r); 29 bw = new BufferedWriter(w); 30 31 String line = null; 32 //读取行,直到返回null 33 //readLine()方法只返回换行符之前的数据 34 while((line = br.readLine()) != null) { 35 //使用BufferWriter对象的写入方法 36 bw.write(line); 37 //写完文件内容之后换行 38 //newLine()方法依据平台而定 39 //windows下的换行是 40 //Linux下则是 41 bw.newLine(); 42 } 43 44 } catch (IOException e) { 45 e.printStackTrace(); 46 } finally { 47 //此处不再需要捕捉FileReader和FileWriter对象的异常 48 //关闭缓冲区就是关闭缓冲区中的流对象 49 if(br != null) { 50 try { 51 r.close(); 52 } catch (IOException e) { 53 e.printStackTrace(); 54 } 55 } 56 if(bw != null) { 57 try { 58 bw.close(); 59 } catch (IOException e) { 60 e.printStackTrace(); 61 } 62 } 63 } 64 } 65 }
-
二进制文件的复制
-
1 import java.io.File; 2 import java.io.FileInputStream; 3 import java.io.FileOutputStream; 4 import java.io.IOException; 5 6 public class Demo { 7 public static void main(String[] args ) { 8 9 String bin = File.separator + "home" + File.separator + "siu" + 10 File.separator + "work" + File.separator + "一个人生活.mp3"; 11 12 String copy = File.separator + "home" + File.separator + "siu" + 13 File.separator + "life" + File.separator + "一个人生活.mp3"; 14 15 FileInputStream i = null; 16 FileOutputStream o = null; 17 18 try { 19 i = new FileInputStream(bin); 20 o = new FileOutputStream(copy); 21 22 //循环的方式读入写出文件,从而完成复制 23 byte[] buf = new byte[1024]; 24 int temp = 0; 25 while((temp = i.read(buf)) != -1) { 26 o.write(buf, 0, temp); 27 } 28 29 } catch (IOException e) { 30 e.printStackTrace(); 31 } finally { 32 if(i != null) { 33 try { 34 i.close(); 35 } catch (IOException e) { 36 e.printStackTrace(); 37 } 38 } 39 if(o != null) { 40 try { 41 o.close(); 42 } catch (IOException e) { 43 e.printStackTrace(); 44 } 45 } 46 } 47 } 48 }
-
对象流的操作:
-
1 public class ObjectInputStream { 2 3 /** 4 * @param args 5 * @throws IOException 6 * @throws ClassNotFoundException 7 * @throws FileNotFoundException 8 * 读取对象,反序列化 9 */ 10 public static void main(String[] args) throws IOException, ClassNotFoundException { 11 ObjectInputStream ois = new ObjectInputStream(new FileInputStream("e.txt")); 12 Person p1 = (Person) ois.readObject(); 13 Person p2 = (Person) ois.readObject(); 14 System.out.println(p1); 15 System.out.println(p2); 16 ois.close(); 17 } 18 19 }
-