黑马程序员:将字符串的所有字符转化为大写(ByteArrayOutPutStream/ByteArrayInPutStream)

来源:互联网 发布:ubuntu引导windows 编辑:程序博客网 时间:2024/06/05 17:40

import java.io.*;

/**
 *
 */
/**
 * @author Administrator
 *
 */
public class ByteArrayTest {

 /**
  * @param args
  */

 public static void main(String[] args) {
  // TODO Auto-generated method stub

  String tem = "abcdefgh";
  byte[] src = tem.getBytes();
  ByteArrayInputStream in = new ByteArrayInputStream(src);
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  transform(in, out);
  byte[] result = out.toByteArray();
  System.out.print(new String(result));
  transform(System.in, System.out); // 键盘输入屏幕输出 ,ctrl+"z"结束键盘输入
 }

 public static void transform(InputStream in, OutputStream out) {
  
  try {
   int ch = in.read();
   while (ch  != -1) {
    int reUpper = Character.toUpperCase((char) ch);
    out.write(reUpper);
    ch=in.read();
   }
  } catch (Exception e) {
   // TODO: handle exception
   e.printStackTrace();
  }

 }

}

原创粉丝点击