Java中的字符流和其它流

138 阅读4分钟

一起养成写作习惯!这是我参与「掘金日新计划 · 4 月更文挑战」的第14天,点击查看活动详情

Java中的字符流和其它流

需要:

        (1)掌握字符输入输出流类的特点及使用。

        (2)掌握利用字符输入流和字符输出流进行文件内容拷贝的操作。

        (3)理解对象流(序列化)类的特点及使用。

使用字符流进行文件拷贝的方法:

需求:

        把当前项目目录下的a.txt内容复制到当前项目目录下的b.txt中

数据源:

         a.txt -- 读取数据 -- 字符转换流 -- InputStreamReader -- FileReader

目的地:

        b.txt -- 写出数据 -- 字符转换流 -- OutputStreamWriter -- FileWriter

    FileReader fr=new FileReader("a.txt");
    FileWriter fw=new FileWriter("b.txt");
    char[] chs=new char[1024];
    int len=0;
    while((len=fr.read(chs))!=-1){
        fw.write(chs,0,len);
    }

        最后要记得关闭流哦,使用fr.close()和fw.close()。

字符缓冲流的特殊方法:

需求:

        把当前项目目录下的a.txt内容复制到当前项目目录下的b.txt中

数据源:

 a.txt -- 读取数据 -- 字符转换流 -- InputStreamReader -- FileReader -- BufferedReader

目的地:

b.txt -- 写出数据 -- 字符转换流 -- OutputStreamWriter -- FileWriter -- BufferedWriter

字符缓冲流的特殊方法:

        BufferedWriter:

                public void newLine():根据系统来决定换行符

        BufferedReader:

                public String readLine():一次读取一行数据

        包含该行内容的字符串,不包含任何行终止符,如果已到达流末尾,则返回 null

    BufferedReader br=new BufferedReader(new FileReader("FileCopyDemo1.java"));
    BufferedWriter bw=new BufferedWriter(new FileWriter("copy.java"));
    String str=null;
    while((str=br.readLine())!=null){
        bw.write(str);
        bw.newLine();
        bw.flush();
    }

单级文件夹的复制:

思路分析:

        A:封装目录

        B:获取该目录下的所有文件的File数组

        C:遍历该File数组,得到每一个File对象

        D:把该File进行复制

    File srcFile = new File("e:\\demo");
    File destFile = new File("e:\\test");
    if (!destFile.exists()) {  //目标文件夹若不存在,则创之。
        destFile.mkdir();
    }
    File[] fileArray = srcFile.listFiles();
    for (File file : fileArray) {
        File newFile = new File(destFile, file.getName());
        copyFile(file, newFile);
    }

copyFile.java

    private static void copyFile(File file, File newFile) throws IOException {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream file));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newFile));
        byte[] bys=new byte[1024];
        int len=0;
        while((len=bis.read(bys))!=-1){
            bos.write(bys, 0, len);
        }
        bis.close();
        bos.close();
    }

RandomAccessFile类的特点及使用:

随机访问流:

        RandomAccessFile类不属于流,是Object类的子类。

        但它融合了InputStream和OutputStream的功能。

        支持对文件的随机访问读取和写入。

        public RandomAccessFile(String name,String mode):第一个参数是文件路径,第二个参数是操作文件的模式。

        模式有四种,我们最常用的一种叫"rw",这种方式表示我既可以写数据,也可以读取数据

代码思路:

        1.创建RandomAccessFile对象,并以只读模式打开time.txt文件

        2.读取还可以使用次数,第一次读取时times为5

        3.判断剩余次数

        4.每执行一次代表使用一次,次数就减少一次

        5.将记录指针重新指向文件开头

        6.将剩余次数再次写入文件

        7.关闭这个随机存取文件流并释放任何系统

    RandomAccessFile raf = new RandomAccessFile("time.txt", "rw");
    int times = Integer.parseInt(raf.readLine())-1;
    if(times > 0){
        System.out.println("您还可以试用"+ times+"次!");
        raf.seek(0);
        raf.write((times+"").getBytes());
    }else{
        System.out.println("试用次数已经用完!");
    }
    raf.close();

将E盘下所有文本文件的内容拷贝到一个新的文件中:

思路分析:

        A:封装目录

        B:获取该目录下所有的文本文件

        C:封装目的地

        D:遍历该File数组,得到每一个File对象

        E:将每个File对象封装为数据源

        F:依次将遍历的数据源拷贝到目的地