javaSE IO流(二)—— 各种各样的流

javaSE IO流(二)—— 各种各样的流,第1张

一、Java IO

1. 关于C++在I/O流上的 *** 作,之前也整理过一点。

C语言fputs fgets fputc fgetc fscanf fprintf及fopen *** 作的整理_玛丽莲茼蒿的博客-CSDN博客_fgetc fscanf最近写了一些文件 *** 作的程序,频繁使用下面的函数。所以整理一下。一、fopen函数fopen函数的一般用法FILE *f;if((f=fopen("1.txt","这里写文件 *** 作"))==NULL){ printf("Open file failed\n");}1.以只读方式打开文件。fopen第二个参数为r。要求文件“1.txt”必须存在,不存在的话只读方式不会帮你创建的FILE *f;if((f=fopen("1.txt","r"))==NULL){ //打开只读文件phttps://blog.csdn.net/qq_44886213/article/details/1173658072. 输入是输出是相对来说的(一个程序和文件进行通信,文件的输出对于程序来说是输入),那我们站位在什么角度呢?

答:站位在程序(内存)的角度。内存读取文件叫输入,往文件里写东西叫输出。

3. 一般I/O有两种

以前接触过的是对文件的I/O,还有一种是网络传输,两个网络设备之间的I/O。

4. 流的分类

 理解节点流和处理流:从进行I/O的两方之间简单建立起来的I/O流叫节点流。如果想让这个节点流传输得更快或者更安全,就要在这个节点流外面再包一层流,叫处理流。

5. IO流的体系

四个抽象基类:

字节流:InputStream、OutputStream

字符流:Reader 、Writer

 哪些是重点呢?

6、在Main函数中和Test单元测试中,文件路径是不同的

二、访问文件的4个节点流 2.1 FileReader 1、 FileReader类记忆点
  • 为了保证close *** 作一定执行,需要使用try-catch-finally
  • 读入的文件一定要存在。不然在调用new FileReader的时候会报“FileNotFound”异常
2、 FileReader类常用方法

(1)构造方法

FileReader fr = new FileReader(file);

(2)int read()

一次只能读一个字符,直接作为返回值返回。

注意:返回的不是char类型,而是char对应的int。返回值-1时,表示读到文件末尾

(3)int read(char [])

一次读多个字符,读入到char[] 数组中

注意:返回的依然是int类型“每次实际读出的个数”。返回值-1时,表示读到文件末尾

(4)close

3、int read()一次读一个字符 *** 作演示

首先,我们在对应的Module下建立一个hello.txt文件,输入内容“hello world!!!”。

    @Test
    public void test01() throws IOException {
        //1)创建File对象
        File file = new File("hello.txt");

        //2)创建FileReader对象
        FileReader fr = new FileReader(file);

        //3) 读文件
        int data;  
        while((data = fr.read()) !=-1){
            System.out.print((char)data);
        }

        //4)关闭流
        fr.close();
    }

运行结果:

hello world!!! 

但这个代码当中存在一些问题,需要优化一下:

现在是在整个函数外抛出异常,如果我的read函数出错了,那么程序中止,会导致前面打开的流没有close。换句话说,close *** 作是必须要执行的,那就用try-catch-finally语句。

        1)选中要被try包含的代码,按住快捷键Ctrl+Alt+t,选中“try/catch/finally” 

        

        2)由于close本身也需要做异常处理。所以close也要用try-catch包起来。

        

          3)这里还有一个问题。如果不是在read处出错,而是在new FileReader的时候就出错了,fr没有创建成功,那么进入finally的fr.close()时,会出现“空指针异常”。所以我们在进行close *** 作的时候要先判断fr是不是空指针

        

         优化后的完整代码:

@Test
    public void test01() {
        FileReader fr = null;
        try {
            //1)创建File对象
            File file = new File("hello.txt");

            //2)创建FileReader对象
            fr = new FileReader(file);

            //3) 读文件
            int data;
            while((data = fr.read()) != -1){
                System.out.println((char)data);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4)关闭流
            try {
                if(fr != null)
                    fr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }
4、一次读多个字符int read(char[] ) *** 作演示 

难点:错误写法那里,不能用buf.length,而要用len。

  @Test
    public void test02(){
        FileReader fr = null;
        try {
            //1)创建File对象
            File file = new File("hello.txt");

            //2)创建FileReader对象
            fr = new FileReader(file);

            //3)读文件
            char []buf = new char[5];
            int len;
            while((len = fr.read(buf))!= -1){
                /*------输出方式(一)------*/
                //错误写法
//                for(int i=0; i
2.2 FileWriter 2.2.1 FileWriter类的记忆点
  • 为了保证close *** 作一定执行,需要使用try-catch-finally
  • 输出的文件可以不存在。不存在的话会自动创建
  • “append追加”还是覆盖
2.2.2 FileWriter类的常用方法

(1)构造方法

fw = new FileWriter(file, true); //true为追加

fw = new FileWriter(file, false); //false为不追加
//等同于fw = new FileWriter(file);

(2)write(char)

        一次写入一个字符

(3)write(char [] )

        一次写入多个字符

(4)write(String)

        一次写入一个字符串

2.2.3 三个常用write()方法的一波 *** 作
     @Test
    public void test01(){
        FileWriter fw = null;
        try {
            //1)创建File对象
            File file = new File("notExist.txt");

            //2)创建FileWriter对象
            fw = new FileWriter(file, true);

            //3)写入 *** 作
            fw.write('3');
            fw.write(new char[]{'S','e','p','t','e','m','b','e','r'});
            fw.write("You must find more time on this work");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4) 关闭流
            try {
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

运行结果: 

3SeptemberYou must find more time on this work

 如果想要换行,就输入“\n”

2.3 使用FileReader和FileWriter实现文本文件的复制

 模板:

        //1)创建File对象:读出和写入各一个
        
        //2)创建FileReader对象 和 FileWriter 对象
        
        //3)读出和写入 *** 作
        
        //4)关闭两个流

完整代码

     @Test
    public void test02() {
        FileReader fr = null;
        FileWriter fw = null;
        try {
            //1)创建File对象:读出和写入各一个
            File readFile = new File("hello.txt");
            File writeFile = new File("notExist.txt");

            //2)创建FileReader对象 和 FileWriter 对象
            fr = new FileReader(readFile);
            fw = new FileWriter(writeFile, true);

            //3)读出和写入 *** 作
            char []buf = new char[5];
            int len;
            while((len = fr.read(buf)) != -1){
//                //方式(一)
//                String s = new String(buf, 0, len);
//                fw.write(s);
                //方式(二)
                fw.write(buf,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                //4)关闭两个流
                if(fr != null)
                    fr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

            try {
                //4)关闭两个流
                if(fw != null)
                    fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

PS:发现File类中有创建文件、删除文件,但好像并没有拷贝文件的 *** 作。所以不能通过file.copy(源文件,目的文件)一行代码直接实现吗 

那能否用FileWriter和FileReader去拷贝一张图片呢?

答:不能。

 出来的bbb.jpg是打不开的。

2.4 FileInputStream 2.5 FileOutoutStream

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/2991038.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-09-23
下一篇 2022-09-23

发表评论

登录后才能评论

评论列表(0条)