javaSE(14)(字节流与字符流)

字节输入流:
package zz.itheima.byte_char_stream;

import java.io.FileInputStream;

public class TestFileInputStream {
    //测试字节输入流(读文件)
    public static void main(String[] args) {
        /*演示FileInputStream public FileInputStream(File file) public FileInputStream(String name) public int read() public int read(byte[] b) new String(byte[] b) public void close()*/
        try {
            FileInputStream fis = new FileInputStream("demo.txt");//没有的话会自动创建

        /* int val = fis.read();//读文件。一次读取一个字节(8位) System.out.println((char)val);*/

            //一次读取一个字节太慢,来个循环
        /* int val; while ((val=fis.read())!=-1) {//把读的结果赋值给val,并且如果返回-1说明读取完毕了,如果不是-1说明还有内容 System.out.println((char)val); }*/
            //一次读取太慢。来个循环
        /* while (true) { int val = fis.read(); if (val==-1) { break; }else { System.out.println((char)val); } }*/
            System.out.println("**********************");
            //一次读取太慢,来一辆车一次拉好多货
            byte[] temp = new byte[1024];
            fis.read(temp);
            System.out.println(new String(temp).trim());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
字节输出流:
package zz.itheima.byte_char_stream;

import java.io.FileOutputStream;

public class TestFileOutputStream {
    //测试文件输出流(写文件)
    public static void main(String[] args) {
        /*演示FileOutputStream类 public FileOutputStream(File file) public FileOutputStream(File file,boolean append) public FileOutputStream(String name) public FileOutputStream(String name,boolean append) public void write(int b) 参数是ASCII表中的码值,不是普通数字 public void write(byte[] b) String对象的getBytes方法 public void write(byte[] b, int off, int len) public void flush() ???不做任何操作 字节流不用缓冲区,二进制数据会直接写入到硬盘中 public void close()*/ 
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream("demo.txt");
            //fos.write(97);//参数是ASCII表中的码值,不是普通数字,一次写一个字节,比较慢
            byte[] val = {97,98,13,99,'v'};//13是换行
            fos.write(val);
            byte[] hello = {'h','e','l','l','o'};
            fos.write(hello);
            String s = "你好!";
            fos.write(s.getBytes());//字符串转换为字节数组
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
字符输入(读文件)流:
package zz.itheima.byte_char_stream;

import java.io.File;
import java.io.FileReader;

public class TestFileReader {
    //测试文件(字符)输入流
    public static void main(String[] args) {
        /*FileReader类的常用方法: public FileReader(File file) public FileReader(String fileName) public int read() public int read(char[] cbuf) public void close()*/
        try {
            //架管道
            File f = new File("demo.txt");
            FileReader fr = new FileReader(f);
            /*int res = fr.read();还是一次读取一个字符 System.out.println((char)res);*/

        /* //来辆车 char[] temp = new char[(int)f.length()];//一次装的大小刚好为文件大小 fr.read(temp); System.out.println(new String(temp));*/

            //对于较大文件循环多次读取
            char[] temp = new char[1024];
            while (true) {
                int res = fr.read(temp);
                if (res==-1) {
                    break;
                }
                System.out.println(new String(temp));
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
字符输出(写文件)流:
package zz.itheima.byte_char_stream;

import java.io.FileWriter;
import java.io.IOException;

public class TestFileWrite {

    public static void main(String[] args) {
        // 演示FileWriter类
/* public FileWriter(File file) public FileWriter(String fileName) public FileWriter(File file,boolean append) public FileWriter(String fileName,boolean append) public void write(String str) public void write(char[] cbuf) public void write(char[] b,int off, int len) public void flush() public void close()*/
        try {
            FileWriter fw=new FileWriter("demo.txt");
            fw.write("wor\nld");

            char[] temp={'h','e','l','l','o'};
            fw.write(temp);

            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
字符流的缓冲区:
package zz.itheima.byte_char_stream;

import java.io.FileWriter;

public class TestHuanChong {

    public static void main(String[] args) {
        // 演示字符流的缓冲区
                //默认缓冲区大小8K,超过就自动写;没超过就需要调用flush()或close()才会写
        try {
            FileWriter fw = new FileWriter("demo.txt");
            for (int i = 0; i < 1024; i++) {
                fw.write("a");
                //fw.flush();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
字节数组大小:
package zz.itheima.byte_char_stream;

import java.io.File;
import java.io.FileInputStream;

public class TestByteArrayLength {

    public static void main(String[] args) {
        // 演示读取字节时,数组的大小
                //对于较小文件可以是文件大小,然后一次性读取;对于较大文件可以循环多次读取
    /* try { File f = new File("demo.txt"); FileInputStream fis = new FileInputStream(f); byte[] temp = new byte[(int)f.length()]; fis.read(temp); System.out.println(new String(temp)); } catch (Exception e) { e.printStackTrace(); }*/
        //1。家管道
        try {
            File f = new File("demo.txt");
            FileInputStream fis = new FileInputStream(f);

            //来辆车
            byte[] temp = new byte[(char)f.length()];
            fis.read(temp);
            //把字节转换成字符
            String val = new String(temp);
            System.out.println(val);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
每次读取内容多少不同的效率:
package zz.itheima.byte_char_stream;

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

public class TestEfficiency {

    // 每次一个数组
    public static void copy1()
    {
        long begin=System.currentTimeMillis();
        try {
            File f=new File("E:\\Oracle的钢铁侠植入广告.mov");
            FileInputStream fis=new FileInputStream(f);
            FileOutputStream fos=new FileOutputStream("d:\\1.mov");
            byte[] temp=new byte[1024*100];   //一次拉100k

            while(true)
            {
                //装车
                int res=fis.read(temp);
                if(res==-1)
                    break;
                //卸车
                fos.write(temp,0,res);  
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        long end=System.currentTimeMillis();
        System.out.println(end-begin);
    }

    //每次一个字节
    public static void copy2()
    {
        long begin=System.currentTimeMillis();
        try {
            File f=new File("E:\\Oracle的钢铁侠植入广告.mov");
            FileInputStream fis=new FileInputStream(f);
            FileOutputStream fos=new FileOutputStream("d:\\1.mov");

            while(true)
            {
                //装车
                int res=fis.read();
                if(res==-1)
                    break;
                //卸车
                fos.write(res); 
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        long end=System.currentTimeMillis();
        System.out.println(end-begin);
    }
    public static void main(String[] args) {
        /*复制一个Mp4 两种方式(1.每次一个字节 2.每次一个数组)的效率测试*/
        copy2();
    }

}
复制文件:
package zz.itheima.byte_char_stream;

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

public class CopyFile {
    /*复制一个图片 对于较小文件可以一次性读,然后一次性写*/
    public static void copy1()
    {
        try {
            // 1.读取源路径文件的数据 
            //架管道
            File f=new File("E:\\1.jpg");
            FileInputStream fis=new FileInputStream(f);
            //来辆车
            byte[] temp=new byte[(int)f.length()];
            //装货
            fis.read(temp);
            // 2.把数据写入到目标路径的文件中
            //架管道
            FileOutputStream fos=new FileOutputStream("d:\\1.jpg");
            //写
            fos.write(temp);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /*复制一个图片 对于较大文件可以边读边写*/
    public static void copy2()
    {
        try {
            File f=new File("E:\\1.jpg");
            FileInputStream fis=new FileInputStream(f);
            FileOutputStream fos=new FileOutputStream("d:\\1.jpg");
            byte[] temp=new byte[1024*10];   //一次拉10k

            while(true)
            {
                //装车
                int res=fis.read(temp);
                if(res==-1)
                    break;
                System.out.println(res);
                //卸车
                fos.write(temp,0,res);  
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) {
        copy1();
    }

}
复制字符串文件:
package zz.itheima.byte_char_stream;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class CopyStringFile {

    // 复制
    // 1.从源路径读取数据
    // 2.把数据写入到目标路径

    // 对于较小文件可以一次性读,然后一次性写
    public static void copy1() {

        // 架管道
        FileReader fr = null;
        // 架管道
        FileWriter fw = null;
        try {
            File f = new File("E:\\1.jpg");
            fr = new FileReader(f);
            // 来辆车
            char[] temp = new char[(int) f.length()];
            // 装车
            fr.read(temp);

            fw = new FileWriter("e:\\1.jpg");
            // 卸货
            fw.write(temp);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fr.close();
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    // 对于较大文件可以边读边写
    public static void copy2(){
        // 架管道
        FileReader fr = null;
        // 架管道
        FileWriter fw = null;
        try {
            File f = new File("demo.txt");
            fr = new FileReader(f);
            fw = new FileWriter("e:\\2.txt");
            char[] temp = new char[1024];

            while (true) {
                int res=fr.read(temp);
                if(res==-1)
                    break;
                else {
                    fw.write(temp,0,res);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            try {
                fr.close();
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
    public static void main(String[] args) {

    }

}
例子一:
package zz.itheima.byte_char_stream;

import java.io.FileOutputStream;
import java.util.Scanner;

public class Demo1 {

    // 练习:用户在控制台多次录入学生姓名,直到录入quit退出,然后把这些姓名换行写到一个文本文件中
    public static void method1() throws Exception{
        Scanner sc = new Scanner(System.in);
        FileOutputStream fos = new FileOutputStream("E:\\demo.txt");
        String flag ;
        do {
            System.out.println("请输入学生姓名:");
            String name = sc.nextLine();
            fos.write(name.getBytes());
            fos.write(13);
            fos.write(10);

            System.out.println("是否继续(y/n)");
            flag = sc.nextLine();
        } while (flag.equalsIgnoreCase("y"));
        fos.close();
    }

    public static void method2() throws Exception {
        // 练习:用户在控制台多次录入学生姓名,直到录入quit退出,然后把这些姓名换行写到一个文本文件中
        Scanner sc = new Scanner(System.in);
        FileOutputStream fos = new FileOutputStream("e:\\demo.txt");
        String flag;
        String all="";

        do {

            System.out.println("请输入学生姓名:");
            String name = sc.nextLine();
            all=all+name+"\r\n";   //记事本对于回车换行的解析跟其他编辑器不同
            System.out.println("是否继续(y/n)");
            flag = sc.nextLine();

        } while (flag.equalsIgnoreCase("y"));

        System.out.println(all);
        fos.write(all.getBytes());
        fos.close();
    }
    public static void main(String[] args)throws Exception {
        //method1();
        method2();

    }

}
例子二:
package zz.itheima.byte_char_stream;

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


public class Demo2 {
    //一次性读写
    public static void copy1(){
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            File f = new File("E:\\1.jpg");
            fis = new FileInputStream(f);
            byte[] temp = new byte[(int)f.length()];
            fis.read(temp);

            fos = new FileOutputStream("E:\\2.jpg");
            fos.write(temp);
        } catch (Exception e) {
            e.printStackTrace();
        } finally{
            try {
                fis.close();
                fos.close();
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
    }
    //多次分段读写
    public static void copy2(){
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            File f = new File("E:\\1.jpg");
            fis = new FileInputStream(f);
            fos = new FileOutputStream("E:\\3.jpg");
            byte[] temp = new byte[10];
            while (true) {
                int res = fis.read(temp);
                if (res==-1) {
                    break;
                }else {
                    fos.write(temp, 0, res);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally{
            try {
                fis.close();
                fos.close();
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
    }
    public static void main(String[] args) {
        //copy1();
        copy2();

    }

}
例子三:
package zz.itheima.byte_char_stream;

import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class Demo3 {

    public static void main(String[] args) {
        // 练习:用户在控制台多次录入学生姓名,每3个写入到一个文本文件中
        FileWriter fw = null;
        try {
            fw = new FileWriter("demo.txt");
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        Scanner sc = new Scanner(System.in);
        int n = 1;
        String name = "";

        try {
            do {
                System.out.println("请输入第" + n + "个学生的姓名:");
                name = sc.nextLine();
                fw.write(name + "\n");

                if (n % 3==0) {
                    fw.flush();
                }
                n++;
            } while (!name.equalsIgnoreCase("quit"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally{
            try {
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}

你可能感兴趣的:(java,字节流,字符流,se)