zoukankan      html  css  js  c++  java
  • Java IO(四)------字节输入输出流

     1、字节输出流:OutputStream

      这个抽象类是表示字节输出流的所有类的超类。 输出流接收输出字节并将其发送到某个接收器。

      方法摘要:

      

    FileOutputStream

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

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

    1.1.1 FileOutputStream类写入数据到文件中

      

    public class FileOutputStreamDemo {
        public static void main(String[] args) throws IOException {
            //需求:将数据写入到文件中。
            //创建存储数据的文件。
            File file = new File("c:\file.txt");
            //创建一个用于操作文件的字节输出流对象。一创建就必须明确数据存储目的地。
            //输出流目的是文件,会自动创建。如果文件存在,则覆盖。
            FileOutputStream fos = new FileOutputStream(file);
            //调用父类中的write方法。
            byte[] data = "abcde".getBytes();
            fos.write(data);
            //关闭流资源。
            fos.close();
        }
    }

     给文件中续写和换行

    我们直接new FileOutputStream(file)这样创建对象,写入数据,会覆盖原有的文件,那么我们想在原有的文件中续写内容怎么办呢?

    继续查阅FileOutputStreamAPI。发现在FileOutputStream的构造函数中,可以接受一个boolean类型的值,如果值true,就会在文件末位继续添加。

    构造方法

     

    给文件中续写数据和换行,代码演示:

    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 = "
    "+"aaa";
            fos.write(str.getBytes());
            fos.close();
        }
    }

    异常的处理

    public class FileOutputStreamDemo3 {
        public static void main(String[] args) {
            File file = new File("c:\file.txt");
            //定义FileOutputStream的引用
            FileOutputStream fos = null;
            try {
                //创建FileOutputStream对象
                fos = new FileOutputStream(file);
                //写出数据
                fos.write("abcde".getBytes());
            } catch (IOException e) {
                System.out.println(e.toString() + "----");
    throw new RuntimeException("文件写入失败,重试");
    
            } finally {
                //一定要判断fos是否为null,只有不为null时,才可以关闭资源
                if (fos != null) {
                    try {
                        fos.close();
                    } catch (IOException e) {
                        throw new RuntimeException("关闭资源失败");
                    }
                }
            }
        }
    }

     2、字节输入流:InputStream

     这个抽象类是表示输入字节流的所有类的超类。

      方法摘要:

      

      

     

      

     FileInputStream

    InputStream有很多子类,其中子类FileInputStream可用来读取文件内容。

    FileInputStream 从文件系统中的某个文件中获得输入字节。

     FileInputStream类读取数据read方法

    在读取文件中的数据时,调用read方法,实现从文件中读取数据

     

    public class FileInputStreamDemo {
        public static void main(String[] args) throws IOException {
            File file = new File("c:\file.txt");
            //创建一个字节输入流对象,必须明确数据源,其实就是创建字节读取流和数据源相关联。
            FileInputStream fis = new FileInputStream(file);
            //读取数据。使用 read();一次读一个字节。
            int ch = 0;
            while((ch=fis.read())!=-1){
                System.out.println("ch="+(char)ch);
            }
            // 关闭资源。
            fis.close();
        }
    }

    读取数据read(byte[])方法

    public class FileInputStreamDemo2 {
        public static void main(String[] args) throws IOException {
            /*
             * 演示第二个读取方法, read(byte[]);
             */
            File file = new File("c:\file.txt");
            // 创建一个字节输入流对象,必须明确数据源,其实就是创建字节读取流和数据源相关联。
            FileInputStream fis = new FileInputStream(file);        
            //创建一个字节数组。
            byte[] buf = new byte[1024];//长度可以定义成1024的整数倍。        
            int len = 0;
            while((len=fis.read(buf))!=-1){
                System.out.println(new String(buf,0,len));
            }
            fis.close();
        }
    }

    复制文件

    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();
        }
  • 相关阅读:
    realplayer web播放器控件参数和函数
    几种技术语言简介!
    电子书标志设计,精品设计,形象设计,封面设计,宣传广告设计作品欣赏
    QuickCHM2.6出现了"不支持此接口"
    svchost.exe占用CPU 100%的解决方法
    [转]网站健康检查
    php新帮手 PHPMaker v5.0.1.0
    【OpenGL】理解GL_TRIANGLE_STRIP等绘制三角形序列的三种方式
    UML用例图总结
    【转】Ogre的八叉树场景管理器OctreeSceneManager
  • 原文地址:https://www.cnblogs.com/-lwl/p/11261062.html
Copyright © 2011-2022 走看看