Java:输入流、输出流的使用

InputStream使用示例:

//第一种实现方式:每次按照一个字节接收直到文件结束
package com.wyz.test;

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

public class test {
    public static void main(String[] args) {
        String filePath = "E:\\hello.txt";
        int readData = 0;
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(filePath);
            while ((readData = fileInputStream.read()) != -1){
                System.out.print((char) readData);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
//第二种实现方式:按照自定义数组的大小接收字节直至文件结束
package com.wyz.test;

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

public class test {
    public static void main(String[] args) {
        String filePath = "E:\\hello.txt";
        byte[] buff = new byte[8];
        int readLength = 0;
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(filePath);
            while ((readLength = fileInputStream.read(buff)) != -1){
                System.out.print(new String(buff, 0, readLength));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

OutputStream使用示例:

package com.wyz.test;

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

public class test {
    public static void main(String[] args) {
        String filePath = "E:\\hello.txt";
        int readData = 0;
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(filePath); //这样使用是覆盖文件原来的内容
//          下面一条语句是追加内容到文件末尾,不会覆盖文件之前的内容
//          fileOutputStream = new FileOutputStream(filePath, true);
            fileOutputStream.write('H');
            String str = "Hello OutputStream!";
            fileOutputStream.write(str.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

FileReader使用示例:

//第一种实现方式:每次按照一个字符接收直到文件结束
package com.wyz.test;

import java.io.FileReader;
import java.io.IOException;

public class test {
    public static void main(String[] args) {
        String path = "E:\\hello.txt";
        FileReader fileReader = null;
        int data = 0;
        try {
            fileReader = new FileReader(path);
            while((data = fileReader.read()) != -1){
                System.out.print((char) data);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(fileReader != null){
                    fileReader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
//第二种实现方式:按照自定义数组的大小接收字符直至文件结束
package com.wyz.test;

import java.io.FileReader;
import java.io.IOException;

public class test {
    public static void main(String[] args) {
        String path = "E:\\hello.txt";
        FileReader fileReader = null;
        char[] buff = new char[8];
        int readLength = 0;
        try {
            fileReader = new FileReader(path);
            while((readLength = fileReader.read(buff)) != -1){
                System.out.print(new String(buff, 0, readLength));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(fileReader != null){
                    fileReader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

FileWriter使用示例:

package com.wyz.test;

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

public class test {
    public static void main(String[] args) {
        String path = "E:\\hello.txt";
        FileWriter fileWriter = null;
        char[] chars = {'h', 'e', 'l', 'l', 'o'};
        try {
            fileWriter = new FileWriter(path);
            fileWriter.write("H"); //写入单个字符
            fileWriter.write(chars); //写入数组
            fileWriter.write("Hello FileWriter!".toCharArray(), 0, 5); //写入数组的指定部分
            fileWriter.write("Hello FileWriter!"); //写入指定字符串
            fileWriter.write("Hello FileWriter!", 0, 5); //写入字符串的指定部分
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //attention:一定要关闭流或者flush才能真正把数据写入到文件
            try {
                fileWriter.close(); //close()底层调用了flush()并关闭流
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
  • 10
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wistain

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值