输入输出流——字节流部分

在文件之后 比较合适出现的 就是 关于 流的操作了。

猜想关于数据的写入和写出操作,本质上是01形式的二进制数,而这些数据的排列方式是不能乱套的。他们是一个有序的整体,这队长长的用于表示一些内容的东西就称作流了。在这里面用Stream 来标识。

 java.io 包中定义了多个流类,用来实现输入/输出功能,以不同的角度可以分类为:

   1、按数据流的方向分为输入和输出。

   2、按数据处理的单位分为字节流和字符流。

   3、按功能不同分为节点流和处理流。

package IOPart;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class IOPart {
    
    public static void main(String[] args) {
        /**
         * 在F:	ryFile目录下,创建一个a.txt 文件,修改里面的内容为:used to try the first demo of inputStream.
         */
        try {
            //如果 在这个 路径找不到 这样一个 文件的话,就会报出,文件未找到异常,所以 要用 FileNotFoundException 包裹
            FileInputStream fileInputStream = new FileInputStream(new File("f:\tryFile\a.txt"));
            byte[] contents = new byte[1024];
            fileInputStream.read(contents);//这里会报一个 读写异常,再从文件流往内存中的数组中写入数据的时候,可能会出现问题,需要用IOException包裹一下
            String result = new String(contents);
            System.out.println(result);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

运行结果:

used to try the first demo of inputStream.

//这个编辑器还真是高级,自动抹掉了我后面茫茫多的空格。事实上因为 我们 创建了一个 长达 1024个 bite位的数组。所以。有多少读多少,没有填空,补足。这样就会有弊端,我们要存储的数据的内容的大小不确定,那么这个 byte给多大合适呢?

方案一:

package IOPart;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class IOPart {
    
    public static void main(String[] args) {
        /**
         * 在F:	ryFile目录下,创建一个a.txt 文件,修改里面的内容为:used to try the first demo of inputStream.
         */
        try {
            //如果 在这个 路径找不到 这样一个 文件的话,就会报出,文件未找到异常,所以 要用 FileNotFoundException 包裹
            FileInputStream fileInputStream = new FileInputStream(new File("f:\tryFile\a.txt"));
            byte[] contents = new byte[fileInputStream.available()];
            fileInputStream.read(contents);//这里会报一个 读写异常,再从文件流往内存中的数组中写入数据的时候,可能会出现问题,需要用IOException包裹一下
            String result = new String(contents);
            System.out.println(result);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

流作为资源的一种使用之后是要关闭的。

package IOPart;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class IOPart {
    
    public static void main(String[] args) {
        /**
         * 在F:	ryFile目录下,创建一个a.txt 文件,修改里面的内容为:used to try the first demo of inputStream.
         */
        FileInputStream fileInputStream=null;
        try {
            //如果 在这个 路径找不到 这样一个 文件的话,就会报出,文件未找到异常,所以 要用 FileNotFoundException 包裹
            fileInputStream = new FileInputStream(new File("f:\tryFile\a.txt"));
            byte[] contents = new byte[fileInputStream.available()];
            fileInputStream.read(contents);//这里会报一个 读写异常,再从文件流往内存中的数组中写入数据的时候,可能会出现问题,需要用IOException包裹一下
            String result = new String(contents);
            System.out.println(result);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(fileInputStream!=null){
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

这种抽取形式,还是比较重要的。

方式二:

package IOPart;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class IOPart {
    
    public static void main(String[] args) {
        IOPart ioPart = new IOPart();
        ioPart.method2();
        
    }

    private void method2() {
        FileInputStream fileInputStream = null;
        try {
            fileInputStream= new FileInputStream(new File("f:\tryFile\SendPart.java"));
            byte[] contents = new byte[1024];
            int length = 0;
            while((length=fileInputStream.read(contents))!=-1){
                System.out.println(new String(contents,0,length));
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally {
            if(fileInputStream!=null){
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

    private void method1() {

        /**
         * 在F:	ryFile目录下,创建一个a.txt 文件,修改里面的内容为:used to try the first demo of inputStream.
         */
        FileInputStream fileInputStream=null;
        try {
            //如果 在这个 路径找不到 这样一个 文件的话,就会报出,文件未找到异常,所以 要用 FileNotFoundException 包裹
            fileInputStream = new FileInputStream(new File("f:\tryFile\a.txt"));
            byte[] contents = new byte[fileInputStream.available()];
            fileInputStream.read(contents);//这里会报一个 读写异常,再从文件流往内存中的数组中写入数据的时候,可能会出现问题,需要用IOException包裹一下
            String result = new String(contents);
            System.out.println(result);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(fileInputStream!=null){
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
}

写出:

package IOPart;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class OutputDemo1 {

    public static void main(String[] args) {
        
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(new File("F:\tryFile\output.txt"));
            String contentString = new String("lifei");
            fileOutputStream.write(contentString.getBytes());
            System.out.println("写出成功");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            if(fileOutputStream!=null){
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

字节流拷贝文件:

package IOPart;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class CopyFile1 {
    
    public static void main(String[] args) {
        
        method2();
        
    }

    private static void method2() {

        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {//C:\Users\Administrator\Desktop\震撼世界的演讲《梦想》 标清(270P).qlv
            //fileInputStream  = new FileInputStream(new File("f:/tryFile/SendPart.java"));
            /**
             * 这里面还可以复制的有 视频文件,音频文件,其实就是 各种文件了,只要把要拷贝的文件,放在FileInputStream里面就可以了。当然针对字符 还有专门为字符打造的操作流对象。
             */
            fileInputStream  = new FileInputStream(new File("C:\Users\Administrator\Desktop\震撼世界的演讲《梦想》 标清(270P).qlv"));
            fileOutputStream  = new FileOutputStream(new File("f:/tryFile/copy.qlv"));
            
            byte[] contents = new byte[1024];
            int length = 0;
            while((length=fileInputStream.read(contents))!=-1){
                fileOutputStream.write(contents, 0, length);
            }
            

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            if(fileInputStream!=null){
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(fileOutputStream!=null){
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        System.out.println("复制完成");
    }

    private static void method1() {

        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            fileInputStream  = new FileInputStream(new File("f:/tryFile/SendPart.java"));
            fileOutputStream  = new FileOutputStream(new File("f:/tryFile/copy.txt"));
            byte[] contents = new byte[fileInputStream.available()];
            fileInputStream.read(contents);
            fileOutputStream.write(contents);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            if(fileInputStream!=null){
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(fileOutputStream!=null){
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        System.out.println("复制完成");
    }

}
原文地址:https://www.cnblogs.com/letben/p/5184253.html