文件流FileReader读入数据的基本操作

导读:本篇文章讲解 文件流FileReader读入数据的基本操作,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

/**
 *
 * 一、流的分类:
 * 1.操作数据单位:字节流、字符流
 * 2.数据的流向:输入流、输出流
 * 3.流的角色:节点流、处理流
 *
 * 二、流的体系结构
 * 抽象基类         节点流(或文件流)                               缓冲流(处理流的一种)
 * InputStream     FileInputStream   (read(byte[] buffer))        BufferedInputStream (read(byte[] buffer))
 * OutputStream    FileOutputStream  (write(byte[] buffer,0,len)  BufferedOutputStream (write(byte[] buffer,0,len) / flush()
 * Reader          FileReader (read(char[] cbuf))                 BufferedReader (read(char[] cbuf) / readLine())
 * Writer          FileWriter (write(char[] cbuf,0,len)           BufferedWriter (write(char[] cbuf,0,len) / flush()
 *
 *
 */

文件流FileReader读入数据的基本操作

首先要将文件所处的绝对路径显示出来:

public static void main(String[] args) {
   File file = new File("hello.txt");//相较于当前工程;
   System.out.println(file.getAbsolutePath());//D:\java123\JavaSenior\hello.txt

   File file1 = new File("day09\\hello.txt");
                            
   System.out.println(file1.getAbsolutePath());//D:\java123\JavaSenior\day09\hello.txt
}

但是在单元测试中和在main方法中显示出来的绝对路径有所不同:

最后输出的跟上边的file1的绝对路径一致

public void testFileReader()throw IOException{

   //1.实例化File类的对象,指明要操作的文件
   File file = new File("hello.txt");//相较于当前Module;和之前的main中方法不一致
   System.out.println(file.getAbsolutePath());//D:\java123\JavaSenior\day09\hello.txt

   //2.提供具体的流
   fr = new FileReader(file);

   //3.数据的读入
   //read():返回读入的一个字符。如果达到文件末尾,返回-1
   //方式一:
   int data = fr.read();
   while(data != -1){
       System.out.print((char)data);//helloworld
       data = fr.read();
    }

   fr.close();
}

对read()操作升级:使用read的重载方法

    public void testFileReader1()  {
        //try-catch-finally用的快捷键是ALT+SHIFT+Z
        FileReader fr = null;
        try {
            //1.File类的实例化
            File file = new File("hello.txt");

            //2.FileReader流的实例化
            fr = new FileReader(file);

            //3.读入的操作
            //read(char[] cbuf):返回每次读入cbuf数组中的字符的个数。如果达到文件末尾,返回-1
            char[] cbuf = new char[5];
            int len;
            while((len = fr.read(cbuf)) != -1){
                //方式一:
                //错误的写法
//                for(int i = 0;i < cbuf.length;i++){
//                    System.out.print(cbuf[i]);//helloworld123ld
//                }
                //正确的写法
//                for(int i = 0;i < len;i++){
//                    System.out.print(cbuf[i]);
//                }
                //方式二:
                //错误的写法,对应着方式一的错误的写法
//                String str = new String(cbuf);//helloworld123ld
//                System.out.print(str);
                //正确的写法
                String str = new String(cbuf,0,len);
                System.out.print(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fr != null){
                //4.资源的关闭
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }

    }

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/83296.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!