JAVASE基础模块三十二(数据输入输出流 内存操作流 操作字符数组 与 操作字符串 字节打印流 PrintStream 字符打印流 PrintWriter)

JAVASE基础模块三十二(数据输入输出流 内存操作流 操作字符数组 与 操作字符串 字节打印流 PrintStream 字符打印流 PrintWriter)

数据输入输出流

  • 数据输入输出流 特点是 能够读写基本数据类型

    1. DataOutputStream
    2. DataInputStream
    import java.io.*;
    
    public class Test {
        public static void main(String[] args) throws IOException {
            write();
            DataInputStream d = new DataInputStream(new FileInputStream("cc.txt"));
            int l=d.readInt();
            System.out.println(l);
            double b=d.readDouble();
            System.out.println(b);
            boolean c = d.readBoolean();
            System.out.println(c);
            char dd = d.readChar();
            System.out.println(dd);
            d.close();
        }
    
        private static void write() throws IOException {
            DataOutputStream dd = new DataOutputStream(new FileOutputStream("cc.txt"));
            dd.write(100);
            dd.writeDouble(1.35);
            dd.writeBoolean(true);
            dd.writeChar('d');
            dd.close();
        }
    }
    运行结果:
    1681913241
    -2.3534373704777817E-185
    
    Process finished with exit code 0
    

内存操作流

  • 内存操作流 此流不直接关联文件 只在内存中进行读写

  • ByteArrayOutputStream 它在内存中维护了一个字节byte数组 作为缓冲区

  • 随着数据的不断被写入 缓冲区会不断地扩充

  • 可以使用toByteArray() toString()获取缓冲区的数据

  • 此类实现了一个输出流,其中的数据被写入一个 byte 数组

  • 缓冲区会随着数据的不断写入而自动增长。可使用 toByteArray () 和 toString () 获取数据

  • 关闭 ByteArrayOutputStream 无效

    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    
    public class NeiCun {
        public static void main(String[] args) throws IOException {
            ByteArrayOutputStream bb = new ByteArrayOutputStream();
            bb.write("一树梨花压海棠".getBytes());
            //toByteArray(); 取出ByteArrayOutputStream维护的字节数组
            byte[] bytes = bb.toByteArray();
            String s = new String(bytes);
            System.out.println(s);
    
            String s8 = bb.toString();
            System.out.println(s8);
            bb.close();
            //ByteArrayInputStream
            //ByteArrayInputStream (byte[] byt)
            //创建一个ByteArrayInputStream 使用byt作为它自身的缓冲区
    
            ByteArrayInputStream bbb = new ByteArrayInputStream(bytes);
            byte[] bytes1 = new byte[1024];
            int len = bbb.read(bytes1);
            String s1 = new String(bytes1, 0, len);
            System.out.println(s1);
    }
    运行结果:
    一树梨花压海棠
    一树梨花压海棠
    一树梨花压海棠
    
    Process finished with exit code 0
    
  • 应用 合并文件歌曲

    import java.io.*;
    import java.util.ArrayList;
    public class HeBing {
        public static void main(String[] args) {
            FileInputStream f1 = null;
            FileInputStream f2 = null;
            ByteArrayOutputStream b2 = null;
            FileOutputStream f3 = null;
            ByteArrayInputStream b1 = null;
            try {
                f1 = new FileInputStream("mm.mp3");
                f2 = new FileInputStream("dd.mp3");
                ArrayList<FileInputStream> fr = new ArrayList<>();
                fr.add(f2);
                fr.add(f1);
                b2 = new ByteArrayOutputStream();
                int len=0;
                byte[] bx = new byte[1024 * 8];
                for (FileInputStream in : fr) {
                    while ((len=in.read(bx))!=-1){
                        b2.write(bx,0,len);
                    }
                    in.close();
                }
                byte[] b11 = b2.toByteArray();
                b1 = new ByteArrayInputStream(b11);
                f3 = new FileOutputStream("he.mp3");
                int len1=0;
                byte[] bx1 = new byte[1024 * 8];
                while ((len1=b1.read(bx1))!=-1){
                    f3.write(bx1,0,len1);
                    f3.flush();
                }
                System.out.println("合并完毕");
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (f3 != null) {
                        f3.close();
                    }    if (b1 != null) {
                        b1.close();
                    }    if (b1 != null) {
                        b2.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    

操作字符数组 与 操作字符串

  • 与内存操作流的区别在于这两个流针对的是字符串与字符 就算存入int类型 也会转换成字符

  • 操作字符数组 他维护了一个字符数组作为缓冲区

    1. CharArrayReader
    2. CharArrayReader
  • 操作字符串

    1. StringReader
    2. StringWriter
    import java.io.*;
    
    public class Neic {
        public static void main(String[] args) throws IOException {
            CharArrayWriter cc = new CharArrayWriter();
            cc.write("莱茵科技");
            cc.write(new char[]{'d','d'});
            char[] chars = cc.toCharArray();
            System.out.println(String.valueOf(chars));
            System.out.println(cc.toString());
            //StringWriter
            StringWriter stringWriter = new StringWriter();
            stringWriter.write("abc");
            stringWriter.write("abc");
            stringWriter.write("abc");
            stringWriter.write("abc");      			System.out.println(stringWriter.toString());
        }
    }
    
    

打印流

  • 打印流 只能写出数据 不能读取数据 单个的 不是成对的
  • 分类
    1. 字节打印流 PrintStream
    2. 字符打印流 PrintWriter

字节打印流 PrintStream

  • 特点

    1. PrintStream 为其他输出流添加了功能 使它们能够方便地打印各种数据值表示形式
    2. 它还提供其他两项功能 与其他输出流不同 PrintStream 永远不会抛出 IOException 异常情况仅设置可通过checkError 方法测试的内部标志
    3. 为了自动刷新,可以创建一个 PrintStream 这意味着可在写入 byte 数组之后自动调用 flush 方法
    4. 可调用其中一个 println 方法 或写入一个换行符或字节 (’\n’)
    5. PrintStream 打印的所有字符都使用平台的默认字符编码转换为字节 在需要写入字符而不是写入字节的情况下 应该使用 PrintWriter 类
  • 构造方法

    1. PrintStream(File file) 创建具有指定文件且不带自动行刷新的新打印流
    2. PrintStream(OutputStream out, boolean autoFlush) 创建新的打印流
  • out 标准”输出流。

  1. 此流已打开并准备接受输出数据 此流对应于显示器输出或者由主机环境或用户指定的另一个输出目标
  2. 对于简单独立的 Java 应用程序 编写一行输出数据的典型方式是:System.out.println(data)
import java.io.IOException;
import java.io.PrintStream;

public class Tst {
    public static void main(String[] args) throws IOException {
        PrintStream pp = new PrintStream("s.txt");
        pp.write("一树梨花压海棠".getBytes());
        pp.println("一树梨花压海棠");
        pp.println("一树梨花压海棠");
        pp.println("一树梨花压海棠");
        PrintStream out = System.out;
        out.write("hhhe".getBytes());
        out.write("一树梨花压海棠".getBytes());
        System.out.println("abc");
        System.out.write("ab".getBytes());

    }
}
运行结果:
hhhe一树梨花压海棠abc
ab
Process finished with exit code 0
TXT文件结果:
一树梨花压海棠一树梨花压海棠
一树梨花压海棠
一树梨花压海棠

字符打印流 PrintWriter

  • 构造方法

    1. PrintWriter(OutputStream out, boolean autoFlush) 通过现有的 OutputStream 创建新的 PrintWriter
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.PrintWriter;
    
    public class ZiFu {
        public static void main(String[] args) throws IOException {
        //需要刷新
            PrintWriter ps = new PrintWriter("hh.txt");
            ps.write("一树梨花压海棠");
            ps.write("一树梨花压海棠");
            ps.write("一树梨花压海棠");
            ps.write("一树梨花压海棠");
            ps.flush(); 
            //不需要刷新
            PrintWriter ps1 = new PrintWriter(new FileWriter("fg.txt"), true);
            ps1.println("一树梨花压海棠");
            ps1.println("一树梨花压海棠");
            ps1.println("一树梨花压海棠");
            ps1.println("一树梨花压海棠");
            ps.close();
            ps1.close();
        }
    }
    
    运行结果:
    Process finished with exit code 0
    TXT文件结果:
    一树梨花压海棠一树梨花压海棠一树梨花压海棠一树梨花压海棠TXT文件结果:
    一树梨花压海棠
    一树梨花压海棠
    一树梨花压海棠
    一树梨花压海棠
    
  • 应用 复制文件

    import java.io.*;
    
    public class FuZhiDaYin {
        public static void main(String[] args) throws IOException {
            PrintWriter p1 = new PrintWriter(new FileOutputStream("fzfz.mp3"), true);
            BufferedReader b1 = new BufferedReader(new FileReader("C:\\Users\\yllch\\Desktop\\mm.mp3"));
            while (true) {
                String s = b1.readLine();
                if (s != null) {
                    p1.println(s);
                } else {
                    break;
                }
            }
            p1.close();
            b1.close();
        }
    }
    
    
    运行结果:
    Process finished with exit code 0
    D:\IDEA\IdeaProjects\ZaQiZaBa\fzfz.mp3
    

应用

  • 复制多级文件夹

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    public class FuZhi {
        public static void main(String[] args) throws IOException {
            //1.封装源文件夹
            File srcFolder = new File("C:\\Users\\demo");
            //2.封装目标文件夹
            File targetFloder = new File("E:\\demo");
            if (!targetFloder.exists()) {
                targetFloder.mkdirs();
            }
            //3.复制文件夹
            copyFolder(srcFolder, targetFloder);
            System.out.println("复制完成");
    
        }
        private static void copyFolder(File srcFolder, File targetFloder) throws IOException {
            //1.遍历源文件夹
            File[] files = srcFolder.listFiles();
            for (File f : files) {
                //System.out.println(f);
                if(f.isFile()){
                    copyFiles(f, targetFloder);
                }else{
                    //递归
                    //System.out.println(f);
                    //封装子目标文件夹
                    File childFolder = new File(targetFloder, f.getName());
                    // System.out.println(file);
                    if (!childFolder.exists()) {
                        childFolder.mkdirs();
                    }
                    //递归
                    copyFolder(f,childFolder);
                }
            }
        }
        //复制文件
        private static void copyFiles(File f, File targetFloder) throws IOException {
            FileInputStream in = new FileInputStream(f);
            String fileName = f.getName();
            FileOutputStream out=null;
            if(fileName.endsWith(".jpg")){
                String substring = fileName.substring(0, fileName.lastIndexOf("."));
                fileName=substring+".png";
                out= new FileOutputStream(new File(targetFloder,fileName));
            }else{
                out = new FileOutputStream(new File(targetFloder, fileName));
            }
    
            byte[] bytes = new byte[1024 * 8];
            int len=0;
            while ((len=in.read(bytes))!=-1){
                out.write(bytes,0,len);
                out.flush();
            }
            in.close();
            out.close();
        }
    }
    
    
  • 删除多级文件夹

    import java.io.File;
    
    public class ShanChu {
        public static void main(String[] args) {
            //1.删除多级文件夹
            //1.封装源文件夹
            File srcFolder = new File("C:\\Users\\demo");
            deleteFolder(srcFolder);
        }
        private static void deleteFolder(File srcFolder) {
            File[] files = srcFolder.listFiles();
            for (File f : files) {
                if (f.isFile()) {
                    f.delete();
                }else{
                    deleteFolder(f);
                }
            }
            srcFolder.delete();
        }
    }
    

待续…

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值