JavaSE基础--(二)IO部分

流的概念

在java中把不同的输入/输出抽象为流

流的分类

### 输入流和输出流 ###
    输入流:数据从硬盘到内存
    输出流:数据从内容到硬盘

### 字节流和字符流 ###
    字节流(抽象类):InputStream,OutputStream
        字节流主要是对非文本文件(如一些媒体文件 :图片,电影,音乐等)进行操作的注意:
        字节流可以对所有类型的文件进行操作。
        字节流读取中文(在Unicode编码上上两个字节)容易乱码
    字符流(抽象类):Reader,Writer    
        字符流只能对文本文件进行读取, 他比字节流对文本文件的操作效率高    

    字节流在操作时本身不会用到缓冲区(内存),是文件本身直接操作
    字符流在操作时使用了缓冲区,通过缓冲区再操作文件(必须关流,否则数据不能从缓冲区写入硬盘中)

### 节点流和处理流 ###
    按照流是否直接与特定的地方(如磁盘、内存、设备等)相连,分为节点流和处理流两类。

    #### 节点流 ####
        节点流可以从或向一个特定的地方(节点)读写数据。如FileReader
    #### 处理流 ####
        处理流:是对一个已存在的流的连接和封装,通过所封装的流的功能调用实现数据读写。
        如BufferedReader。
        处理流的构造方法总是要带一个其他的流对象做参数。

File

### file的作用 ###
    可利用file对象的方法去操作文件和目录
    FileInputStream 类或者 FileReader 类的构造函数有多个,如使用 File 对象为参数;
    使用表示路径的String对象作为参数;
    刚开始一直不明白为什么先构造一个 File 对象,现在终于明白了,“如果处理文件或者目录
    名,就应该使用 File 对象,而不是path字符串。

### File类方法 ###

    1 boolean createNewFile()   创建新文件
    2 boolean delete()          删除文件
    3 boolean mkdir()           创建一个指定file对象的目录
    4 boolean exist()           判断文件是否存在
    5 boolean isFile()          判断文是否是文件
    6 boolean isDirectory()     判断是否是目录
    7 String getName()          得到file对象的名字
    8 String getPath()          得到file对象的路径
    9 String[] list()           列出file对象所有子文件和路径
    10 long length()            返回file文件的长度

### 列出指定目录的全部文件(包括隐藏文件) ###
`public static void print(File f){
            //判断文件不为空
            if(f != null){
                //判断是否是文件夹
                if(f.isDirectory()){
                    //取出path下的所有文件(或文件夹)
                    File[] fileArray = f.listFiles();
                    //判断file是否为空
                    if(fileArray != null){
                        //遍历取出fileArray所有不为空的文件
                        for (int i = 0; i < fileArray.length; i++) {
                            //递归调用
                            print(fileArray[i]);
                        }
                    }
                }else{
                    // 输出文件
                    System.out.println(f);
                }
            }
        }`
### 文件过滤器(FilenameFileter接口)###
返回该目录下一级所有的*.java文件
    public class FilenameFilterTest
        {
            public static void main(String[] args) 
            {
                File file = new File(".");
                String[] nameList = file.list(new MyFilenameFilter());
                for(String name : nameList)
                {
                    System.out.println(name);
                }
            }
        }
        // 实现自己的FilenameFilter实现类
        class MyFilenameFilter implements FilenameFilter
        {
            public boolean accept(File dir, String name)
            {
                // 如果文件名以.java结尾,返回true
                return name.endsWith(".java");
            }
        }

FileReader与BufferedReader(BufferedWriter)的用途

FileReader是由java.io.InputStreamReade扩展来的,是针对文件读取的。
单独使用FileWriter 每写一个字符,硬盘就有一个写动作,效率低

BufferedReader由Reader类扩展而来,提供了很实用的readLine,读取分行文本很适合。
BufferedWriter:是给字符输出流提高效率用的(先写入缓冲区,最后统一写入硬盘),
缓冲区对象建立时,必须要先有流对象。

IO转换流的使用InputStreamReader与OutputStreamWriter ##

InputStreamReader将字节输入流转化为字符输入流
OutputStreamWriter将字节输出流转化为字符输出流

        File file = new File ("hello.txt");
        FileInputStream in=new FileInputStream(file);
        InputStreamReader inReader=new InputStreamReader(in);
        BufferedReader bufReader=new BufferedReader(inReader);

打印流(暂空)


IO规范用法

    //字节流直接操作文件
1   File file = new File ("hello.txt");
        FileInputStream in=new FileInputStream(file);

        //字符流操作时用到缓冲区
    2   File file = new File ("hello.txt");
        FileReader fileReader=new FileReader(file);
        BufferedReader bufReader=new BufferedReader(fileReader);

    3   File file = new File ("hello.txt");
        FileInputStream in=new FileInputStream(file);
        InputStreamReader inReader=new InputStreamReader(in);
        BufferedReader bufReader=new BufferedReader(inReader);

字节字符输入输出的四个case

    #### 字符输入流范例(Reader) ####
  `public static void main(String[] args) throws IOException {
                //根据path路径创建一个File
                File file = new File("E:\\MyText\\书单.txt");
                //创建字符输入流
                FileReader fileReader = null;
                //创建缓冲流流
                BufferedReader reader = null;
                try {
                    // 读取文件
                    fileReader = new FileReader(file);
                    // 通用的缓冲方式文本读取
                    reader = new BufferedReader(fileReader);
                    // 读取文本中的首行文件内容
                    String strLine = reader.readLine();
                    //循环读取直到读尽
                    while (strLine != null) {
                        strLine = reader.readLine();
                    }
                } catch (FileNotFoundException ex) {
                    ex.printStackTrace();
                } catch (IOException ix) {
                    ix.printStackTrace();
                } finally {
                    //关闭流(java1.7新特性,对于实现AutoCloseable接口的类try块自动关流)
                    if (reader != null) {
                        try {
                            reader.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }`
    #### 字符输出流范例(Writer) ####
`public static void main(String[] args) {
                //建立一个名为writerTest的file对象(作用:可操作file对象)
                File file = new File("E:\\writerTest.txt");
                //建立一个字符输出流来完成读取文件
                FileWriter fileWriter = null;
                //建立一个缓冲流包装fileWriter
                BufferedWriter writer = null;
                try {
                    //实例化FileWriter
                    fileWriter = new FileWriter(file);
                    //实例化BufferedWriter
                    writer = new BufferedWriter(fileWriter);            
                    //用write方法写入
                    writer.write("");
                    //换行写
                    writer.newLine();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    //判断读入的不为null时,关闭输出流
                    if(writer != null){
                        try {
                            writer.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }

            }`
    #### 字节输出流范例(OutputStream) ####
`public static void getText(String path){       
                try {   
                    //实例化一个file
                    File file = new File(path);
                    //如果file不存在,则创建一个新的文件
                    if(!file.exists()){
                         file.createNewFile();
                    }
                    //创建字符输入流
                    FileInputStream fis = new FileInputStream(file);
                    //创建一个长度为1024的竹筒
                    byte[] bbuf = new byte[1024];
                    //用于保存时机读取的字字节数
                    int hasRead = 0;
                    //循环取出“竹筒”中的水滴,
                    while ((hasRead = fis.read(bbuf)) > 0){
                        //取出“竹筒”中的水滴,,将字节数组转化为字符串输入
                        System.out.println(new String(bbuf,0,hasRead));
                    }
                    //关闭文件输入流,放在finally块中更安全
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }`
    #### 字节输出流范例(OutputStream) ####
`public static void main(String[] args){
                try(
                    // 创建字节输入流
                    FileInputStream fis = new FileInputStream("FileOutputStreamTest.java");
                    // 创建字节输出流
                    FileOutputStream fos = new FileOutputStream("newFile.txt"))
                {
                    byte[] bbuf = new byte[32];
                    int hasRead = 0;
                    // 循环从输入流中取出数据
                    while ((hasRead = fis.read(bbuf)) > 0 )
                    {
                        // 每读取一次,即写入文件输出流,读了多少,就写多少。
                        fos.write(bbuf , 0 , hasRead);
                    }
                    //关流,放在finally快中更好
                    write.close();
                }
                catch (IOException ioe)
                {
                    ioe.printStackTrace();
                }
            }`
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值