zoukankan      html  css  js  c++  java
  • IO流-------字节流

    import java.io.*;
    public class Example01 {
        public static void main(String[] args) throws Exception {
            // 创建一个文件字节输入流

    File  file=new File("D:/test.txt");
    FileInputStream inputStream=new FileInputStream(file);

    int b = 0;           // 定义一个int类型的变量b,记住每次读取的一个字节
            while (true) {
                b = in.read(); // 变量b记住读取的一个字节
                if (b == -1) { // 如果读取的字节为-1,跳出while循环
                    break;
                }
                System.out.println(b); // 否则将b写出
            }
            in.close();
        }
    }

    输出流:

    package com.example.io;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    public class Example2 {
        /**
         * @param args
         * @throws Exception 
         */
        public static void main(String[] args) throws Exception {
            // TODO Auto-generated method stub
            File  file=new File("D:/test.txt");
            FileOutputStream out=new FileOutputStream(file);
            String str="北京啊";
            byte[] b=str.getBytes();
            for (int i = 0; i < b.length; i++) {
                out.write(b[i]);
            }
            out.close();
        }
    }

    追加流

    package com.example.io;
    
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    
    public class Example3 {
    
        /**
         * @param args
         * @throws Exception 
         */
        public static void main(String[] args) throws Exception {
            // TODO Auto-generated method stub
            File  file=new File("D:/test.txt");
            FileOutputStream out=new FileOutputStream(file,true);
            String str="欢迎你";
            byte[] b=str.getBytes();
            for (int i = 0; i < b.length; i++) {
                out.write(b[i]);
            }
            out.close();
        }
    }

    文件的拷贝:

    package com.example.io;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    public class Example4 {
    
        /**
         * @param args
         * @throws IOException 
         */
        public static void main(String[] args) throws IOException {
            // TODO Auto-generated method stub
            File  file=new File("D:/test1.txt");
            FileOutputStream out=new FileOutputStream(file);
            File file2=new File("D:/test.txt");
            FileInputStream in=new FileInputStream(file2);
            int len;
            long beginintime=System.currentTimeMillis();
            while ((len=in.read())!=-1) {
                out.write(len);
            }
            long endtime=System.currentTimeMillis();
            System.out.println(endtime-beginintime);
            in.close();
            out.close();
        }
    }

    字节流缓冲流:

    import java.io.*;
    public class Example05 {
        public static void main(String[] args) throws Exception {
            // 创建一个字节输入流,用于读取当前目录下source文件夹中的mp3文件
            InputStream in = new FileInputStream("source\江南style.mp3");
            // 创建一个文件字节输出流,用于将读取的数据写入当前目录的target文件中
            OutputStream out = new FileOutputStream("target\江南style.mp3");
            // 以下是用缓冲区读写文件
            byte[] buff = new byte[1024]; // 定义一个字节数组,作为缓冲区
            // 定义一个int类型的变量len记住读取读入缓冲区的字节数
            int len;
            long begintime = System.currentTimeMillis();
            while ((len = in.read(buff)) != -1) { // 判断是否读到文件末尾
                out.write(buff, 0, len); // 从第一个字节开始,向文件写入len个字节
            }
            long endtime = System.currentTimeMillis();
            System.out.println("拷贝文件所消耗的时间是:" + (endtime - begintime) + "毫秒");
            in.close();
            out.close();
        }
    }

    字节缓冲流:

    import java.io.*;
    public class Example07 {
        public static void main(String[] args) throws Exception {
            // 创建一个带缓冲区的输入流
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
                    "src.txt"));
            // 创建一个带缓冲区的输出流
            BufferedOutputStream bos = new BufferedOutputStream(
                    new FileOutputStream("des.txt"));
            int len;
            while ((len = bis.read()) != -1) {
                bos.write(len);
            }
            bis.close();
            bos.close();
        }
    }
    加油啦!加油鸭,冲鸭!!!
  • 相关阅读:
    发送邮件
    php防止表单重复提交
    mysql 优化之注意
    mysqldump
    项目中下拉框链接问题
    css在IE和Firefox下的兼容性
    利用curl并发来提高页面访问速度
    修改linux下mysql目录权限
    ajax跨域
    wireshark抓包
  • 原文地址:https://www.cnblogs.com/clarencezzh/p/5235554.html
Copyright © 2011-2022 走看看