java IO 流

先来看看一个IO流的总体概况:


1:先来看看FileInputStream和FileOutputStream的用法:字节流的用法示例

 public static void main(String[] args) {
        //数据写入文件,这里加了中文涉及到乱码问题
        String a = "javaIO 流";
        FileOutputStream fo = null;
        FileInputStream fi = null;
        try {
            fo = new FileOutputStream("1.txt");
            byte[] bstr = a.getBytes("UTF-8");   //这里统一用UTF-8格式进行写入操作防止乱码的发生
            fo.write(bstr);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if(fo != null){
                    fo.close();
                    fo = null;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //从文件中读取数据
        File file = new File("1.txt");
        try {
            fi = new FileInputStream(file);
           byte[] buffer = new byte[1024];
            int len = fi.read(buffer);    //从byte数组中读取一定长度的数据如果不加buffer的话会出现乱码原因是read()方法跟系统平台的编码格式有关
            String re = new String(buffer,0,len,"UTF-8"); //规定编码格式为UTF-8防止读取的时候有乱码的发生
            System.out.println(re);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(fi != null){
                try {
                    fi.close();
                    fi = null;
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }

    }
2:再来看看FileRead和FileWriter的用法:字符流的用法

public static void main(String[] args) {
        
        //字符输出流
        FileWriter fw = null;
        try {
            fw = new FileWriter("2.txt");
            fw.write(a + 2);
            if(fw != null){
                fw.close();
                fw = null;
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        //从文件中读取数据:字符输入流
        File f = new File("2.txt");
        try {
            fReader = new FileReader(f);
            char[] buf = new char[1024];
            int len = fReader.read(buf);
            String res = new String(buf,0,len);
            System.out.println(res);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            if(fReader != null){
                try {
                    fReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                fReader = null;
            }
        }
        

    }



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值