Java学习_Day 18(学习内容:尚硅谷IO流JAVA零基础P598-P612)

P598 IO流-缓冲流课后练习1

// 异或的加解密操作

P599 IO流-缓冲流课后练习2

P600 IO流-转换流概述与InputStreamReader的使用

	// 字节输入流 ---> 字符输入流的转换
    @Test
    public void test1() throws IOException {
        FileInputStream fis = new FileInputStream("X.txt");
        InputStreamReader isr = new InputStreamReader(fis,"UTF-8"); //使用系统默认字符集UTF-8

        char[] cbuf = new char[20];
        int len;
        while ((len = isr.read(cbuf)) != -1){
            String str = new String(cbuf, 0, len);
            System.out.print(str);
        }
        isr.close();
    }

P601 IO流-转换流实现文件的读入和写出

    // 综合使用InputStreamReader和OutputStreamWriter
    @Test
    public void test2() throws IOException {
        File file1 = new File("XX.txt");
        File file2 = new File("XXX.txt");
        FileInputStream fis = new FileInputStream(file1);
        FileOutputStream fos = new FileOutputStream(file2);
        InputStreamReader isr = new InputStreamReader(fis,"UTF-8");
        OutputStreamWriter osw = new OutputStreamWriter(fos,"gbk");

        char[] cbuf = new char[20];
        int len;
        while ((len = isr.read(cbuf)) != -1){
            osw.write(cbuf, 0, len);
        }
        isr.close();
        osw.close();
    }

P602 IO流-多种字符集编码的说明

/*
 * ASCII/ISO8859-1/GB2312/GBK/Unicode/UTF-8
 */

P603 IO流-标准的输入、输出流

    @Test
    public void test1(){
        BufferedReader br = null;
        try {
            InputStreamReader isr = new InputStreamReader(System.in);
            br = new BufferedReader(isr);

            while (true){
                String data = br.readLine();
                if ("e".equalsIgnoreCase(data) || "exit".equalsIgnoreCase(data)){
                    break;
                }
                String upperCase = data.toUpperCase();
                System.out.println(upperCase);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

P604 IO流-打印流的使用

// PrintStream, PrintWriter
// 提供了一系列重载的println和print的方法

P605 IO流-数据流的使用

/**
 * DataInputStream/DataOutputStream
 * 用户读取或写出基本数据类型的变量或字符串
 */

P606 IO流与网络编程-复习:每天一考

P607 IO流与网络编程-复习:IO流概述

P608 IO流与网络编程-复习:节点流

P609 IO流与网络编程-复习:缓冲流与转换流

P610 IO流与网络编程-复习:其他几个处理流

P611 IO流与网络编程-对象序列化机制的理解

P612 IO流与网络编程-对象流序列化与反序列化字符串操作

package com.io;

import org.junit.Test;

import java.io.*;

public class ObjectInputOutputStreamTest {
    /*
    序列化过程:将内存中的java对象保存到磁盘中或通过网络传输出去
     */
    @Test
    public void testObjectOutputStream(){
        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(new FileOutputStream("object.dat"));
            oos.writeObject(new String("我爱徐州"));
            oos.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (oos != null){
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    // 反序列化
    @Test
    public void testObjectInputStream(){
        ObjectInputStream ois = null;
        try {
            ois = new ObjectInputStream(new FileInputStream("object.dat"));
            Object obj = ois.readObject();
            String str = (String) obj;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (ois != null){
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值