为了账号安全,请及时绑定邮箱和手机立即绑定

【九月打卡】第11天 通过I/O实现文件的读取与写入(下)

标签:
活动

课程名称:Java体系课

章节名称:通过I/O实现文件的读取与写入(下)

课程内容:

  • 应用字符输入输出流实现文本读取与写入
  • 转换流的应用
  • 缓冲区的作用
  • 绝对路径与相对路径
  • URLConnection

学习收获:

  • Reader与Writer
    Reader是所有字符输入流的抽象父类
    Writer是所有字符输出流的抽象父类
    FileReader与FileWriter分别对应了文本文件的读取与写入(构造方法可以传入File对象,也可以直接传入完整路径名的字符串)
  • Writer写入文本文件
/*Writer写入文本文件过程*/
    public void writeTextFile(){
        Writer writer = null;
        try {
            File file = new File("d:/test.txt");
            //创建文件
            if (!file.exists()) {
                file.createNewFile();
            }
            //实例化writer对象
            writer = new FileWriter(file);
            //write()方法用于覆盖已有文件内容
            writer.write("这是一个新文件New");
            //append()方法用于在文件末尾追加
            writer.append(":Append内容");
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            //关闭writer对象
            if(writer != null){
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
  • Reader读取文本文件
/*FileReader读取文本文件案例*/
    public void readTextFile(){
        Reader reader = null;
        try{
            File file = new File("d:/test.txt");
            //实例化Reader对象
            reader = new FileReader(file);
            int ch = 0;
            //逐个字符读取
            while((ch = reader.read()) != -1){
                System.out.print((char)ch);//UTF-8编码集
            }
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            if(reader != null){
                try {
                    //关闭reader对象
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
  • 转换流的应用
    输入/输出体系中还提供了两个转换流,这两个转换流用于实现将字节流转换成字符流
    字符是字节的子集,所以没有提供字符输入流转换为字节输入流的
  • InputStreamReader将字节输入流转换成字符输入流
public void isrSample(){
       FileInputStream fis = null;
       InputStreamReader isr = null;
       try{
           File file = new File("d:/test.txt");
           fis = new FileInputStream(file);
           isr = new InputStreamReader(fis,"UTF-8");
           StringBuffer buffer = new StringBuffer();
           while(isr.ready()){
               buffer.append((char)isr.read());
           }
           System.out.println(buffer.toString());
       }catch (IOException e){
           e.printStackTrace();
       }finally {
           if(isr != null){
               try {
                   isr.close();
               } catch (IOException e) {
                   e.printStackTrace();
               }
           }
           if(fis != null){
               try {
                   fis.close();
               } catch (IOException e) {
                   e.printStackTrace();
               }
           }
       }
   }
  • OutputStreamWriter将字节输出流转换成字符输出流
//利用OutputStreamWriter写入文本文件
   public void oswSample() {
       FileOutputStream fos = null;
       OutputStreamWriter osw = null;
       try {
           File file = new File("D:/test.txt");
           //创建文件
           if (!file.exists()) {
               file.createNewFile();
           }
           fos = new FileOutputStream(file);
           osw = new OutputStreamWriter(fos, "UTF-8");
           osw.write("这是一个新文件!");
       } catch (IOException e) {
           e.printStackTrace();
       } finally {
           try {
               if (osw != null) {
                   osw.close();
               }
               if (fos != null) {
                   fos.close();
               }
           } catch (IOException e) {
               e.printStackTrace();
           }
       }
   }
  • 转换流使用意义
    上级只提供字节流,方法需要转化为字符流
    使用FileReader操作文本数据时,该对象使用的是默认的编码表,即GBK。汉字容易乱码。如果要使用指定编码表时,必须使用转换流。如utf-8
  • 缓冲区的作用
    默认文件的读取与写入都是逐个字节/字符完成的,但这种处理方式并不高效,如果将读取或写入的数据整块在内存中缓存,一次性批量读取、写入,便可以有效提高数据交互效率
    BufferedInputStream与BufferedOutputStream用于缓冲字节输入、输出流
    BufferedReader与BufferedWriter用于缓冲字符输入、输出流
public void readBuffer(){
        Reader reader =null;
        BufferedReader br = null;
        try{
            File file = new File("d:/FileSample.java");
            reader = new FileReader(file);
            br = new BufferedReader(reader);
            String line = null;
            while((line = br.readLine()) != null){
                System.out.println(line);
            }
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            try {
                if (br != null) {
                    br.close();
                }
                if(reader!= null){
                    reader.close();
                }
            }catch (IOException e){
                e.printStackTrace();
            }
        }
  • 绝对路径与相对路径
    文件或目录在硬盘上的绝对地址,在任意位置都可以通过这个路径直接访问
    文件或者目录相对于当前文件的地址,这个地址会随着当前文件的地址的改变而改变
  • URLConnection
  1. Internet上的每一个网页都具有一个唯一的名称标识,通常称之为URL(Uniform Resource Locator, 统一资源定位器)。它是www的统一资源定位标志,简单地说URL就是web地址,俗称“网址”。
  2. URL——类。openConnection()方法,返回一个链接到URL的URLConnection 。
  3. URLConnection——将URL转化为字节流的类。方法getInputStream()返回从此打开的连接读取的输入流。
public class URLConnectionSample {
    public static void main(String[] args) {
        InputStream is = null;
        OutputStream os = null;
        try {
            URL url = new URL("https://manongbiji.oss-cn-beijing.aliyuncs.com/images/weixin.jpg");
            URLConnection connection = url.openConnection();
            is = connection.getInputStream();
            os = new FileOutputStream("d:/weixin.jpg");
            byte[] bs = new byte[1024];
            int len = 0;
            while((len = is.read(bs)) != -1){
                //System.out.println(len);
                os.write(bs,0,len);
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(os != null){
                    os.close();
                }

                if (is != null) {
                    is.close();
                }
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }
}

课程截图

图片描述

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
JAVA开发工程师
手记
粉丝
0
获赞与收藏
0

关注作者,订阅最新文章

阅读免费教程

  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消