通过举例了解java中的流

 

Java流结构介绍

Java所有的流类位于java.io包中,都分别继承字以下四种抽象流类型。

  字节流 字符流
输入流 InputStream Reader
输出流 OutputStream Writer

1.继承自InputStream/OutputStream的流都是用于向程序中输入/输出数据,且数据的单位都是字节(byte=8bit),如图,深色的为节点流,浅色的为处理流。

 

2.继承自Reader/Writer的流都是用于向程序中输入/输出数据,且数据的单位都是字符(2byte=16bit),如图,深色的为节点流,浅色的为处理流。

Java流对文件的读取代码演示

1.java的输出流---字节流

代码展示:

public class Ioshow {
    public static void main(String[] args) {
        new Ioshow().outStreamMethod();
    }
    public void outStreamMethod(){
        try {
            //创建输出字节流  并指定输出的文件地址
            OutputStream os = new FileOutputStream("F:/iotest/outio.txt");
            //要输入文件的字段
            String ostr = "写入到文件中";
            //调用输出流的write方法输出到指定文件中
            os.write(ostr.getBytes());
            
            System.out.println("执行完毕");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

运行结果:

2.Java的输入流---字节流

代码展示

public class Ioshow {
    public static void main(String[] args) {
//        new Ioshow().outStreamMethod();
        new Ioshow().inputStreamMethod();
    }
    public void inputStreamMethod(){
        try {
            //创建输入字节流  并指定要读取的文件地址
            InputStream is = new FileInputStream("F:/iotest/inputio.txt");
            //声明一个字节流对象
            byte cont[]=new byte[1024];
            //将文件中的内容读取到字节数组中
            is.read(cont);
            //打印出读取的内容
            System.err.println(new String(cont));
            
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

运行结果

3.java输入流----字符流

 代码展示

public void fileinput(){
        try {
            File file = new File("F:/iotest/inchartio.txt");
                
            //创建输入流   并指定读取文件位置   
            InputStream reader = new FileInputStream(file);
            //创建字符输入流     并传入输流对象  并指定输入流的读取编码
            InputStreamReader is = new InputStreamReader(reader, "UTF-8");
            char chat[] = new char[1000];
            //将文件中的内容读取到   chart[]中
            is.read(chat);
            for (char c : chat) {
                System.out.print(c);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
    }

 运行结果

4.java输出流-----字符流

实现将inchartio.txt文件的内容读取到outchartio.txt中

 代码展示

public class Ioshow {
    public static void main(String[] args) {
//        new Ioshow().outStreamMethod();
//        new Ioshow().inputStreamMethod();
        new Ioshow().fileinput();
    }
    public void fileinput(){
        try {
            System.out.println("====start read====");
            File file = new File("F:/iotest/inchartio.txt");
            //创建输入流   并指定读取文件位置   
            InputStream reader = new FileInputStream(file);
            //创建字符输入流     并传入输流对象  并指定输入流的读取编码
            InputStreamReader is = new InputStreamReader(reader, "UTF-8");
            char chat[] = new char[1000];
            //将文件中的内容读取到   chart[]中
            is.read(chat);
            System.out.print(chat);
            //声明一个输出的文件
            File outfile = new File("F:/iotest/outchartio.txt");
            System.out.println(outfile.exists());
            if (outfile.exists()==false) {//文件若不存在
                System.out.println("没有该文件夹    创建了一个");
                outfile.createNewFile();//创建文件
            }
            //创建输出流
            OutputStream os = new FileOutputStream(outfile);
            //创建字符流   并传入输出流   指定字符编码
            OutputStreamWriter osw = new OutputStreamWriter(os, "utf-8");
            //将读取到的内容写到文件中
            osw.write(chat);
            //清空输出流
            osw.flush();
            System.out.println("===the end====");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

运行结果

Java流对图片的读取和写入

 代码展示:

public class Ioshow {
    public static void main(String[] args) {
        new Ioshow().imginput();
    }
    public void imginput(){
        InputStream is = null;
        DataInputStream dis = null;
        OutputStream os =null;
        DataOutputStream dos =null;
        try {
            is = new FileInputStream("F:/iotest/帅气猴.jpg");
            dis = new DataInputStream(is);
            
            os = new FileOutputStream("F:/iotest/输出猴.jpg");
            dos = new DataOutputStream(os);
            int img ;
            while ((img = dis.read())!=-1) {
                dos.write(img);
            }
            
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                dos.close();
                os.close();
                dis.close();
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            
        }
         
    }
}

运行结果

原文地址:https://www.cnblogs.com/kuoAT/p/6946132.html