• 2※、处理流(包装流派)、缓冲流【字节缓冲流、字符缓冲流】-->【字节缓冲流、字符缓冲流】 、字符集、转换流以及打印流


    总结:

    低级流:节点流(基础的字节流和字符流)  InputStream\OutputStream   Reader\Writer
    高级流:封装流(对低级流进行包装、提供简易、高级的方法)、节点流
                --缓冲流 【缓冲数组、性能较好】
                                  --缓冲流字节流[和节点流操作一致]  BufferedInputStream\BufferedOutputStream  【操作方法是一样的】
                                  --缓冲流字符流  BufferedReader\BufferedWriter 【单个字符、多个字符的读取】 (不同、提供读取一行的操作)
                                                    --[输入-读取一行] write();
                                                    --[输出-换行] newLine();
                   -- 字节流BufferedInputStreamBufferedOutputStream
                   -- 字符流BufferedReader\BufferedWriterd
                   --BufferedReader(不同、提供读取一行的操作)
                   --BufferedWriter
    
                --转换流  【指定编码==字节流+编码(字符流)--输出流 InputStreamReader
                                  --输入流 OutputStreamWriter
                --打印流   【打印操作、打印方法(输出流)--字节输出流 PrintStream
                                  --字符输出流 PrintWriter
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    在已有的流的基础上进行包装,实现高级操作【可以设定字符集编码】
    注意:要是出现编码格式不一样,记得先复制后调整编码格式就行,然后再粘贴回来,即可完成不乱码和???的编码格式不一样情况的处理。

    ※缓冲流 【字节缓冲流、字符缓冲流】

    –具备一个内置的缓冲区,默认的大小为8192,所以比普通字节流的操作速率要高
    –字符缓冲流【字符输入流有readLine方法、字符输出流有newLine方法】
    –注意:在缓冲流中使用flush()方法将缓冲区的数据同步到文件中
    –API:
    –readLine方法
    –newLine方法

    缓冲流【字节流】文件复制以及BufferedOutputStream的源码查看

    /**
     * @author Lantzrung
     * @date 2022年8月1日
     * @Description
     */
    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    
    public class BufferDemo {
        // 使用场景:一般都是使用字节的缓冲流
        public static void main(String[] args) throws IOException {
    
    	// 注意: InputStream和BufferedOutputStream的区别是它存放了一个数组进去
    	// byte[] buff = new byte[1024];里面后还会存放进去BufferedInputStream
    	// 的 protected volatile byte buf[];缓冲数组中进行缓冲操作;
    	
    	// 查看源码BufferedOutputStream缓冲数组的源码这里可以看出它缓冲数组的长度是为8192
    	// public BufferedOutputStream(OutputStream out) {
    	//	 this(out, 8192);
    	// }
    
    	// 这里也可以看出BufferedOutputStream,int size返回的是buf = new
    	// byte[size]的一个数组长度为8192的默认缓冲数组
    	// public BufferedOutputStream(OutputStream out, int size) {
    	// 	super(out);
    	// if (size <= 0) {
    	//	 throw new IllegalArgumentException("Buffer size <= 0");
    	// }
    	//	 buf = new byte[size];
    	// }
    
    	// 字符的缓冲流 输入-输出 作用提高读写效率 默认的缓冲数组长度为8192 = 1024*8 意思就是它固定当前IO流的操作为8KB了
    	// 也就是说不管你在定义了程序定义的存储数据是多长都会默认当次是为8192的缓冲数组
    	// 应用: 文件复制
    	// --输入
    	InputStream in = new FileInputStream("D:\\Test\\List.wmv");
    	// 封装了低级流-节点流
    	BufferedInputStream input = new BufferedInputStream(in);
    	// -- 输出
    	OutputStream out = new FileOutputStream("D:\\Test\\test\\List.wmv");
    	BufferedOutputStream output = new BufferedOutputStream(out);
    	// --读取--写出
    	byte[] buff = new byte[1024];
    	int len = 0;
    	// 读取并判断
    	while ((len = input.read(buff)) != -1) {
    	    // 读取多少写出多少
    	    output.write(buff, 0, len);
    	    // 同步刷新
    	    output.flush();
    	}
    	// 关闭资源
    	input.close();
    	in.close();
    	output.close();
    	out.close();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64

    缓冲流【字符流】 【读取操作、写出操作】

    
    /**
     * @author Lantzrung
     * @date 2022年8月1日
     * @Description
     */
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.Reader;
    import java.io.Writer;
    
    public class BufferedCharDemo01 {
        public static void main(String[] args) throws IOException {
    	// 缓冲的字符流
    	// --读取操作
    	 read();
    	// --写出操作
    	write();
        }
    
        // 写出操作
        public static void write() throws IOException {
    	// 缓冲的字符流
    	// --读取操作
    //	Writer out = new FileWriter("D:\\files\\los.txt");// 节点流
    	Writer out = new FileWriter("D:\\files\\los.txt",true);// 可以增加追加模式
    	BufferedWriter writer = new BufferedWriter(out);// 封装了低级的字符输入法
    	// 写出数据
    	// append(c) write(str)
    	writer.write("demo test!!");
    	writer.newLine();// 换行
    	writer.flush();
    	// 关闭流
    	writer.close();// flushBuffer();
    	out.close();
        }
    
        public static void read() throws IOException {
    	// 缓冲的字符流
    	// --读取操作
    	Reader in = new FileReader("D:\\files\\los.txt");// 节点流
    	BufferedReader reader = new BufferedReader(in); // 封装了低级的字符输入流
    
    	// readLine 是读取一行,如果流中的数据读取完了则返回null
    	String line = "";
    	while ((line = reader.readLine()) != null) {
    	    System.out.println(line);
    	}
    	// 关闭流
    	reader.close();
    	in.close();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57

    案例:
    一、普通的字节流和缓冲字节流的复制文件速率
    二、字符缓冲流读取数据和写出数据的操作

    练习:文本排序:请将文本信息恢复顺序。【使用了字符流的读取和写出操作】

    文档1排序好–》文本档2

    3.侍中、侍郎郭攸之、费祎、董允等,此皆良实,志虑忠纯,是以先帝简拔以遗陛下。愚以为宫中之事,事无大小,悉以咨之,然后施行,必得裨补阙漏,有所广益。
    8.愿陛下托臣以讨贼兴复之效,不效,则治臣之罪,以告先帝之灵。若无兴德之言,则责攸之、祎、允等之慢,以彰其咎;陛下亦宜自谋,以咨诹善道,察纳雅言,深追先帝遗诏,臣不胜受恩感激。
    4.将军向宠,性行淑均,晓畅军事,试用之于昔日,先帝称之曰能,是以众议举宠为督。愚以为营中之事,悉以咨之,必能使行阵和睦,优劣得所。
    2.宫中府中,俱为一体,陟罚臧否,不宜异同。若有作奸犯科及为忠善者,宜付有司论其刑赏,以昭陛下平明之理,不宜偏私,使内外异法也。
    1.先帝创业未半而中道崩殂,今天下三分,益州疲弊,此诚危急存亡之秋也。然侍卫之臣不懈于内,忠志之士忘身于外者,盖追先帝之殊遇,欲报之于陛下也。诚宜开张圣听,以光先帝遗德,恢弘志士之气,不宜妄自菲薄,引喻失义,以塞忠谏之路也。
    9.今当远离,临表涕零,不知所言。
    6.臣本布衣,躬耕于南阳,苟全性命于乱世,不求闻达于诸侯。先帝不以臣卑鄙,猥自枉屈,三顾臣于草庐之中,咨臣以当世之事,由是感激,遂许先帝以驱驰。后值倾覆,受任于败军之际,奉命于危难之间,尔来二十有一年矣。
    7.先帝知臣谨慎,故临崩寄臣以大事也。受命以来,夙夜忧叹,恐付托不效,以伤先帝之明,故五月渡泸,深入不毛。今南方已定,兵甲已足,当奖率三军,北定中原,庶竭驽钝,攘除奸凶,兴复汉室,还于旧都。此臣所以报先帝而忠陛下之职分也。至于斟酌损益,进尽忠言,则攸之、祎、允之任也。
    5.亲贤臣,远小人,此先汉所以兴隆也;亲小人,远贤臣,此后汉所以倾颓也。先帝在时,每与臣论此事,未尝不叹息痛恨于桓、灵也。侍中、尚书、长史、参军,此悉贞良死节之臣,愿陛下亲之信之,则汉室之隆,可计日而待也。
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    注意:要是实现的txt文本出现乱码记得改变编码格式为UTF-8

    输出的结果只会出现在控制台中不会进行转移到输出文件目录下面

    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.Reader;
    import java.io.Writer;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.Iterator;
    
    /**
     * @author Lantzrung
     * @date 2022年8月1日
     * @Description
     */
    
    public class BufferedChar {
        public static void main(String[] args) throws IOException {
    	// 文本拷贝并将其中的内容排序
    
    	Reader in = new FileReader("D:\\Test\\test.txt");// 节点流
    	BufferedReader reader = new BufferedReader(in);// 封装了低级的字符输入流
    
    	// --缓冲流输出
    	// -- 写出操作
    	Writer out = new FileWriter("D:\\Test\\test\\test.txt"); // 节点流
    	BufferedWriter writer = new BufferedWriter(out);// 封装了低级的字符输出流
    
    	// --定义集合
    	ArrayList<String> list = new ArrayList<>();
    	String line = "";
    	// 读取并保存在集合中
    	while ((line = reader.readLine()) != null) {
    	    list.add(line);
    	}
    
    	// 遍历输出
    	for (String string : list) {
    	    System.out.println(string);
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    使用比较器进行升降序比较

    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.Reader;
    import java.io.Writer;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.Iterator;
    
    /**
     * @author Lantzrung
     * @date 2022年8月1日
     * @Description
     */
    
    public class BufferedChar {
        public static void main(String[] args) throws IOException {
    	// 文本拷贝并将其中的内容排序
    
    	Reader in = new FileReader("D:\\Test\\test.txt");// 节点流
    	BufferedReader reader = new BufferedReader(in);// 封装了低级的字符输入流
    
    	// --缓冲流输出
    	// -- 写出操作
    	Writer out = new FileWriter("D:\\Test\\test\\test.txt"); // 节点流
    	BufferedWriter writer = new BufferedWriter(out);// 封装了低级的字符输出流
    
    	// --定义集合
    	ArrayList<String> list = new ArrayList<>();
    	String line = "";
    	// 读取并保存在集合中
    	while ((line = reader.readLine()) != null) {
    	    list.add(line);
    	}
    	// 对集合进行排序[提供比较器定义比较规则]
    	// 降序操作
    //	Collections.sort(list, new Comparator() { // 注意:Collection不加s是接口加s是实现类
    //	    @Override
    //	    public int compare(String o1, String o2) {
    //		return o2.charAt(0) - o1.charAt(0);
    //	    }
    //	});
    //	 升序操作
    	Collections.sort(list, new Comparator<String>() { // 注意:Collection不加s是接口加s是实现类
    	    @Override
    	    public int compare(String o1, String o2) {
    		return o1.charAt(0) - o2.charAt(0);
    	    }
    	});
    
    	// 遍历输出
    	for (String string : list) {
    	    writer.write(string);
    	    writer.newLine();
    	    writer.flush();
    	}
    	// 关闭资源
    	reader.close();
    	in.close();
    	writer.close(); // flushBuffer
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66

    字符集

    字符集:也叫编码表。是一个系统支持的所有字符的集合,包括各国家文字、标点符号、图形符号、数字等。
    编码:将字符–》字节
    解码:将字节–》字符
    在这里插入图片描述

    ASCII字符集 :
    ASCII(American Standard Code for Information Interchange,美国信息交换标准代码)是基于拉丁字母的一套电脑编码系统,用于显示现代英语,主要包括控制字符(回车键、退格、换行键等)和可显示字符(英文大小写字符、阿拉伯数字和西文符号)。
    基本的ASCII字符集,使用7位(bits)表示一个字符,共128字符。ASCII的扩展字符集使用8位(bits)表示一个字符,共256字符,方便支持欧洲常用字符。
    ISO-8859-1字符集:
    拉丁码表,别名Latin-1,用于显示欧洲使用的语言,包括荷兰、丹麦、德语、意大利语、西班牙语等。
    ISO-8859-1使用单字节编码,兼容ASCII编码。
    GBxxx字符集:
    GB就是国标的意思,是为了显示中文而设计的一套字符集。
    GB2312:简体中文码表。一个小于127的字符的意义与原来相同。但两个大于127的字符连在一起时,就表示一个汉字,这样大约可以组合了包含7000多个简体汉字,此外数学符号、罗马希腊的字母、日文的假名们都编进去了,连在ASCII里本来就有的数字、标点、字母都统统重新编了两个字节长的编码,这就是常说的"全角"字符,而原来在127号以下的那些就叫"半角"字符了。
    GBK:最常用的中文码表。是在GB2312标准基础上的扩展规范,使用了双字节编码方案,共收录了21003个汉字,完全兼容GB2312标准,同时支持繁体汉字以及日韩汉字等。
    GB18030:最新的中文码表。收录汉字70244个,采用多字节编码,每个字可以由1个、2个或4个字节组成。支持中国国内少数民族的文字,同时支持繁体汉字以及日韩汉字等。
    Unicode字符集 :
    Unicode编码系统为表达任意语言的任意字符而设计,是业界的一种标准,也称为统一码、标准万国码。
    它最多使用4个字节的数字来表达每个字母、符号,或者文字。有三种编码方案,UTF-8、UTF-16和UTF-32。最为常用的UTF-8编码。
    UTF-8编码,可以用来表示Unicode标准中任何字符,它是电子邮件、网页及其他存储或传送文字的应用中,优先采用的编码。互联网工程工作小组(IETF)要求所有互联网协议都必须支持UTF-8编码。所以,我们开发Web应用,也要使用UTF-8编码。它使用一至四个字节为每个字符编码,编码规则:
    128个US-ASCII字符,只需一个字节编码。
    拉丁文等字符,需要二个字节编码。
    大部分常用字(含中文),使用三个字节编码。
    其他极少使用的Unicode辅助字符,使用四字节编码。

    ※转换流、【InputStreamReader】、【OutputStreamWriter】

    –在构建转换流时,可以封装节点流,并指定编码集

    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.io.OutputStreamWriter;
    
    /**
     * @author Lantzrung
     * @date 2022年8月1日
     * @Description
     */
    
    public class TransforDemo {
        // 使用场景。当需要指定编码进行读写操作时,则使用转换流
        // 转换流的使用【实现编码之间的转换--字节+编码】 封装的是字节流
        public static void main(String[] args) throws IOException {
    	//
    	write();
    	//
    	read();
    
        }
    
        public static void write() throws IOException {
    	// 读取数据--输出流
    	OutputStream out = new FileOutputStream("D:\\day011\\text.txt");
    	OutputStreamWriter writer = new OutputStreamWriter(out, "UTF-8");
    
    	// 写出
    	writer.write("挺好的");
    	writer.flush();
    	// 关闭流
    	writer.close();
    	out.close();
    
        }
    
        public static void read() throws IOException {
    	// 读取数据--输入流
    	InputStream in = new FileInputStream("D:\\day011\\text.txt\\");
    	InputStreamReader reader = new InputStreamReader(in, "UTF-8");
    	
    	// 缓冲数组
    	char[] buff = new char[1024];
    	int len = 0;
    	// 循环读取
    	while ((len = reader.read(buff)) != -1) {
    	    System.out.println(new String(buff, 0, len));
    	}
    	// 关闭流
    	reader.close();
    	in.close();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57

    案例:

    1、键盘输入数据,在控制台中输出,当键盘输入exit的时候退出

    2、将data.txt的UTF-8文本内容复制到data1.txt中以GBK的形式

    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.io.OutputStreamWriter;
    
    /**
     * @author Lantzrung
     * @date 2022年8月1日
     * @Description
     */
    
    public class TransforTest2 {
        // 使用场景。当需要指定编码进行读写操作时,则使用转换流
        // 将data.txt的UTF-8文本内容复制到data1.txt中以GBK的形式
        public static void main(String[] args) throws IOException {
    
    	// 注意:要是输出格式是错误的会显示乱码和????????的操作 所以要进行源文件的编码格式
    //	InputStream in = new FileInputStream("D:\\Files\\0729\\data.java");
    //	InputStreamReader reader = new InputStreamReader(in, "UTF-8");
    
    	// 这里使用了匿名内部类进行操作
    	InputStreamReader reader = new InputStreamReader(new FileInputStream("D:\\Files\\0729\\data.txt"), "UTF-8");
    //	OutputStream out = new FileOutputStream("D:\\Files\\0729\\data.txt");
    //	OutputStreamWriter writer = new OutputStreamWriter(out, "GBK");
    	// 这里使用了匿名内部类进行操作
    
    	OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream("D:\\Files\\0729\\data01.txt"), "GBK");
    
    	char[] buff = new char[1024];
    	int len = 0;
    	while ((len = reader.read(buff)) != -1) {
    	String str = new String(buff, 0, len); // buff是提供数组,0是开始,len是最后的过程的结束
    	    writer.write(str);
    	    writer.flush();
    	}
    	// 关闭流
    	reader.close();
    	writer.close();
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    ※打印流【字节流】和【字符流】 和 【out】源码查看

    ※–打印流、PrintStream、PrintWriter
    –输出语句中的out对象就是PrintStream
    –可以使用构造器给打印流设置字符集编码
    –PrintStream 设置编码的构造器
    PrintStream ps = new PrintStream(file, csn);
    PrintStream ps = new PrintStream(fileName, csn);
    PrintStream ps = new PrintStream(out, autoFlush, encoding);
    –PrintWriter设置编码的构造器
    PrintWriter pw = new PrintWriter(file, csn);
    PrintWriter pw = new PrintWriter(fileName, csn);
    –API:
    print()方法:各种类型数据的打印方法,没有换行
    printf()方法:安照字符串格式进行打印输出的方法
    println()方法:各种类型数据的打印方法,有换行的

    使用打印流给文本输出数据【数值、字符、对象】

    /**
     * @author Lantzrung
     * @date 2022年8月1日
     * @Description
     */
    package PrintDemo;
    
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.io.OutputStreamWriter;
    import java.io.PrintStream;
    import java.io.PrintWriter;
    
    public class PrintDemo {
        // 转换流【输出、输入】和打印流【输出】
        // 打印流主要是提供:print、printf、println
        // 使用场景:输出【涉及编码使用PrintWriter】【一般也是使用PrintWriter】
        public static void main(String[] args) throws IOException {
    	printStreamDemo();// PrintStream【字节打印流】
    	printWriterDemo();// OutputStreamWriter【字符打印流】【提供字节流或者字符流】
        }
    
        // append(c); write(b);
        // 这里查看out源码字节码进行操作
        // System.out.println(); // out本身就是一个PrintStream
        // * @see java.io.PrintStream#println()
        // * @see java.io.PrintStream#println(boolean)
        // * @see java.io.PrintStream#println(char)
        // * @see java.io.PrintStream#println(char[])
        // * @see java.io.PrintStream#println(double)
        // * @see java.io.PrintStream#println(float)
        // * @see java.io.PrintStream#println(int)
        // * @see java.io.PrintStream#println(long)
        // * @see java.io.PrintStream#println(java.lang.Object)
        // * @see java.io.PrintStream#println(java.lang.String)
      
        
        // OutputStreamWriter【字符打印流】
        public static void printWriterDemo() throws IOException {
    	// 输出流
    	// OutputStream out = new FileOutputStream("D:\\Files\\0729\\data01.txt", true);
    	// 1、通过封装流来实现指定编码
    //	OutputStream out = new FileOutputStream("D:\\Files\\0729\\data01.txt", true);
    //	OutputStreamWriter writer = new OutputStreamWriter(out, "UTF-8");
    	
    	// 1、使用了匿名内部类进行操作
    	OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream("D:\\Files\\0729\\data01.txt", true), "UTF-8");
    	// 2、转换流封装字节流,然后打印流封装转换流
    	PrintWriter pw = new PrintWriter(writer);// new PrintWriter(out);
    	//
    	pw.println("姓名:张三");
    	// pw.printf("姓名:%s,年龄:%d", "zhang", 18);
    	// pw.println("hello1");
    	// 刷新
    	pw.flush();
    	// 关闭资源
    	pw.close();
        }
    
        // PrintStream【字节打印流】
        public static void printStreamDemo() throws IOException {
    	// 输出流
    //	OutputStream out = new FileOutputStream("D:\\Files\\0729\\data01.txt", true);
    //	// 构建流 //自动刷新 true PrintStream【字节打印流】 无法实现格式转换
    //	PrintStream ps = new PrintStream(out, true);
    	OutputStream out = new FileOutputStream("D:\\Files\\0729\\data01.txt", true);
    	OutputStreamWriter writer = new OutputStreamWriter(out, "UTF-8");
    	PrintWriter ps = new PrintWriter(writer);
    
    	// append(c); write(b);
    	// System.out.println(); //out本身就是一个PrintStream
    	ps.print("hello1");
    	ps.printf("姓名:%s,年龄:%d", "zhang", 18);
    	ps.println("hello1");
    	// 刷新
    	ps.flush();
    	// 关闭资源
    	ps.close();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82

    练习1

    【要求:使用打印流、并指定编码为UTF-8】
    通过键盘输入模拟用户交流,然后把交流的信息记录在log.txt中。
    –格式:【用户1】【时间yyyy-MM-dd HH:mm:ss】【内容】
    –当其中用户一个输入exit时,聊天结束。

    /**
     * @author Lantzrung
     * @date 2022年8月1日
     * @Description
     */
    package com.ghomework0801;
    
    import java.io.FileOutputStream;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.io.OutputStreamWriter;
    import java.io.PrintWriter;
    import java.io.Writer;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.Scanner;
    
    //【要求:使用打印流、并指定编码为UTF-8】
    //通过键盘输入模拟用户交流,然后把交流的信息记录在log.txt中。
    //--格式:【用户1】【时间yyyy-MM-dd HH:mm:ss】【内容】
    //--当其中用户一个输入exit时,聊天结束。
    public class HomeWork01 {
        public static void main(String[] args) throws IOException {
    	// 通过键盘输入模拟用户交流,然后把交流的信息记录在log.txt中。
    	// 1、创建Scanner
    	Scanner sc = new Scanner(System.in);
    	// 构建输出流 字符流 使用追加模式
    	OutputStream writer1 = new FileOutputStream("D:\\Files\\0729\\data01.txt", true);// 追加模式就是不会覆盖
    	OutputStreamWriter writer = new OutputStreamWriter(writer1, "UTF-8");
    	PrintWriter pw = new PrintWriter(writer);//
    	// 定义一个变量
    	boolean oneTurn = true;// 是否第一个用户的回合
    	// 2、实现用户的交流
    	while (true) {
    	    int man = oneTurn ? 1 : 2;// 那个用户
    	    System.out.println("用户" + man + "请输入发送的信息:");
    	    String msg = sc.next();
    	    // --格式:【用户1】【时间yyyy-MM-dd HH:mm:ss】【内容】
    	    String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
    	    // System.out.printf("【用户%d】【时间:%s】【内容:%s】", man);
    	    String line = "【用户" + man + "】【时间:" + time + "】【内容:" + msg + "】\n";// 格式语句
    	    // 输出日志
    	    writer.write(line);
    	    // 刷新
    	    writer.flush();
    	    // 切换用户[取反]
    	    oneTurn = !oneTurn;
    	    // 退出
    	    if (msg.equals("exit")) {
    		break;
    	    }
    	}
    	// 3、关闭
    	sc.close();
    	writer.close();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59

    练习2:

    实现班级学生注册操作、定义方法实现添加一个学生数据,并保存在students.txt中,例如:

    Student [code=20220101, name=zhangsan, age=16,address=gz]
    Student [code=20220102, name=li, age=15,address=gz]
    
    • 1
    • 2

    –提供方法实现根据学生编号返回学生详细数据的方法,例如:

    20220102
    返回:
    Student [code=20220102, name=li, age=15,address=gz]
    null
    
    • 1
    • 2
    • 3
    • 4
    /**
     * @author Lantzrung
     * @date 2022年8月1日
     * @Description
     */
    package com.g0801homekork;
    
    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.io.OutputStreamWriter;
    import java.io.PrintWriter;
    import java.io.UnsupportedEncodingException;
    
    //--实现班级学生注册操作、定义方法实现添加一个学生数据,并保存在students.txt中,例如:
    //--提供方法实现根据学生编号返回学生详细数据的方法,例如:
    //20220102
    //返回:
    //Student [code=20220102, name=li, age=15,address=gz]
    //null
    
    public class Demo01 {
    }
    
    class Clazz {
        private static String path = "D:\\test\\students.txt";// 绝对路径不用区分进行调用
    
        // 测试代码
        public static void main(String[] args) {
    	// Student [code=20220101, name=zhangsan, age=16,address=gz]
    	// Student [code=20220102, name=li, age=15,address=gz]
    	Student stu = new Student("20220101", "zhangsan", 16, "gz");
    	Student stu1 = new Student("20220102", "li", 15, "gz");
    	// 调用添加数据方法
    //	insert(stu);// 把stu的实例对象的数据调用到students文本中去
    	// 调用返回数据方法
    	getStudent("20220101");//返回:Student [code=20220101, name=zhangsan, age=16, gz=gz]
    	getStudent("20220102");//返回:null
    	
        }
    
        // --提供方法实现根据学生编号返回学生详细数据的方法,
        public static String getStudent(String code) {
    	// 1、构建流【缓冲字符流-读取一行】
    	BufferedReader in = null;
    	try {
    	    // 1、构建流【缓冲字符流-读取一行】
    	    in = new BufferedReader(new FileReader(path));
    
    	    // 2、遍历条件是否满足
    	    String stu = "";
    	    while ((stu = in.readLine()) != null) {
    
    		// 构建格式 code = xxx
    		code = "code=" + code;
    		if ((stu.contains(code))) {
    		    // 返回数据
    		    System.out.println("返回:" + stu);
    		    // 返回String stu
    		    return stu;
    		}
    	    }
    	} catch (IOException e) {
    	    e.printStackTrace();
    	} finally {
    	    try {
    		in.close();
    	    } catch (Exception e2) {
    		e2.printStackTrace();
    	    }
    	}
    	// --条件不满足返回null
    	System.out.println("返回:" + null);
    	return null;
        }
    
        // --实现班级学生注册操作、定义方法实现添加一个学生数据、并保存在students.txt中
        public static void insert(Student student) {
    	// 1、构建流 【字符打印流】
    	OutputStream out = null;
    	OutputStreamWriter writer = null;
    	PrintWriter pw = null;
    	try {
    	    // 调用一个方法查询该学号是否有重复,没有则添加
    	    // 构建输出流 字符流 使用追加模式
    	    out = new FileOutputStream(path, true);
    	    // 转换流
    	    writer = new OutputStreamWriter(out, "UTF-8");
    	    // 字符打印流
    	    pw = new PrintWriter(writer, true);
    	    // 打印输出
    	    pw.println(student.toString());
    	    // 刷新
    	    pw.flush();
    	} catch (FileNotFoundException | UnsupportedEncodingException e) {
    	    e.printStackTrace();
    	} finally {
    	    // 关闭流
    	    try {
    		pw.close();
    		writer.close();
    		out.close();
    	    } catch (IOException e) {
    		e.printStackTrace();
    	    }
    	}
        }
    }
    
    class Student {
        // [code=20220101, name=zhangsan, age=16,address=gz]
        private String code;
        private String name;
        private int age;
        private String address;
    
        public Student(String code, String name, int age, String address) {
    	super();
    	this.code = code;
    	this.name = name;
    	this.age = age;
    	this.address = address;
        }
    
        public Student() {
    	super();
        }
    
        @Override
        public String toString() {
    	return "Student [code=" + code + ", name=" + name + ", age=" + age + ", address=" + address + "]";
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
  • 相关阅读:
    Flutter dart语言特点总结
    python基于PHP+MySQL的大学生交友社交网站
    Node.js 入门教程 16 npm 将软件包安装到哪里
    Python 面试题解析
    【Linux网络编程】信号和定时器
    AP5186 三功能 LED 降压型恒流芯片 手电筒 LED芯片
    net-java-php-python-单位办公OA系统计算机毕业设计程序
    Redis哨兵模式
    10.1网站编写(Tomcat和servlet基础)
    【LeetCode】按公因数计算最大组件大小 [H](并查集)
  • 原文地址:https://blog.csdn.net/Lantzrung/article/details/126108561