I/O输入输出--其他流

一,节点流
/**
 * 字节数组,节点流
 * 数组的长度有限,数据量不会很大
 * 要求:文件内容不用太大
 * 1文件---->字节数组,从文件中读取到字节数组中,用read
 * 2字节数组--->文件,从字节数组中写入到另一个文件中,用write
 * 可以实现文件的拷贝
 * @author zjf-pc
 *
 */

package fjz;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.omg.CORBA.portable.OutputStream;
import org.omg.CORBA_2_3.portable.InputStream;

public class Demo008 {

    /**
     * 1,文件---程序---字节数组
     * 1)文件输入流       InputStream  read
     *   字节数组输出流   ByteArrayOutputStream
     *   
     *   
     *   2,字节数组---程序---文件
     *   1), 文件输出流   OutputStream  Write
     *      字节数组输入流  ByteArrayInputStream
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
         String srcPath = "e:/test/zjf.txt";
         byte[] data =  getBytesFromFile(srcPath);
         System.out.println(new String(data));
         getFileFromBytes(data);
         
    }
    /**
     * 文件---程序----字节数组  属于读
     * @param srcPath
     * @return
     * @throws IOException
     */
    public static byte[] getBytesFromFile(String srcPath) throws IOException
    {
        //创建文件源
        File src = new File(srcPath);
        //创建字节数组目的地
        byte[] dest = null;
        //选择流
        //文件输入流
        BufferedInputStream is = new BufferedInputStream(new FileInputStream(src));
        //字节数组输出流,不能使用多态
        ByteArrayOutputStream bos =  new ByteArrayOutputStream();
        //操作,不断读取文件,写出到字节数组中
        byte[] flush = new byte[1024];
        int len = 0;
        while(-1!=(len = is.read(flush)))
        {
            //写出到字节数组中
            bos.write(flush,0,len);
        }
        bos.flush();
        dest = bos.toByteArray();
        //bos.close();
        //is.close();
        FileUtil.close(bos,is);
        return dest;
        
    }
    /**
     * 字节数组----程序---文件
     * @throws IOException 
     */ 
    public static void getFileFromBytes(byte[] data) throws IOException
    {
        //创建文件目的地
        File dest = new File("e:/test/dir/hh.txt");
        //创建文件输出流
        BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(dest,false));
        os.write(data);
        os.flush();
        
    }
}


其中,可以使用类来关闭流

import java.io.Closeable;
import java.io.IOException;

public class FileUtil {
   /**
    * 工具类关闭流
    * 可变参数:...   只能形参最后一个位置,处理方式与数组一样
    * @param io
    */
    public static void close(Closeable ... io)
    {
        for(Closeable temp:io)
        {
            if(null!=temp)
            {
                try {
                    temp.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
}

二,数据输入/输出流
1,从文件读取数据

 /**
    * 从文件读取数据+类型
 * @throws IOException 
    */
   public static void read(String destPath) throws IOException
   {
       //创建源
       File src = new File(destPath);
       //选择流
       DataInputStream dis = new DataInputStream(
               new BufferedInputStream(
                      new FileInputStream(src)
                      )
               );
       //操作,读取的顺序与写出的一致,必须存在才能读取
      double point = dis.readDouble();
      long num = dis.readLong();
      String str = dis.readUTF();
      System.out.println(str);
     
      dis.close();
       
   }
   

2,数据+类型输出到文件

/**
     * 数据+类型输出到文件
     * @throws IOException 
     */
   public  static  void write(String destPath) throws IOException
   {
       double point = 2.5;
       long num = 100;
       String str = "数据类型";
       //创建源
       File dest = new File(destPath);
       //选择流  DataOutputStream
       DataOutputStream dos = new DataOutputStream(
               new BufferedOutputStream(
                       new FileOutputStream(dest)
                       )
               );
       dos.writeDouble(point);
       dos.writeLong(num);
       dos.writeUTF(str);
       dos.flush();
       dos.close();
   }

三,引用类型(对象)保留数据+类型
 * 反序列化   输入流   ObjectInputStreeam readObject() 从文件中读取
 * 序列化      输出流   ObjectOutputStream   writeObject() 写入到文件中
 * 先序列化后反序列化,反序列化必须与序列化一致
 * 不是所有的对象都可以序列化  java.io.Serializable(空接口)
 * 不是所有的属性都需要序列化 +transient

申明:将内容写入到文件中时,我们查看是不能看懂的,从文件中读取该内容到控制台才看以看出该内容

1,写入到文件中

public static void write(String destPath) throws FileNotFoundException, IOException
   {
       Enployee   emp = new Enployee("zjf",100000);
       int[] arr = {1,2,3,45};
       //创建源
        File dest = new File(destPath);
        //选择流  DataOutputStream
        ObjectOutputStream dos = new ObjectOutputStream(
                new BufferedOutputStream(
                        new FileOutputStream(dest)
                        )
                );
        dos.writeObject(emp);
        dos.writeObject(arr);
        dos.close();
    }

2,从文件中读取到控制台

public static void read(String destPath) throws IOException, ClassNotFoundException
       {
           //创建源
           File src = new File(destPath);
           //选择流
           ObjectInputStream dis = new ObjectInputStream(
                   new BufferedInputStream(
                          new FileInputStream(src)
                          )
                   );
           //操作,读取的顺序与写出的一致,必须存在才能读取
         Object obj =  dis.readObject();
         if((obj)instanceof Enployee)
         {
             Enployee emp = (Enployee) obj;
             System.out.println(emp.getName());
             System.out.println(emp.getSalary());
         }
         obj = dis.readObject();
         int[] arr = (int[]) obj;
         System.out.println(Arrays.toString(arr));
         
          dis.close();
           
       }

三,打印流

* 三个常量
 * 1,System.in 输入流  键盘输入
 * 2,system.out  输出流  控制台输出
 *
 *
 * 重定向
 * setIn();
 * setOut();
 * setErr();

//重定向  --->比如setOut,不再输出到控制台,输出到文件
           System.setOut(new PrintStream(
                   new BufferedOutputStream(
                   new FileOutputStream("e:/test/print.txt")
                   )
                   ,true)
                  );
           System.out.println("dshgaskaghdsfjkghdsfkj");
 //回到控制台输出
           System.setOut(new PrintStream(
                   new BufferedOutputStream(
                   new FileOutputStream(FileDescriptor.out)
                   )
                   ,true)
                  );
           System.out.println("back.....");

 

posted on 2016-10-30 15:18  我想静静_zjf  阅读(188)  评论(0编辑  收藏  举报

导航