IO流与网络编程-复习
1. 说明流的三种分类方式
数据单位:字节流、字符流
流向:输入流、输出流
流的角色:节点流、处理流

2. 写出4个IO流中的抽象基类,4个文件流,4个缓冲流

InputStream	FileInputStream		BufferedInputStream
OutputStream	FileOutputStream		BufferedOutputStream
Reader		FileReader			BufferedReader
Writer		FileWriter			BufferedWriter

InputStreamReader:父类Reader
异常:XxxException XxxError

RandomAccessFile

3.字节流与字符流的区别与使用情景
字节流:read(byte[] buffer) / read()	非文本文件
字符流:read(char[] cbuf) / read()	文本文件
如果只是对文本文件的复制,可以使用字节流,如果需要读取文件数据,则只能使用字符流

4.使用缓冲流实现a.jpg文件复制为b.jpg文件的操作
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File("a.jpg")));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File("b.jpg")));

byte[] buffer = new byte[1024];
int len;
while((len = bis.read(buffer)) != -1){
	bos.write(buffer,0,len);
}

bos,close()
bis.close()

//此时的异常应该使用try-catch-finally处理。

5. 转换流是哪两个类,分别的作用是什么?请分别创建两个类的对象。
InputStreamReader:将输入的字节流转换为输入的字符流。解码
OutputStreamWriter:将输出的字符流转换为输出的字节流。编码

InputStreamReader isr = new InputStreamReader(new FileInputStream("a.txt"),"utf-8");

OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("b.txt"),"gbk");

package top.qaqaq.java.P606;

import org.junit.jupiter.api.Test;

import java.io.*;

/**
 * @author RichieZhang
 * @create 2022-10-30 下午 4:05
 */
public class StreamTest {

    @Test
    public void test1(){
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            bis = new BufferedInputStream(new FileInputStream("爱情与友情.jpg"));
            bos = new BufferedOutputStream(new FileOutputStream("爱情与友情5.jpg"));

            byte[] buffer = new byte[1024];
            int len;
            while ((len = bis.read(buffer)) != -1){
                bos.write(buffer,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bos != null){
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bis != null){
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Test
    public void test2(){
        InputStreamReader isr = null;
        OutputStreamWriter osw = null;
        try {
            FileInputStream fis = new FileInputStream("dbcp.txt");
            FileOutputStream fos = new FileOutputStream("dbcp_gbk1.txt");

            isr = new InputStreamReader(fis,"utf-8");
            osw = new OutputStreamWriter(fos, "gbk");

            char[] cbuf = new char[1024];
            int len;
            while ((len = isr.read(cbuf)) != -1){
                osw.write(cbuf,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (osw != null){
                try {
                    osw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (isr != null){
                try {
                    isr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇