字节流

1. InputStream 字节输入流

代码演示

     InputStream in = System.in;

     System.out.println("int read(byte b) 方法演示");
//int read()
int bt = 0;
while((bt=in.read())>0){
System.out.print(bt+" ");
if(bt == 10){ //回车\r(13) 换行\n(10)
break;
}
}
System.out.println("\n\rint read(byte[] buffer) 方法演示"); //int read(byte[] buffer)
int length = 0;
byte[] buffer = new byte[10];
while((length=in.read(buffer)) != 0){
for(int i=0; i<length; i++){
System.out.print(buffer[i]+" ");
}
break;
} System.out.println("\n\rint read(byte[] buffer, int offset, int len) 方法演示"); //int read(byte[] buffer, int offset, int len)
int len = 1024;
int count = 0;
byte[] buf = new byte[len];
while((count=in.read(buf, 0, len))>0){
for(int i=0; i<count; i++){
System.out.print(buf[i]+" ");
}
break;
}
in.close();

2. OutputStream 字节输出流

代码演示

     OutputStream out = System.out;
//void write(int b)
out.write(65); //字符A out.write(13); //回车 \r
out.write(10); //换行 \n //flush()
out.flush(); //write(byte[] bytes)
byte[] bytes = new String("张晓明").getBytes();
out.write(bytes); out.write(13); //回车 \r
out.write(10); //换行 \n //write(byte[] bytes, int offset, int length)
bytes = new String("zhangxiaoming").getBytes();
out.write(bytes, 5, 8); out.close();

字符流

1. Reader 字符输入流

代码演示

     Reader reader = new InputStreamReader(System.in);

     //int read()
System.out.println("int read() 方法演示");
int c;
while((c=reader.read()) != 13){
System.out.print((char)c);
}
reader.read(); //int read(char[] buf)
System.out.println("\n\rint read(char[] buf) 方法演示");
int count = 0;
char[] buf = new char[1024];
while((count=reader.read(buf)) > 0){
String str = new String(buf, 0, count);
if(str.indexOf("stop")>=0) break;
System.out.print(str);
} //int read(char[] buffer, int offset, int len)
System.out.println("\n\rint read(char[] buffer, int offset, int len) 方法演示");
int length = 1024;
char[] buffer = new char[length];
while((count=reader.read(buffer, 0, length)) > 0){
String str = new String(buffer, 0, count);
if(str.indexOf("stop")>=0) break;
System.out.print(str);
}

2. Writer 字符输出流

代码演示

     Writer writer = new OutputStreamWriter(System.out);
String str = "中国"; //write(String str) 写入字符串
writer.write(str); //write(int c) 写入单个字符
writer.write(10); //换行符 //write(String str, int offset, int length) 写入部分字符串
writer.write(str, 0, 1); writer.write(10); //write(char[] buf) 写入字符数组
char[] chars = str.toCharArray();
writer.write(chars); writer.write(10); //write(char[] buf, int offset, int length) 写入部分字符数组
writer.write(chars, 0, 1);
writer.write(10); writer.flush(); //append(char c) 追加字符
writer.append('z');
writer.write(10); String str2 = "中华人民共和国";
//append(CharSequence csq)
writer.append(str2);
writer.write(10); //append(CharSequence csq, int offset, int length)
writer.append(str2, 0, 4); writer.close();

最新文章

  1. arcgis将图片转成shp地图
  2. SharePoint—用REST方式访问列表
  3. hdu 4038 2011成都赛区网络赛H 贪心 ***
  4. 泛型类型的协变(covariant)和逆变
  5. 初探appium之appium的使用
  6. Windows 8 电话激活密钥。(更新至 2013-07-21)
  7. 解决python 提示 SyntaxError: Missing parentheses in call to &#39;print&#39;
  8. TSQL基础(一) - 查询
  9. JAVA 计算地球上任意两点(经纬度)距离
  10. 利用webBrowser获取框架内Html页面内容
  11. linux 自动备份数据库
  12. 解析SS、SP、BP寄存器
  13. Apache Mina -2
  14. 2018项目总结(vue+apicloud)
  15. Spring常用注解总结(1)
  16. python模块之时间模块
  17. 微信小程序简单介绍 一
  18. Python3虚拟环境安装:virtualenv、virtualenvwralpper
  19. 【五校联考3day2】C
  20. LeetCode 第 342 题(Power of Four)

热门文章

  1. JavaScript 按位与和逻辑与
  2. H3C 帧中继与水平分割
  3. Python--day48--今日内容
  4. 高级教程: 作出动态决策和 Bi-LSTM CRF 重点
  5. H3C HDLC帧格式
  6. 浅谈集合框架五——集合框架扩展:Collections工具类的使用,自定义比较器
  7. PHP mysql扩展整理,操作数据库的实现过程分析
  8. linux 在 /proc 里实现文件
  9. 【t092】迷之阶梯
  10. 相似文本文档分析之SimHash算法