栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java的IO流操作

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java的IO流操作

一、IO的分类 1、按流向分类(以java程序为中心)

输入流 读取数据

输出流 写出数据

2、按数据类型分类

字节流:

字节输入流 读取数据 InputStream

字节输出流 写出数据 OutputStream

字符流:

字符输入流 读取数据 Reader

字符输出流 写出数据 Writer

如何区分使用的是字节流还是字符流呢?

使用windows自带的记事本打开。如果能够看懂里面的内容,就是用字符流,如果不能够读懂,就使用字节流

字节流可以读取任何数据,如果不懂用什么,就使用字节流

二、字节流的输出操作 1、往指定文件写入数据

经过上面的分析,我们最后采用的是,OutputStream ,通过观察API发现,OutputStream这个类是一个抽象类,而抽象类不能被实例化 要想实例化,我们就必须找到一个具体的实现子类

FileOutputStream

FileOutputStream(File file) 创建文件输出流以写入由指定的 File对象表示的文件。

FileOutputStream(String name) 创建文件输出流以指定的名称写入文件。

public class IODemo1 {
    public static void main(String[] args) throws IOException {
        File file = new File("C:\Users\邹政\Desktop\测试\a.txt");
        //获取要输出到的文件,如果没有就自动创建,如果指定路径不存在则会抛出异常,默认最后一行是文件
        FileOutputStream fos = new FileOutputStream(file);

        fos.write("请继续加油".getBytes());
        //如果想要输入的数据换行,则使用rn
        fos.write("rn".getBytes());
        fos.write("请继续加油1".getBytes());
        fos.write("rn".getBytes());
        fos.write("请继续加油2".getBytes());
        fos.write("rn".getBytes());
        fos.write("请继续加油3".getBytes());
        fos.close();
    }
}

2、写入文件的几个方法

void write(byte[] b)

将 b.length个字节从指定的字节数组写入此文件输出流。

void write(byte[] b, int off, int len)

将 len字节从位于偏移量 off的指定字节数组写入此文件输出流。

void write(int b)

将指定的字节写入此文件输出流。

3、追加内容到写入的数据

public FileOutputStream(String name, boolean append)

public class IODemo2 {
    public static void main(String[] args) throws IOException {
        //向文件中追加写入
        File file = new File("C:\Users\邹政\Desktop\测试\a.txt");
        FileOutputStream fos = new FileOutputStream(file,true);

        fos.write("rn".getBytes());
        fos.write("明天会更好".getBytes());
    }
}

追加内容的换行符:

Windows: rn

Mac: r

Linux: n

三、字节流的读取操作
public class IODemo3 {
    public static void main(String[] args) throws IOException {
        File file = new File("C:\Users\邹政\Desktop\测试\a.txt");

        //创建字节流输入对象
        FileInputStream fis = new FileInputStream(file);

        //read()一次只能够读取一个字节,显然要想读取全部内容必须使用循环
        //当读取到底时,会返回-1
        //读取方式一
        int b = 0;
        while ((b = fis.read()) != -1){
            System.out.print((char) b);
        }

        System.out.println();
        //读取方式二 byte[] bytes = new byte[13]     fis.read(bytes)
        //从该输入流中读取最多b.length个字节的数据作为字节数组
        //读取汉字最好使用字符流,否则很容易出现乱码
        FileInputStream fis1 = new FileInputStream(file);
        System.out.println("===========分割线===========");
        byte[] bytes = new byte[1024];
        int length = 0;
        while ((length = fis1.read(bytes)) != -1){
            String s = new String(bytes,0,length);
            System.out.print(s);
        }
        fis.close();
    }
}
四、字符流字节流实战(面面俱到)

import java.io.*;

public class FileCopyDemo1 {
    public static void main(String[] args) throws IOException {
        bufferReadCopy3("C:\Users\邹政\Desktop\测试\a.txt","C:\Users\邹政\Desktop\测试\b.txt");
    }


    //方式一、使用字节输入输出流拷贝文件
    //path1是输入地址
    //path2是输出地址
    //使用read一个一个读
    public static void copy1(String path1,String path2) throws IOException {
       FileOutputStream fos = new FileOutputStream(path2);
       FileInputStream fis = new FileInputStream(path1);

        int a;
       while ((a = fis.read()) != -1 ){
           fos.write(a);
           fos.flush();
       }

       fos.close();
       fis.close();
    }


    //使用数组一次多读
    public static void copy2(String path1,String path2) throws IOException {
        FileOutputStream fos = new FileOutputStream(path2);
        FileInputStream fis = new FileInputStream(path1);

        //从字节流中读取文件进行拷贝
        byte[] b = new byte[1024];
        int length;
        while ((length = fis.read(b)) != -1 ){
            fos.write(b,0,length);
            fos.flush();
        }
        fos.close();
        fis.close();
    }

    //方式二、使用字节缓冲流拷贝文件
    //一个一个读
    public  static void bufferCopy1(String path1,String path2) throws IOException {
        //输出流
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(path2));
        //输入流
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(path1));

        int a = 0;
        while ((a = bis.read()) != -1){
            bos.write(a);
            bos.flush();
        }

        bos.close();
        bis.close();

    }

    //方式二、使用字节缓冲流拷贝文件
    //使用数组一次读多个
    public  static void bufferCopy2(String path1,String path2) throws IOException {
        //输出流
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(path2));
        //输入流
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(path1));

        //创建一个字节数组
        int length;
        byte[] b = new byte[1024];
        while ((length = bis.read(b)) != -1){
            bos.write(b,0,length);
            bos.flush();
        }

        bos.close();
        bis.close();
    }

    //方式三、使用字符流(转换流)拷贝文件
    //一个一个读
    public  static void charCopy1(String path1,String path2) throws IOException {
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(path2));
        InputStreamReader isr = new InputStreamReader(new FileInputStream(path1));
        int length;
        while ((length = isr.read()) != -1){
            osw.write(length);
            osw.flush();
        }

        osw.close();
        isr.close();

    }

    //使用字符流拷贝问价
    //使用数组读取多个
    public  static void charCopy2(String path1,String path2) throws IOException {
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(path2));
        InputStreamReader isr = new InputStreamReader(new FileInputStream(path1));

        char[] c = new char[1024];
        while (isr.read(c) != -1){
            osw.write(c,0,c.length);
            osw.flush();
        }

        osw.close();
        isr.close();

    }

    //字符流(转换流的简化写法)
    //一次拷贝多个
    public  static void charCopy3(String path1,String path2) throws IOException {
        FileWriter fw = new FileWriter(path2);
        FileReader fr = new FileReader(path1);

        char[] c = new char[1024];
        while (fr.read(c) != -1){
            fw.write(c,0,c.length);
            fw.flush();
        }

        fr.close();
        fw.close();

    }

    //一次拷贝一个
    public  static void charCopy4(String path1,String path2) throws IOException {
        FileWriter fw = new FileWriter(path2);
        FileReader fr = new FileReader(path1);

        int c;
        while ((c = fr.read()) != -1){
            fw.write(c);
            fw.flush();
        }

        fr.close();
        fw.close();

    }

    //使用字符缓冲流
    //一个一个读
    public static void bufferReadCopy1(String path1,String path2) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(path1));
        BufferedWriter bw = new BufferedWriter(new FileWriter(path2));
        int b;
        while ((b =br.read())!=-1){
            bw.write(b);
            bw.flush();
        }

        br.close();
        bw.close();

    }

    //使用字符缓冲流
    //使用字符数组读
    public static void bufferReadCopy2(String path1,String path2) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(path1));
        BufferedWriter bw = new BufferedWriter(new FileWriter(path2));
        int length;
        char[] c = new char[1024];
        while ((length =br.read(c))!=-1){
            bw.write(c,0,length);
            bw.flush();
        }

        br.close();
        bw.close();

    }


    //使用字符缓冲流特有的功能
    //一行一行读
    public static void bufferReadCopy3(String path1,String path2) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(path1));
        BufferedWriter bw = new BufferedWriter(new FileWriter(path2));
        String s;
        while ( (s = br.readLine())!=null){
            bw.write(s);
            bw.newline();
        }

        br.close();
        bw.close();

    }

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/684878.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号