美文网首页
java学习

java学习

作者: c667ec5a71d8 | 来源:发表于2018-11-21 17:43 被阅读0次

    下面的例子演示了ByteArrayInputStream 和 ByteArrayOutputStream的使用:

    import java.io.*;

    public class ByteStreamTest {

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

          ByteArrayOutputStream bOutput = new ByteArrayOutputStream(12);

          while( bOutput.size()!= 10 ) {

            // 获取用户输入

            bOutput.write(System.in.read());

          }

          byte b [] = bOutput.toByteArray();

          System.out.println("Print the content");

          for(int x= 0 ; x < b.length; x++) {

            // 打印字符

            System.out.print((char)b[x]  + "  ");

          }

          System.out.println("  ");

          int c;

          ByteArrayInputStream bInput = new ByteArrayInputStream(b);

          System.out.println("Converting characters to Upper case " );

          for(int y = 0 ; y < 1; y++ ) {

            while(( c= bInput.read())!= -1) {

                System.out.println(Character.toUpperCase((char)c));

            }

            bInput.reset();

          }

      }

    }

    以上实例编译运行结果如下:

    asdfghjkly

    Print the content

    a  s  d  f  g  h  j  k  l

    相关文章

      网友评论

          本文标题:java学习

          本文链接:https://www.haomeiwen.com/subject/zincqqtx.html