• JAVA学习笔记(二十一)


    字节流

    字节输出流OutPutStream

    FileOutputStream类

      OutputStream有很多子类,其中子类FileOutputStream可用来写入数据到文件。

      FileOutputStream类,即文件输出流,是用于将数据写入 File的输出流。

    package com.oracle.demo02;
    
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    public class OutPutStreamDemo {
        /*FileOutputStream创建对象,调用构造方法的时候,
         * 如果这个文件对象中文件存在,会覆盖原来的文件
         * 如果不存在,会创建
         * 
         * 换行"
    "
         * 
         * 字节流操作的单位是字节
         * write()方法,一次只能输入一个字节
         * 100代表d的ASCII码
         * 
         * 流对象的使用步骤:
         * 1.创建流子类对象
         * 2.调用write()方法
         * 3.关闭流对象,调用close方法,释放资源
         * */
        public static void main(String[] args) throws IOException {
            File f=new File("D:\test\demo.txt");
            FileOutputStream fos=new FileOutputStream(f,true);
    //        fos.write(3);
    //        fos.write(4);
    //        fos.write(5);
    //        fos.write(6);
            //如果传入的是负数,那么就是汉字,一个数字占一个字节
            //传字节数组的write方法
            byte[] b= {-11,-97,-98,-100};
            //    fos.write(b);
            //void write(byte[] b,int off,int len)
            //fos.write(b, 1, 2);
            //需求:传hello
            //简便写法
             fos.write("
    java".getBytes());
            fos.close();
        }
    }

    给文件中续写数据和换行

    public class FileOutputStreamDemo2 {
        public static void main(String[] args) throws Exception {
            File file = new File("c:\file.txt");
            FileOutputStream fos = new FileOutputStream(file, true);//设置给指定文件续写数据
            String str = "
    "+"itcast";//实现换行
            fos.write(str.getBytes());
            fos.close();
        }
    }

    IO异常的处理

    package com.oracle.demo02;
    
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    public class ThrowsDemo {
    /*
     * IOException处理异常的细节:
     * 1.保证流对象的作用域足够(在块外面声明,在块里面赋值)
     * 2.catch里面怎么处理异常
     *         IO异常一旦出现,一般不能处理
     *         只能,第一,输出异常信息,第二,throw new RuntimeException
     * 3.流对象建立失败,还需要关闭资源吗?
     * 加一个判断,if(fos!=null)
     * */
        
        public static void main(String[] args) throws FileNotFoundException {
            FileOutputStream fos=new FileOutputStream("d:\test\java.txt");
            try (fos;){
                fos.write("java是世界上最好的语言".getBytes());
            }catch(IOException ex) {
                    ex.printStackTrace();
                    throw new RuntimeException("文件写入失败,try again");
            }
        }
    }

     字节流练习

    复制文件

    public class CopyFileTest {
        public static void main(String[] args) throws IOException {
            //1,明确源和目的。
            File srcFile = new File("c:\YesDir	est.JPG");
            File destFile = new File("copyTest.JPG");
            
            //2,明确字节流 输入流和源相关联,输出流和目的关联。
            FileInputStream fis = new FileInputStream(srcFile);
            FileOutputStream fos = new FileOutputStream(destFile);
            
            //3, 使用输入流的读取方法读取字节,并将字节写入到目的中。
            int ch = 0;
            while((ch=fis.read())!=-1){
                fos.write(ch);
            }
            //4,关闭资源。
            fos.close();
            fis.close();
        }
    }

    缓冲数组方式复制文件

      上述代码复制文件效率太低了,并且频繁的从文件读数据,和写数据,能不能一次多把文件中多个数据都读进内容中,然后在一次写出去,这样的速度一定会比前面代码速度快。

    public class CopyFileByBufferTest {
        public static void main(String[] args) throws IOException {
            File srcFile = new File("c:\YesDir	est.JPG");
            File destFile = new File("copyTest.JPG");
            // 明确字节流 输入流和源相关联,输出流和目的关联。
            FileInputStream fis = new FileInputStream(srcFile);
            FileOutputStream fos = new FileOutputStream(destFile);
            //定义一个缓冲区。
            byte[] buf = new byte[1024];
            int len = 0;
            while ((len = fis.read(buf)) != -1) {
                fos.write(buf, 0, len);// 将数组中的指定长度的数据写入到输出流中。
            }
            // 关闭资源。
            fos.close();
            fis.close();
        }
    }
  • 相关阅读:
    线上查询及帮助命令:
    windows: 2.7 3.5 (主要)
    get the execution time of a sql statement.
    java-kafka安装以及使用案例
    java-黑马头条 weex前端路由
    MYSQL安装
    缓存
    Flask中current_app和g对象
    [ValueError: signal only works in main thread]
    Flask-SQLAlchemy操作
  • 原文地址:https://www.cnblogs.com/boringLee/p/9054769.html
Copyright © 2020-2023  润新知