美文网首页
IO之字节流实例:

IO之字节流实例:

作者: 公子请留步LookMe | 来源:发表于2018-08-16 17:29 被阅读0次

    输出流:OutputStream,常用子类:FileOutputStream
    输入流:InputStream,常用子类:FileInputStream

    • 输出流常用的案例用法:
    package demoByteOut;
    
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    
    public class OutputStream1 {
        public static void main(String[] args) {
            Out();
        }
        public static void Out() {
            OutputStream os = null;
            //创建输出文件
            try {
                os = new FileOutputStream("a.txt");
    //          os = new FileOutputStream(new File("a.txt"));
    //          os = new FileOutputStream("a.txt", true);
    //          os = new FileOutputStream(new File("a.txt"),true);
                String str = "helloworld";
                byte[] bs = str.getBytes();
                //将指定字节写入此文件输出流。
                for(byte b : bs) {
                    os.write(b);
                }
                //将 b.length 个字节从指定 byte 数组写入此文件输出流中。
                os.write(bs);
                //将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此文件输出流。
                os.write(bs, 0, 5);
                os.flush();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }catch(IOException e) {
                e.printStackTrace();
            }finally {
                try {
                    os.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
    
    
    • 输入常用的案例方法:
    package demoByte;
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStream;
    
    public class InputSteam1 {
    
        public static void main(String[] args) {
            input3();
        }
        /**
         * 1.方法为单个字节读取
         * 2.效率太低
         */
        public static void input() {
            InputStream is = null;
            try {
                //创建字节输入流对象
                is = new FileInputStream("a.txt");
                //单个字节读取
                int num = is.read();
                System.out.println(num);
                System.out.println((char) num );
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }catch(IOException e) {
                e.printStackTrace();
            }
        }
        /**
         * 向整个文件读取数据
         */
        public static void input2() {
            InputStream is = null;
            try {
                //创建字节输入对象
                is = new FileInputStream("a.txt");
                //定义一个字节数组
                byte[] by = new byte[1024];
                //向字节数组中存储数据,返回读取长度
                int number= is.read(by);
                System.out.println(number);
                System.out.println(new String(by,0,number));
                } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }catch(IOException e) {
                e.printStackTrace();
            }
        }
        /**
         * 向整个文件读取一定的字节 
         */
        public static void input3() {
            InputStream is = null;
            try {
                //创建字节输入对象
                is = new FileInputStream("a.txt");
                //定义一个字节数组
                byte[] by = new byte[1024];
                int len = is.read(by,1,8);
                System.out.println(len);
                System.out.println(new String(by,1,len));
                } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }catch(IOException e) {
                e.printStackTrace();
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:IO之字节流实例:

          本文链接:https://www.haomeiwen.com/subject/ucpobftx.html