java-17-IO流(FileReader&FileWriter)

来源:互联网 发布:萬能网络视频下载神器 编辑:程序博客网 时间:2024/06/05 19:20

1.输入输出流,是相对于设备而言的;简写模式为IO流。

将外设中的数据读入内存中:输入,读。将内存中的数学写入外设中:输出,写。按操作的数据分为字节流和字符流。字节流的两个顶层父类:InputStream,OutputStream字符流的两个顶层父类:Reader输入,Writer输出。这些体系的子类都以父类名作为后缀。前缀名就是该对象的功能。

2.字符流:

其实就是:字节流读取文字字节数据后,不直接操作而是先查指定的编码表,获取对应的文字。再对文字进行操作,简单的来说就是:字节流+编码表=字符流。

3.FileWriter

/*需求1:将一些文字存储到硬盘的一个文件中。如果要操作文字数据,优先考虑字符流。*/public class FileWriterDemo {    private static final String LINE_SEPARATOR = System.getProperty("line.separator");    public static void main(String[] args) throws IOException {        //创建一个可以往文件中写入字符数据的输出流对象。        /*        * 既然是往一个文件中写入文字数据,那么在创建对象时,就必须明确该文件(用于存储数据的目的地).        *        * 如果文件不存在则会自己创建,如果文件存在则会覆盖。        *        * 如果构造函数中加入true,可以实现对文件的续写。        * */        FileWriter fw = new FileWriter("demo.txt",true);        /*        * 调用Writer中的write(String)方法,写入数据。        *        * 其实数据写到了临时存储缓冲区中。        *        * */        fw.write(LINE_SEPARATOR+"abcdef"+"ajksjaksaksa");        /*        * 进行刷新,将数据直接写到目的地。        * *///        fw.flush();        /*        * 关闭流,关闭资源。        *在关闭前,会调用flush刷新缓冲区的数据到目的地。        * */        fw.close();//        fw.write("haha");//关闭流后不能写入,否则抛出java.io.IOException    }
public class IOException {    private static final String LINE_SEPARATOR = System.getProperty("line.separator");    public static void main(String[] args)  {        FileWriter fw = null;//对象在外面声明,里面初始化        try {            fw = new FileWriter("k:\\demo.txt");            //如果出现异常,则对象创建不出来,fw = null;            fw.write(LINE_SEPARATOR+"abcdef"+"ajksjaksaksa");        } catch (java.io.IOException e) {            e.printStackTrace();        } finally{            if (fw != null)//要判断fw是否为空。只有不空才能更有关闭的资格,否则会出现java.lang.NullPointerException                try {                    fw.close();                } catch (java.io.IOException e) {                    throw new RuntimeException("关闭失败");                }        }    }

4.FileReader

/*需求2:读取一个文本文件,读取到的字符打印在控制台。找到了FileReader.*/public class FileReaderDemo {    public static void main(String[] args) throws IOException {//        创建读取字符数据的流对象。        /*        *在创建读取流对象时,必须要明确被读取的文件,一定要确定该文件是存在的。        *        * 用一个读取流关联一个已经存在文件。        * */        FileReader fr = new FileReader("demo.txt");        //用reader中的方法read方法读取字符。        int ch = 0;        while ((ch = fr.read())!=-1){            System.out.print((char)ch);        }        fr.close();    }
public class FileReaderDemo2 {    public static void main(String[] args) throws IOException {        FileReader fr = new FileReader("demo.txt");        /*        * 使用read(char[])读取文本数据。        *        * 首先创建字符数组。        * */        char[] buf =new char[1024];        int len = 0;        while ((len = fr.read(buf))!=-1){            System.out.print(new String(buf,0,len));        }       /* int num = fr.read(buf);//将读到的字符存储到数组中。        System.out.println(num+":"+new String(buf,0,num));        int num1 = fr.read(buf);//将读到的字符存储到数组中。        System.out.println(num1+":"+new String(buf,0,num1));        int num2 = fr.read(buf);//将读到的字符存储到数组中。        System.out.println(num2+":"+new String(buf,0,num2));*/        fr.close();    }}

练习:文本的复制

public class CopyTextTest {    private static final int BUFFER_SIZE = 1024;    public static void main(String[] args){        //首先声明一个输入流和输出流对象        FileReader fr = null;        FileWriter fw = null;        try {            fr = new FileReader("IO流-2.txt");            fw = new FileWriter("Copytext.txt");            //创建一个缓冲区对象,把读的数据全部存入。            char[] buf = new char[BUFFER_SIZE];            for (int len ; (len = fr.read(buf))!=-1;){                fw.write(buf,0,len);            }        } catch (IOException e) {            e.printStackTrace();        } finally {            if (fw!=null){                try {                    fw.close();                } catch (IOException e) {                    e.printStackTrace();                }            }            if (fr!=null){                try {                    fr.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }    }}
阅读全文
0 0