• IO流——字节流


    文件输出流

    FileOutputStream:文件输出流是用于将数据写入 File,每次运行,都会覆盖之前文件中的数据

    FileOutputStream(File file):创建一个向指定 File 对象表示的文件中写入数据的文件输出流

    public class MyTest2 {
    public static void main(String[] args) throws IOException {
    File file = new File("c.txt");//封装文件
    FileOutputStream out = new FileOutputStream(file);
    //输出流所关联的文件如果不存在,则会自动创建
        
    //我们使用输入流,往文件中写入数据
            out.write(97); //a  一次写入一个字节
            out.write(98); //b
            out.write(99);//c
            out.write(300); //如果你超过一个字节的范围,会丢弃掉多余字节
        byte[] bytes = {100, 101, 102, 103, 104, 105};
            out.write(bytes);//一次写入一个字节数组
            System.out.println(new String(bytes));
            out.write(bytes, 0, 3); //一次写入字节数组的一部分
            String str = "我爱学习";
            byte[] bytes1 = str.getBytes(); //解码 utf-8
            out.write(bytes1);
            out.write(bytes1,0,12);
            //流使用完毕要释放资源
            out.close();
          /*  创建字节输出流对象了做了几件事情 ?
            a : 调用系统资源创建a.txt文件
            b:创建了一个out对象
            c:把out对象指向这个文件
            为什么一定要close() ?
            a : 通知系统释放关于管理a.txt文件的资源
            b:让Io流对象变成垃圾, 等待垃圾回收器对其回收*/
        }
    }
    

    FileOutputStream(String name):创建一个向具有指定名称的文件中写入数据的输出文件流

    File file = new File("c.txt");//封装文件
    FileOutputStream out2 = new FileOutputStream("e.txt");//传入的是字符串路径
    

    FileOutputStream(File file, boolean append):创建一个向指定 File 对象表示的文件中写入数据的文件输出流

    **FileOutputStream(String name, boolean append): **创建一个向具有指定 name 的文件中写入数据的输出文件流

    public class MyTest2 {
        public static void main(String[] args) throws IOException {
            //默认的是,每次运行,都会覆盖之前文件中的数据
           //参数2:true 代表追加写入,不会重新覆盖文件中之前的数据
            FileOutputStream out = new FileOutputStream("e.txt",true);
            out.write("你忘了你有多美".getBytes());
            out.write("
    ".getBytes());//换行
            out.write("我爱你".getBytes());
            out.write("
    ".getBytes());//换行
    
            out.close();
        }
    }
    

    文件输入流

    文件输出流:就是文件中的数据

    FileInputStream(File file) :通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中的 File 对象 file 指定。

    案例1:一次读取一个字节来读取文本文件

    public class MyTest {
    public static void main(String[] args) throws IOException {
    File file = new File("h.txt");
    //file.createNewFile();
    //输入流所关联的文件,如果不存在就会报错
    FileInputStream in = new FileInputStream(file);
    int b = in.read();//一次读取一个字节,返回的是这个字节数据,如果读取不到,返回 -1 我们会拿-1 判断他文件是否读取完
    System.out.println(b);
      }
    }
    

    案例2:一次读取一部分字节,填入到缓冲区数组

    public class MyTest3 {
        public static void main(String[] args) throws IOException {
            FileInputStream in = new FileInputStream("a.txt");
            byte[] bytes = new byte[1024];
            //一次读取一部分字节,填入到缓冲区数组
            int len = in.read(bytes, 0, 20);
            for (byte Byte : bytes) {
                System.out.println(Byte);
            }
    
            in.close();
        }
    }
    

    案例3:读一个字节写一个字节来复制一个文本文件

    public class MyTest {
     public static void main(String[] args) throws IOException {
            //采用文件输入输入流进行文件的复制
            //读一个字节写一个字节来复制一个文本文件
            FileInputStream in = new FileInputStream("MyTest.java");
            FileOutputStream out = new FileOutputStream("C:\Users\ShenMouMou\Desktop\MyTest.java");
            //频繁的读写
    
            int len = 0;//读取到字节
            while ((len=in.read())!=-1){
                out.write(len);
                out.flush();
            }
            //释放资源
            in.close();
            out.close();
            System.out.println("复制完成");
        }
    }
    

    案例4:一次读写一个字节数组来进行复制MP3

    public class MyTest {
        public static void main(String[] args) throws IOException {
            //很显然,一次读写一个字节去复制文件,效率太低,太耗时
            //一次读写一个字节数组来进行复制,才是首选
            File file = new File("烟花易冷Live_林志炫.mp3");
            FileInputStream in = new FileInputStream(file);
            FileOutputStream out = new FileOutputStream("C:\" + file.getName());
            //定义一个字节数组,充当缓冲区
            byte[] bytes = new byte[1024 * 8];
            //定义一个变量,来记录你每次读取到的有效字节个数
            int len = 0;
            long start = System.currentTimeMillis();
            while ((len = in.read(bytes)) != -1) {
                // System.out.println("循环次数");
                out.write(bytes, 0, len);
                out.flush();
            }
            long end = System.currentTimeMillis();
            System.out.println("复制完成耗时" + (end - start) + "毫秒");
            //释放资源
            in.close();
            out.close();
        }
    }
    

    FileInputStream(String name):通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中的路径名 name 指定。

    高效的字节输入输出流

    BufferedInputStream(InputStream in) :创建一个 BufferedInputStream 并保存其参数,即输入流 in,以便将来使用。

    BufferedInputStream(InputStream in, int size):创建具有指定缓冲区大小的 BufferedInputStream 并保存其参数,即输入流 in,以便将来使用

    BufferedOutputStream(OutputStream out):创建一个新的缓冲输出流,以将具有指定缓冲区大小的数据写入指定的底层输出流

    BufferedOutputStream(OutputStream out,int size):创建一个新的缓冲输出流,以将具有指定缓冲区大小的数据写入指定的底层输出流

    public class MyTest {
        public static void main(String[] args) throws IOException {
            copy0(); //高效的流
            copy1();
        }
    
    private static void copy0() throws IOException {
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream("烟花易冷Live_林志炫.mp3"), 1024);
            BufferedOutputStream bos= new BufferedOutputStream(new FileOutputStream("C:\a.mp3"));
            //读取一个字节写入一个字节
            //一次读取一个字节,写一个字节来复制音乐
            int len = 0;//用来记录读取到的字节
            long start = System.currentTimeMillis();
            byte[] bytes = new byte[1024 * 8];
            while ((len = bis.read(bytes)) != -1) {
                bos.write(bytes,0,len);
                bos.flush();
            }
            long end = System.currentTimeMillis();
            bos.close();
            bis.close();
            System.out.println("复制完成耗时" + (end - start) + "毫秒");
        }
    
    private static void copy1() throws IOException {
            File file = new File("烟花易冷Live_林志炫.mp3");
            FileInputStream in = new FileInputStream(file);
            FileOutputStream out = new FileOutputStream("C:\Users\ShenMouMou\Desktop\" + file.getName());
            int len = 0;//用来记录读取到的字节
            byte[] bytes = new byte[1024 * 8];
            long start = System.currentTimeMillis();
            while ((len = in.read(bytes)) != -1) {
                out.write(bytes,0,len);
                out.flush();
            }
            long end = System.currentTimeMillis();
            in.close();
            out.close();
            System.out.println("复制完成耗时" + (end - start) + "毫秒");
        }
    }
    
  • 相关阅读:
    洛谷 P1934 封印
    洛谷 P2426 删数
    洛谷 P3399 丝绸之路
    SQL SERVER镜像配置,无法将 ALTER DATABASE 命令发送到远程服务器实例的解决办法
    Step7:SQL Server 多实例下的复制
    Step6:SQL Server 数据变更时间戳(timestamp)在复制中的运用
    Step5:SQL Server 跨网段(跨机房)FTP复制
    Step4:SQL Server 跨网段(跨机房)复制
    Step3 SQL Server 通过备份文件初始化复制
    Step2:SQL Server 复制事务发布
  • 原文地址:https://www.cnblogs.com/godles/p/11885695.html
Copyright © 2020-2023  润新知