java 从入门到精通

java 中的IO(字节流)

2021-05-27  本文已影响0人  Exception_Cui

字符流

电脑中的所有的文件都已以字节存储
所以所有文件都可以以字符流来读取和存储

字符输入流(硬盘-->内存)

FileInputStream

package IOTest;

import java.io.FileInputStream;
import java.io.IOException;

/**
 * Created by kumamon on 2021/5/27.
 * <p>
 * 电脑中的所有文件都可以用字节来读取
 *
 * 字节输入流
 * 由 硬盘 ->输出到 内存
 */
public class FileInputStreamDemo {


    public static void main(String[] args) throws IOException {
        //inputStream1();
        inputStream2();
    }


    /**
     * 读取多个字节
     * */
    private static void inputStream2() throws IOException {

        FileInputStream fis = new FileInputStream("FileOutPutStrame2.txt");
        int len =0;
        byte[] bytes=new byte[1024];
        while ((len=fis.read(bytes))!=-1){
            String s = new String(bytes);                //结果为    字符串输出流   后面有1024-2*5个 空格
            String sss = new String(bytes,0,len);  //结果为    字符串输出流   有效的转换
            System.out.println(sss);
        }

    }


    /**
     * 读取单个字节
     *  int len = fis.read();
     *  len为 读取到的有效字节  没有了就为-1
     * */
    public static void inputStream1() throws IOException {
        FileInputStream fis = new FileInputStream("FileOutPutStrame2.txt");
        int len = fis.read();
        System.out.println(len);   // 字 的编码表 229   所以读出来也是229

    }
}

字符缓冲流(大文件的效率高)

其实就是增加了一块缓冲区
现将数据读取到缓冲区,
然后一次性读取缓冲区


    /**
     * 使用字节缓冲流读取文件
     *
     * 默认的读取方式  JAVA-->jvm->OS              一个一个读
     *
     * 缓冲流的读取方式 JAVA ——>jvm ->os(缓冲区)    缓冲区一次多个读
     *
     * */
    private static void bufferInputStream() throws IOException {
        FileInputStream fis=new FileInputStream("FileOutPutStrame2.txt");
        BufferedInputStream bis=new BufferedInputStream(fis);
        int len=0;
        byte[] bytes = new byte[1024];
        while ((len = bis.read(bytes)) != -1) {
            System.out.println(new String(bytes));
        }

    }


字符输出流(内存--->硬盘)

FileOutputStream

package IOTest;

import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by kumamon on 2021/5/27.
 *
 *  字节输出流
 *  由 内存 ->输出到 硬盘
 */
public class FileOutputStreamDemo {


    public static void main(String[] args) throws IOException {

        //fileOutputStream1();

        //fileOutputStream2("字符串输出流");

        fileOutputStream();
    }


    /**
     * 存储多个字节
     * */
    private static void fileOutputStream2(String text) throws IOException {

        byte[] bytes = text.getBytes();

        FileOutputStream fos= null;
        try {
            fos = new FileOutputStream("FileOutPutStrame2.txt"); //此项目的根目录 E:\\Java_demo\\
            //fos = new FileOutputStream("\\FileOutPutStrame.txt");  //此项目的根盘 E:\\
           // fos.write(bytes,0,bytes.length);
            fos.write(bytes);  //
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            fos.close();
        }
    }

    /**
     * 存储单个字节
     * */
    private static void fileOutputStream1() throws IOException {
        FileOutputStream fos= null;
        try {
            fos = new FileOutputStream("FileOutPutStrame.txt"); //此项目的根目录 E:\\Java_demo\\
            //fos = new FileOutputStream("\\FileOutPutStrame.txt");  //此项目的根盘 E:\\
            fos.write(97);  //因为A的字节编码为 97  所以为A
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            fos.close();
        }
    }

    /**
     * 内容追加
     *  new FileOutputStream("FileOutPutStrame.txt",true);   //true追加    false不追加
     *
     *  测试追加测试追加测试追加测试追加
     *
     * */
    private static void fileOutputStream() throws IOException {
        FileOutputStream fos= null;
        try {
            fos = new FileOutputStream("FileOutPutStrame.txt",true); //此项目的根目录 E:\\Java_demo\\
            //fos = new FileOutputStream("\\FileOutPutStrame.txt");  //此项目的根盘 E:\\
            fos.write("测试追加".getBytes());  //因为A的字节编码为 97  所以为A
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            fos.close();
        }
    }

}

字符缓冲输出流

    /**
     * 现将数据写入到缓冲区
     * 然后讲缓冲区的数据 flush 刷新到硬盘
     * */
    private static void bufferOutPutStream() throws IOException {
        FileOutputStream fos=new FileOutputStream("bufferOutputStream.txt");
        BufferedOutputStream bufferedOutputStream =new BufferedOutputStream(fos);
        fos.write("我是缓冲字节输出流".getBytes());
        //fos.flush(); 可以省略
        fos.close();

    }


copy文件

package IOTest;

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

/**
 * Created by kumamon on 2021/5/27.
 * 将项目根目录的 1.jpg copy 项目根目录的2.jpg
 */
public class FileCopyDemo {
    public static void main(String[] args) {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream("E:\\Java_demo\\1.jpg");
            fos = new FileOutputStream("E:\\Java_demo\\2.jpg");
            int len = 0;
            byte[] bytes = new byte[1024];
            while ((len = fis.read(bytes)) != -1) {  //将1.jpg读出来的字节写入 bytes
                fos.write(bytes);                    //将bytes 写入2.jpg
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fis.close();
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

上一篇 下一篇

猜你喜欢

热点阅读