IO流:文件输入流、输出流,直接代码解析如何应用。

导读:本篇文章讲解 IO流:文件输入流、输出流,直接代码解析如何应用。,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

目录

一、FileInputStream 文件输入流

二、FileOutputStream 文件输出流 

三、FileReader 字符流

四、FileWriter 字符流 


一、FileInputStream 文件输入流

1、单个字节读取文件

   public void readFile01(){
        //文件路径
        String filePath = "d:\\hello.txt";

        //用于接收单个字节
        int readData = 0;

        //初始化FileInputStream,首先置为空
        FileInputStream fileInputStream = null;

        try {

            //创建FileInputStream 对象,用于读取 文件
            fileInputStream = new FileInputStream(filePath);

            //从该输入流读取一个字节的数据,如果没有输入可用,此方法将被阻止
            //如果返回-1,表示读取完毕
            while((readData = fileInputStream.read()) != -1){

                System.out.print((char) readData);//转成char显示

            }

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

            //关闭文件流,释放资源
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

2、使用 read(byte[] b) 进行优化

public void readFile02(){
        //文件路径
        String filePath = "d:\\hello.txt";

        //首先定义一个字节数组
        byte[] buf = new byte[8]; //一次性读取8个字节

        //读取长度
        int readLength = 0;

        //初始化FileInputStream ,置为空
        FileInputStream fileInputStream = null;

        try {

            //创建FileInputStream 对象,用于读取 文件
            fileInputStream = new FileInputStream(filePath);

            //从该输入流读取最多b.length字节的数据到字节数组。如果没有输入可用,此方法将被阻止
            //如果返回-1,表示读取完毕
            //如果读取正常,返回实际读取的字节数
            while((readLength = fileInputStream.read(buf)) != -1){

                System.out.print(new String(buf , 0 , readLength));//显示

            }

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

            //关闭文件流,释放资源
            try {

                fileInputStream.close();

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

二、FileOutputStream 文件输出流 

1、创建方式:
  (1)new FileOutputStream(filePath) 的创建方式,当写入内容是会覆盖原来的内容
 
  (2)new FileOutputStream(filePath,true) 的创建方式,当写入内容是,追加到到文件的尾部,不会覆盖

2、代码示例:
public void writeFile(){

        //创建FileOutputStream 对象
        String filePath = "d:\\a.txt";
        FileOutputStream fileOutputStream = null;

        try {

            //得到FileOutputStream 对象
            fileOutputStream = new FileOutputStream(filePath);


            //写入一个字节
            fileOutputStream.write('H');//char 和 int 之间可以自动转换


            //写入字符串
            String str = "hello,world";
            //str.getBytes() 可以把 字符串 -> 字节数组
            fileOutputStream.write(str.getBytes());


            //write(byte[] b,int off,int len) ,将len 字节从位于偏移量off指定字节数组写入
            fileOutputStream.write(str.getBytes(),0,str.length());

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

            try {

                fileOutputStream.close();

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

        }

    }

3、文件拷贝代码示例

public class FileCopy {
    public static void main(String[] args) {

        //将d:\\by.png 拷贝到 d:\\bycopy.png
        
        //源文件路径
        String srcFilePath = "d:\\by.png";
        //新文件路径
        String destFilePath = "d:\\bycopy.png";
        
        //初始化FileInputStream 输入流
        FileInputStream fileInputStream = null;
        //初始化FileOutputStream 输出流
        FileOutputStream fileOutputStream = null;

        try {
            //获取输入流对象
            fileInputStream = new FileInputStream(srcFilePath);
            //获取输出流对象
            fileOutputStream = new FileOutputStream(destFilePath);

            //定义一个字节数组,提高效率
            byte[] buf = new byte[1024];
            //获取数组长度
            int readLen = 0;

            while ((readLen = fileInputStream.read(buf)) != -1){

                //读取到后,就写入到文件 通过 fileOutputStream ,即边读边写
                fileOutputStream.write(buf,0,readLen); //一定要使用这个方法

            }
            System.out.println("拷贝成功");

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //输入流和输出流都要关闭
            try {
                if (fileInputStream != null){
                    fileInputStream.close();
                }
                if (fileOutputStream != null){
                    fileOutputStream.close();
                }

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

三、FileReader 字符流

1、相关方法:

IO流:文件输入流、输出流,直接代码解析如何应用。 

方法 功能
new FilleReader(File/String) 创建对象
read( ) 每次读取单个字节,返回该字符,如果到文件末尾返回-1
read( char[] ) 批量读取多个字符到数组,返回读取到的字符数,如果到文件末尾,则返回-1
new String( char[] ) 将 char[ ] 转换成String
new String(char[] , off, len) 将 char[] 的指定部分转换成String

2、代码示例:单个字符进行读取

    public void readFile01(){
        //文件路径
        String filePath = "d:\\story.txt";

        //初始化对象
        FileReader fileReader = null;

        //接收单个字节
        int data = 0;

        try {
            //1、创建一个FileReader 对象
            fileReader = new FileReader(filePath);

            //循环读取,使用read ,单个字符读取
            while ((data = fileReader.read()) != -1){

                System.out.print((char) data);

            }

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

            try {
                fileReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

3、代码示例:字符数组进行读取

    public void readFile02(){
        //文件路径
        String filePath = "d:\\story.txt";
        //初始化对象
        FileReader fileReader = null;
        
        //字符长度
        int readLen = 0;
        //创建数组
        char[] buf = new char[8];

        try {
            //1、创建一个FileReader 对象
            fileReader = new FileReader(filePath);

            //循环读取,使用read(buf) ,返回的是实际读取到的字符数
            //如果返回-1,说明文件结束
            while ((readLen = fileReader.read(buf)) != -1){

                System.out.print(new String(buf,0,readLen));

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

            try {
                fileReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

四、FileWriter 字符流 

1、相关方法

IO流:文件输入流、输出流,直接代码解析如何应用。

方法 功能
new FileWriter(File/String) 覆盖模式,相当于流的指针在首端
new FileWriter(File/String , true) 追加模式,相当于流的指针在尾端
write(int a) 写入单个字符
write(char[] ) 写入指定数组
write(char[] , off , len) 写入指定数组的指定部分
write (string b) 写入整个字符串
write(string , off , len) 写入字符串的指定部分

2、代码示例

public class FileWriter_ {
    public static void main(String[] args) {
        //文件路径
        String filePath = "d:\\note.txt";

        //创建FileWriter 对象
        FileWriter fileWriter = null;
        
        char[] chars = {'a','b','c'};

        try {
            //获取对象
            fileWriter = new FileWriter(filePath);//默认是覆盖写入

            //write(int):写入单个字符
            fileWriter.write('H');

            //write(char[]): 写入指定数组
            fileWriter.write(chars);

            //write(char[] ,off,len) : 写入指定数组的指定部分
            fileWriter.write(chars , 0 , 2);
            fileWriter.write("努力学习".toCharArray(), 0 , 2);

            //write(String) : 写入整个字符串
            fileWriter.write("努力学习");

            //write(String,off,len) : 写入字符串的指定部分
            fileWriter.write("继续加油", 0 , 2);


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

            try {
                //必须要关闭流,否则无法写入
                //fileWriter.flush();
                fileWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由半码博客整理,本文链接:https://www.bmabk.com/index.php/post/119681.html

(0)

相关推荐

发表回复

登录后才能评论
半码博客——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!