JAVA之io流其他

1. 数据输入输出流的概述和使用

* 数据输入和输出流:
数据输入流: DataInputStream
数据输出流: DataOutputStream
特点: 可以写基本数据类型,可以读取基本数据类型

1.2 内存操作流的概述和使用

* 内存操作流的概述
a:操作字节数组
	ByteArrayOutputStream
	ByteArrayInputStream
	此流关闭无效,所以无需关闭
b:操作字符数组
	CharArrayWrite
	CharArrayReader
c:操作字符串
	StringWriter
	StringReader		
* 构造方法: public ByteArrayOutputStream()

需求:将两首歌合并为一首歌

public class test3 {
    public static void main(String[] args) throws IOException {
        //将两首歌合并为第一首歌
        FileInputStream in1 = new FileInputStream(new File("C:\\Users\\优小熊Xx\\Desktop\\c.mp3"));
        FileInputStream in2 = new FileInputStream(new File("C:\\Users\\优小熊Xx\\Desktop\\c3.mp3"));
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int len1=0;
        byte[] bytes1 = new byte[1024 * 1024];
        while((len1=in1.read(bytes1))!=-1){
            out.write(bytes1,0,len1);
        }
        int len2=0;
        byte[] bytes2 = new byte[1024 * 1024];
        while((len2=in2.read(bytes2))!=-1){
            out.write(bytes2,0,len2);
        }
        byte[] array = out.toByteArray();
        ByteArrayInputStream in = new ByteArrayInputStream(array);
        FileOutputStream out1 = new FileOutputStream(new File("C:\\Users\\优小熊Xx\\Desktop\\c2.mp3"));
        byte[] bytes = new byte[1024 * 1024];
        int len=0;
        while((len=in.read(bytes))!=-1){
            out1.write(bytes,0,len);
        }
    }
}

1.3 打印流的特点以及作为Writer的子类使用

 //打印流:只能写出数据,单个的
        //字节打印流 PrintStream
        //字符打印流 PrintWriter
        /*PrintStream p = new PrintStream("aa.txt");
        p.write("你好呀".getBytes());*/
        PrintWriter p = new PrintWriter(new FileOutputStream("aaa.txt"),true);
        p.write("我像是一个你可有可无的影子");
        p.write("\r\n");
        p.write("你好呀");
        p.println("你好啦");

1.4 Properties的概述和作为Map集合的使用

* Properties的概述

Properties 类表示了一个持久的属性集。
Properties 可保存在流中或从流中加载。
属性列表中每个键及其对应值都是一个字符串。

Properties父类是Hashtable
* 属于双列集合,这个集合中的键和值都是字符串 Properties不能指定泛型

1.5 Properties的特殊功能使用

setProperty(key,value)//给properties中添加,底层是map去重
stringPropertyNames();//得到的是一个键的集合
getProperty(key);//得到键对应的值

1.6 Properties的load()和store()功能

//Properties和io流相结合
//load(InputStream in);//将文本文件中的信息读取到Properties,字节流
//load(Reader reader);//将文本文件中的信息读取到Properties,字符流
//store(OutputStream out,String comments);//将信息从Properties读取到文本文件,字节流
//store(OutputStream out,String comments);//将信息从Properties读取到文本文件,字符流

需求:判断文本文件中是否有"王五",如果有的话,将其对应的键值改为"100"

public class test2 {
    public static void main(String[] args) throws IOException {
     
        Properties p = new Properties();
        p.setProperty("张三","100");
        p.setProperty("李四","200");
        p.setProperty("王五","500");
        p.store(new FileWriter(new File("t.txt")),null);

        Properties p1 = new Properties();
        p1.load(new FileReader(new File("t.txt")));
        Set<String> s = p1.stringPropertyNames();
        for (String s1 : s) {
            if(s1.equals("王五")){
                p1.setProperty("王五","100");
            }
        }
        p1.store(new FileWriter(new File("t.txt")),null);
    }
}

1.7 SequenceInputStream

* 表示其他输入流的逻辑串联。
它从输入流的有序集合开始,
并从第一个输入流开始读取,直到到达文件末尾,接着从第二个输入流读取,
依次类推,直到到达包含的最后一个输入流的文件末尾为止
* 构造方法
SequenceInputStream(InputStream s1, InputStream s2) 
通过记住这两个参数来初始化新创建的 SequenceInputStream(将按顺序读取这两个参数,先读取 s1,然后读取 s2),
以提供从此 SequenceInputStream 读取的字节。
* 构造方法
SequenceInputStream(Enumeration<? extends InputStream> e) 
 通过记住参数来初始化新创建的 SequenceInputStream,该参数必须是生成运行时类型为 InputStream 对象的 Enumeration 型参数。

示例:将一首歌拆分并合并

public class test5 {
    public static void main(String[] args) throws IOException {
        //拆分();
        //合并();
    }

    private static void 合并() throws IOException {
        File file = new File("C:\\Users\\优小熊Xx\\Desktop\\c");
        File[] files = file.listFiles();
        Vector<FileInputStream> v = new Vector<>();
        for (File file1 : files) {
            FileInputStream in = new FileInputStream(file1);
            v.add(in);
        }
        SequenceInputStream se = new SequenceInputStream(v.elements());
        int len=0;
        byte[] bytes = new byte[1024 * 1024];
        FileOutputStream out = new FileOutputStream(new File("C:\\Users\\优小熊Xx\\Desktop\\c1.mp3"));
        while((len=se.read(bytes))!=-1){
            out.write(bytes,0,len);
        }
    }

    private static void 拆分() throws IOException {
        File file = new File("C:\\Users\\优小熊Xx\\Desktop\\c.mp3");
        File newfile = new File("C:\\Users\\优小熊Xx\\Desktop\\c");
        if(!newfile.exists()){
            newfile.mkdirs();
        }
        FileInputStream in = new FileInputStream(file);
        int len=0;
        int i=1;
        byte[] bytes = new byte[1024 * 1024];
        while((len=in.read(bytes))!=-1){
            FileOutputStream out = new FileOutputStream(new File(newfile,(i++)+".mp3"));
            out.write(bytes,0,len);
        }
    }
}

猜你喜欢

转载自blog.csdn.net/Dhxy1030/article/details/108019551