IO流

来源:互联网 发布:淘宝开店一件代发流程 编辑:程序博客网 时间:2024/05/17 23:35

一、IO流简介

         java io流操作的类或接口File文件类InputStream字节输入流OutputStream字节输出流Reader字符输入流Write字符输出流


IO流的分类

  • 根据处理数据类型的不同分为:字符流和字节流
  • 根据数据流向不同分为:输入流和输出流

二、File类

       File类是对文件系统中文件以及文件夹进行封装的对象,可以通过对象的思想来操作文件和文件夹。 File类保存文件或目录的各种元数据信息,包括文件名、文件长度、最后修改时间、是否可读、获取当前文件的路径名,判断指定文件是否存在、获得当前目录中的文件列表,创建、删除文件和目录等方法。 


三、使用字符流和字节流读写数据(编码)----直接上代码。

     1、字符流---读和写

package com.zking.io;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;/** * 需求: * 指定一个文件: * 如果这个文件存在,则读取这个文件中的内容 *  如果这个文件不存在,则写内容到这个文件中 * @author Administrator * */public class TestFile {public static void main(String[] args) {File file=new File("test.txt");//判断 该文件是否存在if(!file.exists()){//不存在:创建文件 写内容System.out.println("该文件不存在,正在创建...");//创建文件try {boolean b=file.createNewFile();if(b){System.out.println("创建成功了...");//写内容FileWriter fw=new FileWriter(file);fw.write("写的内容");fw.close();}} catch (IOException e) {e.printStackTrace();}//file.mkdir();//创建文件夹}else{//存在:读取内容System.out.println("该文件存在,文件内容如下:");//读try {FileReader fr=new FileReader(file);char cbuf[]=new char[1024];int len=0;while((len=fr.read(cbuf))!=-1){//字符数组转成字符串String s=new String(cbuf, 0, len);System.out.println(s);}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}}}

   2、字节流--读写
package com.zking.io;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;/** * 需求: * 指定一个文件: * 如果这个文件存在,则读取这个文件中的内容 *  如果这个文件不存在,则写内容到这个文件中 *  用字节流 * @author Administrator * */public class TestFileByStream {public static void main(String[] args) {File file=new File("test2.txt");//判断 该文件是否存在if(!file.exists()){//不存在:创建文件 写内容System.out.println("该文件不存在,正在创建...");//创建文件try {boolean b=file.createNewFile();if(b){System.out.println("创建成功了...");//写内容(字节流 )FileOutputStream fos=new FileOutputStream(file);fos.write("写的内容".getBytes());fos.close();}} catch (IOException e) {e.printStackTrace();}//file.mkdir();//创建文件夹}else{//存在:读取内容System.out.println("该文件存在,文件内容如下:");//读try {FileInputStream fis=new FileInputStream(file);byte b[]=new byte[1024];int len=0;while((len=fis.read(b))!=-1){//将字节数组转成字符串String s=new String(b, 0, len);System.out.println(s);}} catch (Exception e) {e.printStackTrace();}}}}

四、转换流(字节流转成字符流)

   

  1. InputStreamReader:是字节流通向字符流的桥梁:它使用指定的 charset 读取字节并将其解码为字符。它使用的字符集可以由名称指定或显式给定,或者可以接受平台默认的字符集。
  2. OutputStreamWriter: 是字符流通向字节流的桥梁:可使用指定的 charset 将要写入流中的字符编码成字节。它使用的字符集可以由名称指定或显式给定,否则将接受平台默认的字符集。

这两个流对象是字符体系中的成员,它们有转换作用,本身又是字符流,所以在构造的时候需要传入字节流对象进来。



五、简单的文件复制的实现

      

package com.zking.io;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.RandomAccessFile;/** * 实现文件复制 * 1.来一个文件(字节流) * 2.读文件 * 3.写文件 * @author Administrator * */public class TestCopy {public static void main(String[] args) {File file=new File("使用字节流和字符流进行文件的读写和File操作.webm");File fileOut=new File("C:\\Users\\Administrator\\Desktop\\a");try {//文件字节输入流(读)FileInputStream fis=new FileInputStream(file);//文件字节缓冲输入流BufferedInputStream bis=new BufferedInputStream(fis,10);//文件字节输出流(写)FileOutputStream fos=new FileOutputStream(fileOut+"\\"+file.getName());//文件字节缓冲输出流BufferedOutputStream bos=new BufferedOutputStream(fos);int len=0;byte[] b=new byte[1024];//使用缓冲流while((len=bis.read(b))!=-1){bos.write(b, 0, len);}bos.close();bis.close();} catch (Exception e) {e.printStackTrace();}}}

六、序例化

     

  • 序列化: 将数据结构或对象转换成二进制串的过程
  • 反序列化:将在序列化过程中所生成的二进制串转换成数据结构或者对象的过程
try {//序列化//ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("user.exe"));//oos.writeObject(person);//oos.close();//反序列化(将存档从硬盘中读出来)ObjectInputStream ois=new ObjectInputStream(new FileInputStream("user.exe"));Person person2=(Person) ois.readObject();System.out.println(person2);ois.close();} catch (Exception e) {e.printStackTrace();}