Java——杂七杂八流

一、杂七杂八流
(一)数据输入输出流的概述和使用

  • 数据输入流: DataInputStream
  • 数据输出流: DataOutputStream
  • 特点: 可以写基本数据类型,可以读取基本数据类型`
B:案例演示:	数据输入输出流的使用
	// 写基本数据类型
	dos.writeInt(45) ;
	dos.writeChar('中');
	dos.writeUTF("你好");
	// 读取数据
	int a = dis.readInt() ;
	System.out.println(a);
	char ch = dis.readChar() ;
	System.out.println(ch);
	String str = dis.readUTF() ;
	System.out.println(str);

(二)、内存操作流的概述和使用
A:内存操作流的概述
a:操作字节数组
ByteArrayOutputStream
ByteArrayInputStream
此流关闭无效,所以无需关闭
b:操作字符数组
CharArrayWrite
CharArrayReader
c:操作字符串
StringWriter
StringReader
B:案例演示: 内存操作流的使用
// 构造方法: public ByteArrayOutputStream()
(三)、打印流的概述和特点
A:打印流的概述
通过API查看
字节流打印流
字符打印流
B:打印流的特点

a: 打印流只能操作目的地,不能操作数据源(不能进行读取数据)
	- b: 可以操作任意数据类型的数据 调用print() 方法可以写任意数据类型
- c: 如果我们启用自动刷新,那么在调用println、printf 或 format 方法中的一个方法的时候,会完成自动刷新
  		通过以下构造创建对象 能够启动自动刷新  然后调用println、printf 或 format 方法中的一个方法的时候,会完成自动刷新
  	 * public PrintWriter(OutputStream out,  boolean autoFlush)	 启动 自动刷新
  	 * public PrintWriter(Writer out,  boolean autoFlush)		启动自动刷新
- d: 这个流可以直接对文件进行操作(可以直接操作文件的流: 就是构造方法的参数可以传递文件或者文件路径)
  C:案例演示:	PrintWriter作为Writer的子类使用

(四)PrintWriter实现自动刷新和换行

A:标准输入输出流概述
	在System这个类中存在两个静态的成员变量:
- public static final InputStream in: 标准输入流, 对应的设备是键盘
- public static final PrintStream out: 标准输出流 , 对应的设备就是显示器
  	System.in的类型是InputStream.
  	System.out的类型是PrintStream是OutputStream的孙子类FilterOutputStream 的子类.
  B:案例演示:	输出语句的本质

(五)第二种方式实现键盘录入

A:Scanner
B:BufferedReader的readLine方法。
	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
案例:
//Scanner sc = new Scanner(System.in);
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        while (true){
            System.out.println("请输入字符串");
            String s = reader.readLine();
            System.out.println(s);
            //自定义一个结束标记
            if("886".equals(s)){
                break;
            }
        }

(六)随机访问流概述和写出数据
(1)随机访问流概述
RandomAccessFile概述 最大特点 能读能写
RandomAccessFile类不属于流,是Object类的子类。但它融合了InputStream和OutputStream的功能。
支持对随机访问文件的读取和写入。
RandomAccessFile的父类是Object , 这个流对象可以用来读取数据也可以用来写数据.可以操作任意数据类型的数据.
案例:复制mp3

 RandomAccessFile raf = new RandomAccessFile("歌曲大联唱.mp3", "r");
        RandomAccessFile raf2= new RandomAccessFile("歌曲大联唱2.mp3", "rw");
        int len=0;
        byte[] bytes = new byte[1];
        int sum=0;
        try {
            while ((len = raf.read(bytes)) != -1) {
                raf.seek(200);
                raf2.write(bytes, 0, len);
            }
        }catch (Exception e){
            long pointer = raf2.getFilePointer();
            System.out.println(pointer);
        }

(2)读取数据和操作文件指针

//随机访问流概述
        //RandomAccessFile概述 最大特点 能读能写
        //RandomAccessFile类不属于流,是Object类的子类。但它融合了InputStream和OutputStream的功能。
        //支持对随机访问文件的读取和写入。
        //
        //RandomAccessFile的父类是Object, 这个流对象可以用来读取数据也可以用来写数据.可以操作任意数据类型的数据.
        // writeData();
        RandomAccessFile raf = new RandomAccessFile("a.txt", "rw");
        boolean b = raf.readBoolean();
        int i = raf.readInt();
        String s = raf.readUTF();
        System.out.println(b);
        System.out.println(i);
        System.out.println(s);
        System.out.println(raf.getFilePointer()); //获取文件指针目前处于的位置
        //可以移动文件指针
        System.out.println("---------------------");
        raf.seek(0);//可以设置文件指针的位置
        b = raf.readBoolean();
        i = raf.readInt();
        s = raf.readUTF();
        System.out.println(b);
        System.out.println(i);
        System.out.println(s);
        return;
    }
    private static void writeData() throws IOException {
        RandomAccessFile raf = new RandomAccessFile("a.txt", "rw");
        raf.writeBoolean(false);
        raf.writeInt(1000);
        raf.writeUTF("你好"); //会多写两个字节
        raf.close();

(七)序列化流和反序列化流的概述和使用
A:序列化流的概述
所谓的序列化:就是把对象通过流的方式存储到文件中.注意:此对象 要重写Serializable 接口才能被序列化
反序列化:就是把文件中存储的对象以流的方式还原成对象
序列化流: ObjectOutputStream
反序列化流: ObjectInputStream
案例:

Student student = new Student("战三", 30);
        ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream("student.txt"));
        outputStream.writeObject(student);

(八)Properties的概述和作为Map集合的使用
A:Properties(概述)
Properties 类表示了一个持久的属性集。
Properties 可保存在流中或从流中加载。
属性列表中每个键及其对应值都是一个字符串。
Properties父类是Hashtable
- 属于双列集合,这个集合中的键和值都是字符串 Properties不能指定泛型
B:案例演示: Properties作为Map集合的使用

//属性集合
        //这个集合的键值都是字符串类型
        Properties properties = new Properties();
        //properties.put("武大","金莲");
        //Object obj = properties.get("武大");
        //String value= (String) obj;
        //System.out.println(value);
        //用它自带的方法,来存取数据
        properties.setProperty("武大", "金莲");
        //String v = properties.getProperty("武大");
        //如果通过这个键,没有找到对应的值,会返回默认值
        String v = properties.getProperty("武大2", "扈三娘");
        System.out.println(v);

(2)特殊功能使用
A:Properties的特殊功能
public Object setProperty(String key,String value)
public String getProperty(String key)
public Set stringPropertyNames()
B:案例演示: Properties的特殊功能
(3)load()和store()功能
A:Properties的load()和store()功能
Properties和IO流进行配合使用:
public void load(Reader reader): 读取键值对数据把数据存储到Properties中

  • public void store(Writer writer, String comments)把Properties集合中的键值对数据写入到文件中, comments注释

案例演示:需求:我有一个文本文件,我知道数据是键值对形式的,但是不知道内容是什么。请写一个程序判断是否有“lisi”这样的键存在,如果有就改变其值为”100”

  • a: 把文本文件中的数据加载到Properties集合中
  • b: 判断这个集合中是否有"lisi"这个键
  • 如果有直接修改其值为100
  • c: 把集合中的数据再次存储到文本文件中
 Properties properties = new Properties();
        properties.load(new FileReader("demo.txt"));
        if (properties.containsKey("lisi")) {
            properties.put("lisi", "100");
        }
        properties.store(new FileWriter("demo.txt"), null);

(九)SequenceInputStream拼接
案例需求:1.将a.txt和b.txt两个文本文件的内容合并到c.txt

FileInputStream in1 = new FileInputStream("a.txt");
        FileInputStream in2 = new FileInputStream("b.txt");
        FileOutputStream out = new FileOutputStream("c.txt");

        //SequenceInputStream 表示其他输入流的逻辑串联。它从输入流的有序集合开始,并从第一个输入流开始读取,直到到达文件末尾,接着从第二个输入流读取,依次类推,
        //直到到达包含的最后一个输入流的文件末尾为止。
        //SequenceInputStream(InputStream s1, InputStream s2)
        //通过记住这两个参数来初始化新创建的 SequenceInputStream(将按顺序读取这两个参数,先读取 s1,然后读取 s2),以提供从此 SequenceInputStream 读取的字节。
        SequenceInputStream sequenceInputStream = new SequenceInputStream(in1, in2);
        int len=0;
        byte[] bytes = new byte[1024];
        while ((len=sequenceInputStream.read(bytes))!=-1){
            out.write(bytes,0,len);
            out.flush();
        }
        out.close();
        sequenceInputStream.close();

2.将一个music.mp3文件,拆分成多个小文件,再将多个小文件,合并成一个mp3文件

public class MyTest {
    public static void main(String[] args) throws IOException {
        //chaiFen();
        //heBing();
    }
    //把多个文件合并成一个文件
    private static void heBing() throws IOException {
        File file = new File("E:\\mytest");
        File[] files = file.listFiles();
        Vector<FileInputStream> vector = new Vector<>();
        for (File f : files) {
            if (f.isFile() && f.getName().endsWith(".mp3")) {
                FileInputStream in = new FileInputStream(new File(file, f.getName()));
                vector.add(in);
            }
        }
        SequenceInputStream sequenceInputStream = new SequenceInputStream(vector.elements());
        FileOutputStream outputStream = new FileOutputStream(new File(file, "领悟.mp3"));
        byte[] bytes = new byte[1024 * 8];
        int len = 0;
        while ((len = sequenceInputStream.read(bytes)) != -1) {
            outputStream.write(bytes, 0, len);
            outputStream.flush();
        }
        for (File f : files) {
            if (f.isFile() && f.getName().endsWith(".mp3") && f.length() <= 1024 * 1024) {
                f.delete();
            }
        }
        sequenceInputStream.close();
        outputStream.close();
    }
    //把一个文件,拆成多个文件
    private static void chaiFen() throws IOException {
        FileInputStream in = new FileInputStream("领悟.mp3");
        File file = new File("E:\\mytest");
        if (!file.exists()) {
            file.mkdirs();
        }
        byte[] bytes = new byte[1024 * 1024];
        int len = 0;
        int i = 1;
        while ((len = in.read(bytes)) != -1) {
            FileOutputStream outputStream = new FileOutputStream(new File(file, (i++) + "领悟.mp3"));
            outputStream.write(bytes, 0, len);
            outputStream.close();
        }
        in.close();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值