zoukankan      html  css  js  c++  java
  • 字符流;字节流;带缓冲的输入输出流;以及用scanner读文件

    概念:

    InputStream类是字节输入流的抽象类,是所有字节输入流的父类。

    OutputStream类是字节输入流的抽象类,是所有字节输出流的父类。

    In(可以理解为读)Out(可以理解为写)

    一、字节流的读与写

    FileInputStream类与FileOutStream类都是用来操作磁盘文件的。

    1.

    FileInputStream(String name); 填String类型的路径名

    FileInputStream(File file); 填入File对象

    2.

    FileOutStream(String name); 填String类型的路径名

    FileOutStream(File file); 填入File对象

    实例如下:

    package com.inba.maya.liu;
    import java.io.*;
    public class TextSteram {
    
        public static void main(String[] args) throws Exception{
            //读,字节流
            String s="g:\aaa.txt";
            File f=new File(s);
            
            FileInputStream fs=new FileInputStream(f);
            //byte[] b=new byte[10000];在字节流中既可以自定义长度,也可以用对象.length,但注意一定要转换成int类型
            
            byte[] b=new byte[(int)f.length()];1
            fs.read(b);
            fs.close();
            
            String str=new String(b);
            System.out.println(str);
            
            
            //写,字节流
            /*//查文档
            String str="66666";
            String s="g:\bbb.txt";
            File f=new File(s);
            FileOutputStream fos=new FileOutputStream(f);
            byte[] b=str.getBytes();
            fos.write(b);
            fos.close();
            */
            
            /*
            String str="
    7777777";
            String s="g:\bbb.txt";
            FileOutputStream fos=new FileOutputStream(s,true);
            byte[] b=str.getBytes();
            fos.write(b);
            fos.close();
            */
            
            /*
             * FileInputStream(f),写
             * FileInputStream(f,true),追加
             * FileInputStream(s); 写
             * FileInputStream(s,true); 追加
             * 
             * FileOutputStream(s); 写
             * FileOutputStream(s,true); 追加
             * FileOutputStream(f),写
             * FileOutputStream(f,true),追加
             * */
        }
    }

    二、字符流读与写

    为什么要使用字符流以及字符流与字节流有什么不同:

    使用FileOutStream类向文件中写入数据与使用FileInStream类读取文件中的数据,存在一点不足,即这两种类只能提供字节或字节数组的读取方式,由于汉字在文件中占用两个字节,如果使用字节流,读取不好会出现乱码现象,此时就需要采用字符流FileReader和FileWriter类就会避免这种现象。

    用到的方法:read();读 、 write();写

    同样的FileReader和FileWriter,也加载了两个构造函数,可以直接填写字符串类型的路径,也可以填写File对象;

    1.

    FileReade(String fileName); 填String类型的路径名

    FileReade(File file); 填入File对象

    2.

    FileWriter(String fileName); 填String类型的路径名

    FileWriter(File file); 填入File对象

    实例:

    package com.inba.maya.liu;
    
    import java.io.*;
    import java.util.*;
    public class TextZiFu {
    
        public static void main(String[] args) throws Exception{
            //读,字符流不能用.length
            
            String s="g:\aaa.txt";
            File f=new File(s);
            FileReader fr=new FileReader(f);
            char[] c=new char[5000];
            fr.read(c);
            fr.close();
            String str=new String(c);
            System.out.println(str);
            
            //写字符流
            /*
            String s="g:\qqq.txt";
            FileWriter fw=new FileWriter(s,true);
            
            fw.write("
    9999999");
            fw.close();
            */
        }
    }

    三、带缓存的输入输出流

    缓存可以说是I/O的一种性能优化,缓存流为I/O流增加了内存缓存区。有了缓存去,使得在流上执行skip();mark();reset();方法成为可能

    BufferedInputStream类(读缓存流)可以对任意的InputStream类进行带缓存区的包装以达到性能的优化。(所以一般是用于包装字节流的!)

    同样的BufferedInputStream类与BufferedOutputStream类都有两个构造函数;

    1.

    BufferedInputStream(输入流)

    BufferedInputStream(输入流 , int size); size:按照指定大小创建缓存区

    2.

    BufferedOutputStream(输出流)

    BufferedOutputStream(输出流 , int size); size:按照指定大小创建缓存区

    实例:

    package com.inba.maya.liu;
    
    import java.io.*;
    import java.util.*;
    public class TextZiFu {
    
        public static void main(String[] args) throws Exception{
    
            //读,缓存流
            /*
            String s="g:\aaa.txt";
            File f=new File(s);
            FileInputStream fis=new FileInputStream(s);
            
            BufferedInputStream bis=new BufferedInputStream(fis);
            byte[] b=new byte[(int)f.length()];
            bis.read(b);
            
            bos.close();//一定要先关缓存流
            fos.close();//再关流
            
            String str=new String(b);
            System.out.println(str);
            */
            
            //写,缓存流
            String s="g:\qqq.txt";
            
            FileOutputStream fos=new FileOutputStream(s,true);
            BufferedOutputStream bos=new BufferedOutputStream(fos);
            String str="
    1111111";
            byte[] b=str.getBytes();
            bos.write(b);
            bos.flush();//写的时候调用一下flush();彻底完成输入并清除缓存区
            bos.close();//一定要先关缓存流
            fos.close();//再关流
        }
    }

    四、用scanner读取数据:

    package com.inba.maya.liu;
    import java.io.*;
    import java.util.*;
    public class TextZiFu {
    
        public static void main(String[] args) throws Exception{
            /*
            String s="g:\aaa.txt";
            File f=new File(s);
            Scanner sc=new Scanner(f);
            
            //hasNextLine:如果在此扫描器的输入中存在另一行,则返回 true
            while(sc.hasNextLine()){
                System.out.println(sc.nextLine());
            }
            sc.close();
            */
            
            // 构造一个不带任何字符的字符串生成器,其初始容量为 16 个字符。
            StringBuilder sb=new StringBuilder();
            String s="g:\aaa.txt";
            File f=new File(s);
            Scanner sc=new Scanner(f);
            while(sc.hasNextLine()){
                sb.append(sc.nextLine()+"
    ");
            }
            sc.close();
            System.out.print(sb);
        }
    }
  • 相关阅读:
    element-ui的table表格控件表头与内容列不对齐问题
    uniapp 调用手机相机拍照实现图片上传
    配置Git忽略文件
    【转载】Java的几种常见排序算法
    httpclient封装
    idea 启动springboot项目时报错:org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource
    shell脚本监控网站,异常则进行邮件报警
    nginx配置图片跨域访问
    Nginx基于多端口、多域名配置
    docker部署Nginx项目dockerfile
  • 原文地址:https://www.cnblogs.com/AnswerTheQuestion/p/6267139.html
Copyright © 2011-2022 走看看