zoukankan      html  css  js  c++  java
  • java笔记之IO详解——输出字节流

    输出字节流:

    --------| OutputStream 是所有输出字节流 的父类。 抽象类
    -----------| FileOutStream 向文件输出数据的输出字节流。

    FileOutputStream如何使用呢?
    1. 找到目标文件
    2. 建立数据的输出通道。
    3. 把数据转换成字节数组写出。
    4. 关闭资源

    方法一:

    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    public class outstream {
    
        public static void main(String[] args) throws IOException {
                writetest();
                System.out.println("写入完成了,,,,,,");
        }
        public static void writetest() throws IOException{
            File file=new File("c:\a.txt");
            FileOutputStream fileOutputStream=new FileOutputStream(file);
            fileOutputStream.write('h');
            fileOutputStream.write('e');
            fileOutputStream.write('l');
            fileOutputStream.write('l');
            fileOutputStream.write('o');
            fileOutputStream.close();
        }
    
    }

    此时将hello写入a.txt文件中。

    每次只能将一个字符写入,比较麻烦。

    方法二:

        //使用字节数组把数据写出。
            public static void writeTest2() throws IOException{
                //找到目标文件
                File file = new File("F:\b.txt");
                //建立数据输出通道
                FileOutputStream fileOutputStream = new FileOutputStream(file,true);
                //把数据写出。
                String data = "
    hello world";
                fileOutputStream.write(data.getBytes());
                //关闭资源
                fileOutputStream.close();
            }

    FileOutputStream要注意的细节:
    1. 使用FileOutputStream 的时候,如果目标文件不存在,那么会自动创建目标文件对象。
    2. 使用FileOutputStream写数据的时候,如果目标文件已经存在,那么会先清空目标文件中的数据,然后再写入数据。
    3.使用FileOutputStream写数据的时候, 如果目标文件已经存在,需要在原来数据基础上追加数据的时候应该使用new FileOutputStream(file,true)构造函数,第二参数为true。
    4.使用FileOutputStream的write方法写数据的时候,虽然接收的是一个int类型的数据,但是真正写出的只是一个字节的数据,只是
    把低八位的二进制数据写出,其他二十四位数据全部丢弃。
    00000000-000000000-00000001-11111111 511
    11111111---> -1

     练习:将文件拷贝至其他路径下:

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    /*
    需求: 拷贝一张图片。
    */
    public class CopyImage {
    
        public static void main(String[] args) throws IOException {
            //找到目标文件
            File inFile = new File("F:\美女\1.jpg");
            File destFile = new File("E:\1.jpg");
            //建立数据的输入输出通道
            FileInputStream fileInputStream = new  FileInputStream(inFile);
            FileOutputStream fileOutputStream = new FileOutputStream(destFile); //追加数据....
            //每新创建一个FileOutputStream的时候,默认情况下FileOutputStream 的指针是指向了文件的开始的位置。 每写出一次,指向都会出现相应移动。
            //建立缓冲数据,边读边写
            byte[] buf = new byte[1024]; 
            int length = 0 ; 
            while((length = fileInputStream.read(buf))!=-1){ //最后一次只剩下了824个字节
                fileOutputStream.write(buf,0,length); //写出很多次数据,所以就必须要追加。
            }
            //关闭资源 原则: 先开后关,后开先关。
            fileOutputStream.close();
            fileInputStream.close();
        }
    
    }

    输出字节流
    --------| OutputStream 所有输出字节流的基类 抽象类
    ------------| FileOutputStream 向文件 输出数据 的输出字节流
    ------------| Bufferedoutputstream 缓冲输出字节流 BufferedOutputStream出现的目的是为了提高写数据的效率。
    内部也是维护了一个8kb的字节数组而已。

    使用BufferedOutputStream的步骤:
    1. 找到目标文件
    2. 建立数据的输出通道


    BufferedOutputStream 要注意的细节
    1. 使用BufferedOutStream写数据的时候,它的write方法是是先把数据写到它内部维护的字节数组中。
    2. 使用BufferedOutStream写数据的时候,它的write方法是是先把数据写到它内部维护的字节数组中,如果需要把数据真正的写到硬盘上面,需要
    调用flush方法或者是close方法、 或者是内部维护的字节数组已经填满数据的时候。

    public class Demo2 {
    
        public static void main(String[] args) throws IOException {
            //找到目标文件
            File file = new File("F:\a.txt");
            //建立数据的输出通道
            FileOutputStream  fileOutputStream = new FileOutputStream(file);
            //建立缓冲输出字节流对象
            BufferedOutputStream bufferedOutputStream  = new BufferedOutputStream(fileOutputStream);
            //把数据写出
            bufferedOutputStream.write("hello world".getBytes()); 
            //把缓冲数组中内部的数据写到硬盘上面。
            //bufferedOutputStream.flush();
            bufferedOutputStream.close();
        }
        
    }

    使用字节流拷贝文件

    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    public class CopyFile {
    
        public static void main(String[] args) throws IOException {
            File infile=new File("c:\1.jpg");
            File outfile=new File("e:\1.jpg");
            FileInputStream fileInputStream=new FileInputStream(infile);
            FileOutputStream fileOutputStream =new FileOutputStream(outfile);
            BufferedInputStream BufferedInputStream=new BufferedInputStream(fileInputStream);
            BufferedOutputStream bufferedOutputStream=new BufferedOutputStream(fileOutputStream);
            int leng=0;
            while ((leng=BufferedInputStream.read())!=-1) {
                        bufferedOutputStream.write(leng);
            }
            bufferedOutputStream.close();
            BufferedInputStream.close();
            System.out.println("文件拷贝完成");
        }
    
    }

    异常处理:

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    import javax.management.RuntimeErrorException;
    
    /*
     IO异常 的处理
    
     */
    public class Demo1 {
    
        public static void main(String[] args) {
        //    readTest();
            
            copyImage();
        }
        // 拷贝图片
        public static void copyImage() {
            FileInputStream fileInputStream = null;
            FileOutputStream fileOutputStream = null;
            try {
                // 找到目标文件
                File inFile = new File("F:\美女\1.jpg");
                File outFile = new File("E:\1.jpg");
                // 建立输入输出通道
                fileInputStream = new FileInputStream(inFile);
                fileOutputStream = new FileOutputStream(outFile);
                // 建立缓冲数组,边读边写
                byte[] buf = new byte[1024];
                int length = 0;
                while ((length = fileInputStream.read(buf)) != -1) {
                    fileOutputStream.write(buf, 0, length);
                }
            } catch (IOException e) {
                System.out.println("拷贝图片出错...");
                throw new RuntimeException(e);
            } finally {
                // 关闭资源
                try {
                    if (fileOutputStream != null) {
                        fileOutputStream.close();
                        System.out.println("关闭输出流对象成功...");
                    }
                } catch (IOException e) {
                    System.out.println("关闭输出流资源失败...");
                    throw new RuntimeException(e);
                } finally {
                    if (fileInputStream != null) {
                        try {
                            fileInputStream.close();
                            System.out.println("关闭输入流对象成功...");
                        } catch (IOException e) {
                            System.out.println("关闭输入流对象失败...");
                            throw new RuntimeException(e);
                        }
                    }
    
                }
            }
        }
    
        public static void readTest() {
            FileInputStream fileInputStream = null;
            try {
                // 找到目标文件
                File file = new File("F:\aaaaa.txt");
                // 建立数据输入通道
                fileInputStream = new FileInputStream(file);
                // 建立缓冲数组读取数据
                byte[] buf = new byte[1024];
                int length = 0;
                while ((length = fileInputStream.read(buf)) != -1) {
                    System.out.print(new String(buf, 0, length));
                }
            } catch (IOException e) {
                /*
                 * //处理的代码... 首先你要阻止后面的代码执行,而且要需要通知调用者这里出错了... throw new
                 * RuntimeException(e);
                 * //把IOException传递给RuntimeException包装一层,然后再抛出,这样子做的目的是
                 * 为了让调用者使用变得更加灵活。
                 */
                System.out.println("读取文件资源出错....");
                throw new RuntimeException(e);
            } finally {
                try {
                    if (fileInputStream != null) {
                        fileInputStream.close();
                        System.out.println("关闭资源成功...");
                    }
                } catch (IOException e) {
                    System.out.println("关闭资源失败...");
                    throw new RuntimeException(e);
                }
            }
        }
    
    }
  • 相关阅读:
    3305: Hero In Maze II (优先队列+bfs)
    2016年5月8日 GDCPC省赛总结
    POJ 2361 Tic Tac Toe
    about 字节
    KMP模式匹配
    scau 8616 汽车拉力比赛
    海盗分金--大于半数才成立
    scau 10692 XYM-入门之道
    函数模板和类模板成员函数的定义通常放在头文件中
    c语言运算符优先级巧记
  • 原文地址:https://www.cnblogs.com/AllenRandolph/p/6986593.html
Copyright © 2011-2022 走看看