java中io流的学习

IO

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

public class io03 {
    public static void main(String[] args) throws IOException {
        // io流分字节流和字符流   字符流的效率高
        // Read 字符输入流
        // read() 读取一个字节 read(byte[] b)
        readFile01();
    }

    public static void readFile01() throws IOException {
        String filePath = "D:\\JAVA\\IO\\one\\lx\\src\\Hellow.txt";
        int readData = 0;
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(filePath);
            // 创建对象用于读取文件
            // 读取一个字节 如果返回-1 则读取完成
            while ((readData = fileInputStream.read()) != -1) {
                System.out.print((char)readData);
            }
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            fileInputStream.close();
        }
    }
}

上为读取文件演示

public static void readFile02() throws IOException {
    // 使用read(byte[] b) 进行优化
    String filePath = "D:\\JAVA\\IO\\one\\lx\\src\\Hellow.txt";
    int readData = 0;
    // 定义字节数组
    byte[] buf = new byte[8];
    FileInputStream fileInputStream = null;
    try {
        fileInputStream = new FileInputStream(filePath);
        // 创建对象用于读取文件
        // 读取一个字节 如果返回-1 则读取完成
        while ((readData = fileInputStream.read(buf)) != -1) {
            System.out.print((char)readData);
        }
    } catch (FileNotFoundException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }finally {
        fileInputStream.close();
    }
}

使用字节数组优化代码

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

public class IO4 {
    public static void main(String[] args) throws IOException {
        writeFile();
    }

    // 演示使用FileOutputStream 将数据写入文件
    // 如果文件不存在 将数据写入文件
    public static void writeFile() throws IOException {
        // 创建对象
        String filePath = "D:\\clion\\a.txt";
        FileOutputStream fos = null;
        // 得到对象
        fos = new FileOutputStream(filePath);
        // 写入一个字节
        //fos.write('H');
        // 写入一个字符串
        String s = " hewllow world ";
        fos.write(s.getBytes());//将字符串转成字节数组再次写入
        fos.close();

    }
}

在文件中写入数据 注意 在文件中写入内容是复写原先的内容

public class io05 {
    public static void main(String[] args) throws IOException {
        copyFile();
    }
    // 文件拷贝
    public static void copyFile() throws IOException {
        // 完成文件拷贝
        // 思路分析  创建文件输入流 将文件读入到程序
        // 创建文件输出流 将文件输出
        // 完成程序时应该是读取部分数据就写入 要读取一次就写一次 使用循环写入
        String filePath = "D:\\clion\\a.txt";
        String destPath = "D:\\clion\\nest2.txt";
        FileInputStream fis = null;
        FileOutputStream fos = null;
        // 定义字节数组提高效率
        byte[] buf = new byte[1024];
        int readLen = 0;
        fis = new FileInputStream(filePath);
        fos= new FileOutputStream(destPath);
        while((readLen = fis.read(buf))!=-1){
            // 一边读一边写
            fos.write(buf,0,readLen);
        }
        System.out.println("拷贝成功!");
        fos.close();
        fis.close();
    }
}

文件拷贝

public class io06 {
    public static void main(String[] args) throws IOException {
        // 创建对象
//        String filePath = "D:\\clion\\Story.txt";
//        FileReader fr = new FileReader(filePath);
//        // 循环读取
//        // 1 单字符读取
//        int data;
//        while((data=  fr.read())!=-1){
//            System.out.print((char)data);
//        }
//        fr.close();
        // 使用字符数组读取文件
        int readLine = 0;
        char[] buf = new char[8];
        String filePath = "D:\\clion\\Story.txt";
        FileReader fr = new FileReader(filePath);
        // 循环读取
        // 1 单字符读取
        while((readLine=  fr.read(buf))!=-1){
            System.out.print(new String(buf,0,readLine));
        }
        fr.close();
    }
}

读取文件内容

import java.io.*;

public class io09 {
    public static void main(String[] args) throws IOException {
        // 处理流  处理二进制文件
        String srcFilePath = "D:\\clion\\Story.txt";
        String destFilePath = "D:\\clion\\a.txt";
        BufferedInputStream sif = new BufferedInputStream(new FileInputStream(srcFilePath));
        BufferedOutputStream bof = new BufferedOutputStream(new FileOutputStream(destFilePath));
        byte[] buff = new byte[1024];
        int readLine = 0;
        while((readLine=sif.read(buff))!=-1){
            bof.write(buff,0,readLine);
        }
        bof.close();
        sif.close();
    }
}

增强流 字符

public class io07 {
    public static void main(String[] args) throws IOException {
        String filePath = "D:\\clion\\Story.txt";
        // 创建对象
        // 一定要关流才能将数据真正地写入到
        FileWriter fw = new FileWriter(filePath);
        //1 写入单个字符
        //fw.write("H");
        //2 写一个数组进去
        //char[] chars = {'a','b','c'};
             fw.write(chars);
        // 3 字符转换
        fw.write("韩顺平".toCharArray(), 0, 3);
        fw.close();
    }
}

字节

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值