JAVA高级视频02_IO输入与输出日记3(02-06到10)

02-06

PipedOutputStream

PipedInputStream

在应用程序中创建管道通信。字节类型数据的读写

例子:

import java.io.IOException;

import java.io.PipedOutputStream;

 

 

public class Sender extends Thread {

 

    private PipedOutputStream out=new PipedOutputStream();

    public PipedOutputStream getOutputStream(){

        return out;

    }

 

    public void run(){

        String strInfo=new String("hello,receiver!");

       try {

           out.write(strInfo.getBytes());

           out.close();

       } catch (IOException e) {

           e.printStackTrace();

       }

    }

 

   

 

}

 

 

Receiver类:

import java.io.IOException;

import java.io.PipedInputStream;

 

 

public class Receiver extends Thread {

 

    private PipedInputStream in=new PipedInputStream();

    public PipedInputStream getInputStream(){

        return in;

    }

 

    public void run(){

      

      byte[] buf=new byte[1024];

      

       try {

           int len=in.read(buf);

           System.out.println("the following message comes from sender:/n"+

                  new String(buf,0,len));

 

           in.close();

       } catch (IOException e) {

           e.printStackTrace();

       }

    }

}

 

测试类:

import java.io.IOException;

import java.io.PipedInputStream;

import java.io.PipedOutputStream;

 

 

public class PipedStreamTest {

 

 

    public static void main(String[] args) throws IOException {

 

        Sender t1=new Sender();

        Receiver t2=new Receiver();

      

        PipedOutputStream out=t1.getOutputStream();

        PipedInputStream in=t2.getInputStream();

      

        in.connect(out);

      

        t1.start();

        t2.start();

    }

 

}

 

 

 

 

ByteArrayInputStream

ByteArrayOutputStream

字节数组的读写,支持虚拟文件和内存映像

例子:把输入流中的英文小写字符转换成大写,并输出

import java.io.ByteArrayInputStream;

import java.io.ByteArrayOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

 

 

public class ByteArrayTest {

 

    public static void main(String[] args) {

 

      

        String tmp="sdfwerdfgfgeretcf";

      byte[] src=tmp.getBytes();

        ByteArrayInputStream in=new ByteArrayInputStream(src);

        ByteArrayOutputStream out= new ByteArrayOutputStream();

      

        transform(in,out);

       byte [] results=out.toByteArray();

        System.out.println(new String(results));

    }

 

    public static void transform(InputStream in,OutputStream out){

      

       int ch=0;

       try {

           while((ch=in.read())!=-1){

               int upperCh=Character.toUpperCase(ch);

               out.write(upperCh);

           }

       } catch (IOException e) {

           e.printStackTrace();

       }

    }

}

 

StringReader StringWriter对应字符串处理

重视IO程序代码的复用:

System.in 对应键盘输入,System..out 对应屏幕显示输出

InputStreamread()方法返回-1表示输入流的结束

windowsctrl+Z表示输入流的结束,Linuxctrl+D表示输入流的结束.

 

 

02-07-08-09-10

正如张孝祥老师所讲,字符编码:感觉有点晕,编码知识点还需要下点功夫继续学习一下

Unicode

UTF-8

UTF-16

 

字符编码的操作体验

例子1

public class CharCode {

 

    public static void main(String[] args) {

 

        String strChina="李德国";

        for(int i=0;i<strChina.length();i++){

           System.out.println(Integer.toHexString((int)strChina.charAt(i)));

       }

       byte [] buf=strChina.getBytes();

        for(int i=0;i<buf.length;i++){

           System.out.println(Integer.toHexString(buf[i]));

 

       }

        for(int i=0;i<buf.length;i++){

           System.out.write(buf[i]);

 

       }

        System.out.println();

    }

 

}

 

例子2

import java.io.IOException;

 

 

public class CharDecode {

 

    public static void main(String[] args) throws IOException {

 

        System.out.println("please enter a chinese string");

      byte[] buf=new byte[1024];

        String strInfo=null;

       int pos=0;

       int ch=0;

        while(true){

           ch=System.in.read();

           System.out.println(Integer.toHexString(ch));

           switch(ch){

           case '/r':

               break;

           case '/n':

               strInfo=new String(buf,0,pos);

               for(int i=0;i<strInfo.length();i++){

               System.out.println(Integer.toHexString((int)strInfo.charAt(i)));

 

               }

                 

               break;

               default:

                   buf[pos++]=(byte)ch;

           }

       }

    }

 

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值