Chinaunix首页 | 论坛 | 博客
  • 博客访问: 280684
  • 博文数量: 28
  • 博客积分: 11
  • 博客等级: 民兵
  • 技术积分: 895
  • 用 户 组: 普通用户
  • 注册时间: 2012-11-19 19:50
个人简介

不畏浮云遮望眼,只缘身在最高层

文章分类

全部博文(28)

文章存档

2014年(1)

2013年(27)

分类: Java

2013-01-31 12:28:03

/********5).转换流的使用******
	  * 首先用"标准*输入流System.in这个字节流用主机系统默认的字符编码方案包装成字符流,
	  * 为了进一步提高效率,又把它包装成缓冲流,然后利用这个缓冲流来读取键盘输入的数据变转换为大写字符输出**/
          //InputStreamReader 类是用来把字节流转换为字符流
         //OutputStreamWriter类是用来把字符流转换为字节流
	  System.out.println("请输入信息(退出输入e或exit)");
	  //使用主机系统默认的字符编码方案,把"标准"输入流(键盘输入)这个字节流包装成字符流,再包装成缓冲流
	  BufferedReader br1=new BufferedReader(new InputStreamReader(System.in));
	//  BufferedReader br1=new BufferedReader(new OutputStreamWriter(System.in));
	  try
	  {
		  //读取用户输入的一行数据--》阻塞程序?
		  for(String s=null;(s=br1.readLine())!=null;)
		  {
			  //判断是否输入是e 或者 exit,如果是,退出,结束输入!
			  if(s.equalsIgnoreCase("e")||s.equalsIgnoreCase("exit"))
			  {
				  System.out.println("安全退出!");
				  break;
			  }
			  System.out.println("-->"+s.toUpperCase()); //将读取到的字符转换为大写输出
			  System.out.println("继续输入信息");
		  }
			  
	  }
	  catch(IOException e)
	  {
		  e.printStackTrace();
	  }
	  finally
	  {
		  if(null!=br1)
		  {
			  try
			  {br1.close();}
			  catch(IOException e)
			  {
				  e.printStackTrace();
			  }
		  }
	  }



	  /********6).数据流的使用****************************/
	  /***************************/
        //DataInputStream 读取基本类型数据
       //DataOutputStream 写入基本类型数据
	  //写数据
	  DataOutputStream dos=null;
	  try
	  {
		  //创建连接到指定文件的数据输出流对象
		  dos=new DataOutputStream(new FileOutputStream("D:\\IOTest\\destData.dat"));
		  dos.writeUTF("my钓鱼岛");//写入UTF字符串
		  dos.writeBoolean(true);//写入布尔值
		  dos.writeDouble(123.56897);//写入double型
		  System.out.println("写入成功");
		  
		  //将D:\\IOTest\\destData.dat中的内容读出来
		  String Dr1;
		  boolean it;
		  double count1;
		  DataInputStream dos1=new DataInputStream(new FileInputStream("D:\\IOTest\\destData.dat"));
		  Dr1=dos1.readUTF();   //先写入的先读出来 
		  it=dos1.readBoolean();
		  count1=dos1.readDouble();
		  dos1.close();
		  System.out.println(Dr1);
		  System.out.println(it);
		  System.out.println(count1);
		  System.out.println("读取成功");
		  
	  }
	  catch(IOException e)
	  {
		  e.printStackTrace();
	  }
	  finally
	  {
		  //关闭流对象
		  if(null!=dos)
		  {
			  try
			  {
				  dos.close();
			  }
			  catch(IOException e)
			  {
				  e.printStackTrace();
			  }
		  }
	  }
阅读(1224) | 评论(0) | 转发(0) |
0

上一篇:java 流 小结2

下一篇:java网络编程小结 1

给主人留下些什么吧!~~