Java高级编程-IO操作深入

本文深入探讨了Java中的字符编码如UTF-8的重要性及乱码解决方案,详细讲解了内存操作流如ByteArrayOutputStream和管道流PipedOutputStream在多线程间的数据传输应用。

一、字符编码

    实际的开发过程中的常用编码:

  • GBK/GB2312:国标编码,可以描述中文信息。
  • ISO8859-1:国际通用编码,可以用其描述所有的字母信息。
  • UNICODE编码:采用16进制的方式存储。
  • UTF编码:象形文字部分采用16进制编码,而普通字符采用ISO8859-1,它的优势在于可以快速传输,节约带宽,也成为了我们开发中首选的编码,主要使用‘UTF-8’编码。

范例:列出本机属性

public class Main {
    public static void main(String[] args) {
        System.getProperties().list(System.out);
    }
}

    默认编码为:UTF-8

    项目中出现乱码问题 就是编码和解码标准不统一,而最好的解决乱码的方式,就是都使用utf-8,保证编码统一。

二、内存操作流

    在之前使用的全部都是文件操作流,文件操作流的特点,程序利用InputStream读取文件内容,而后程序利用OutputStream向文件输出内容,所有的操作都是以文件为终端的。

1990527fe6e29f93391adb274f7d8400bf8.jpg

    假设说现在需要实现IO操作,但是不希望产生文件就可以用内存作为终端来处理。

93837daf9925d79b2ac022f1c5526f263ce.jpg

    在Java里面提供了两类的额内存操作流:

  • 字节内存操作流:ByteArrayOutputStream、ByteArrayInputStream;
  • 字符内存操作流:CharArrayWriter、CharArrayReader

    ByteArrayInputStream继承关系:

08714271f4c3bdb1a727131ca8e654cdc76.jpg

    CharArrayWriter继承关系:

290cf2d5f1d3531efc47277309414739fc6.jpg

    

    在ByteArrayOutputStream类里面有一个重要的方法,这个方法可以获取全部保存在内存流中的数据信息,该方法为:

  • 获取数据:public byte[] toByteArray();
  • 使用字符串的形式来获取:public  String toString();

范例:内存输入流

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
public class Main {
    public static void main(String[] args) throws  Exception{
        String str = "www.baidu.com";
        InputStream inputStream = new ByteArrayInputStream(str.getBytes());
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        int data = 0;
        while ((data = inputStream.read()) != -1){
            outputStream.write(Character.toUpperCase(data));
        }
        byte result[] = outputStream.toByteArray();
        System.out.println(new String(result));
        inputStream.close();
        outputStream.close();
    }
}

三、管道流

    主要功能是实现两个线程之间的IO处理操作。

0fb71014caae7a2c2b06f0956a5aa3f0ccc.jpg

对于管道流也是分为两类:

  • 字节管道流:PipedOutputStream、PipedInputStream

        连接处理:public void connect​(PipedOutputStream src) throws IOException

  • 字符管道流:PipedWriter、PipedReader

        连接处理:public void connect​(PipedReader snk) throws IOException

范例:管道流操作

import java.io.*;

class SendThread implements  Runnable{
    private  PipedOutputStream output;
    public  SendThread(){
        this.output = new PipedOutputStream();
    }
    public  void run(){
        for(int i = 0; i< 10 ; i++) {
            try {
                this.output.write(("【第"+(i+1)+"个消息发送" + Thread.currentThread().getName() + "】\n").getBytes());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            this.output.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public  PipedOutputStream getOutputStreamStream(){
        return output;
    }

}


class ReceiveThread implements  Runnable{
    private PipedInputStream input;

    public  ReceiveThread(){
        this.input = new PipedInputStream();
    }
    public  void run(){
        byte data[] = new byte[1024];
        int len = 0;
        ByteArrayOutputStream bos  = new ByteArrayOutputStream();//所有的数据保存在内存输出流
        try {
            while((len = this.input.read(data)) != -1){
                bos.write(data,0,len);

            }
            System.out.println("{"+Thread.currentThread().getName()+"接受消息}\n"+new String(bos.toByteArray()));
            bos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            this.input.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    public  PipedInputStream getInput(){
        return input;
    }

}

public class Main {
    public static void main(String[] args) throws  Exception{
        SendThread send = new SendThread();
        ReceiveThread receive = new ReceiveThread();
        send.getOutputStreamStream().connect(receive.getInput());
        new Thread(send,"消息发送线程").start();
        new Thread(receive,"消息接收线程").start();
    }
}

注:初学者,写的不好请见谅,如有相关问题记得私信我

转载于:https://my.oschina.net/chenzhou/blog/2251275

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值