对java中的输入/输出的总结(字节流,字符流)

1,先是关于字节流

import  java.io. * ;

public   class  SimpleIO  {

    
/**
    System 类包含一些有用的类字段和方法。它不能被实例化。
    字段摘要
static PrintStream     err “标准”错误输出流。
static InputStream     in  “标准”输入流。
static PrintStream     out “标准”输出流。

字段不是一个类,它是一个类的static属性
out是System类的一个属性,而它的类型是PrintStream
PrintStream是一个类,它有个方法是println().
     
*/

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

        
char c;
        System.out.println(
"请输入一个字符:");
        
try
        
{
            c
=(char)System.in.read();
            System.out.println(
"你输入的字符是:"+c);
        }

        
catch(IOException e)
        
{
            System.out.println(
"产生异常");
        }

    }


}

 
2,关于字符流的考虑 。

import  java.io. * ;

public   class  SimpleIOs  {

    
/**

java.lang.Object
  继承者 java.io.Reader
      继承者 java.io.InputStreamReader
      
      构造方法摘要
InputStreamReader(InputStream in)
          创建一个使用默认字符集的 InputStreamReader。
InputStreamReader(InputStream in, Charset cs)
          创建使用给定字符集的 InputStreamReader。
InputStreamReader(InputStream in, CharsetDecoder dec)
          创建使用给定字符集解码器的 InputStreamReader。
InputStreamReader(InputStream in, String charsetName)
          创建使用指定字符集的 InputStreamReader。
      
java.lang.Object
  继承者 java.io.Reader
      继承者 java.io.BufferedReader
      
      构造方法摘要
BufferedReader(Reader in)
          创建一个使用默认大小输入缓冲区的缓冲字符输入流。
BufferedReader(Reader in, int sz)
          创建一个使用指定大小输入缓冲区的缓冲字符输入流。
      
      readLine()为BufferedReader扣的方法,
      
      public String readLine()
                throws IOException

    读取一个文本行。通过下列字符之一即可认为某行已终止:换行 (' ')、回车 (' ') 或回车后直接跟着换行。

    返回:
        包含该行内容的字符串,不包含任何行终止符,如果已到达流末尾,则返回 null 
    抛出:
        IOException - 如果发生 I/O 错误




    InputStreamReader 是字节流通向字符流的桥梁:它使用指定的 charset 读取字节并将其解码为字符。它使用的字符集可以由名称指定或显式给定,否则可能接受平台默认的字符集。

每次调用 InputStreamReader 中的一个 read() 方法都会导致从基础输入流读取一个或多个字节。要启用从字节到字符的有效转换,可以提前从基础流读取更多的字节,使其超过满足当前读取操作所需的字节。

为了达到最高效率,可要考虑在 BufferedReader 内包装 InputStreamReader。例如:

 BufferedReader in
   = new BufferedReader(new InputStreamReader(System.in));
 
     
*/

    
public static void main(String[] args) {
        
// TODO Auto-generated method stub
        
        String s
="";
        
        System.out.println(
"请输入一个字符:");//只读取一个字符,按字符(char)读取.
            
            Reader in
=new InputStreamReader(System.in);
            
try
            
{
                System.out.println(
"你输入的字符串是:"+(char)in.read());        
            }

            
catch(IOException e)
            
{
                System.out.println(
"出现一个异常");
            }

            
        System.out.println(
"请输入一串字符:");//读取一串字符,按字符串(String)读取
        
        BufferedReader newin 
= new BufferedReader(new InputStreamReader(System.in));
        
        
try
        
{
            s
=newin.readLine();
            System.out.println(
"你输入的字符串为:"+s);
        }

        
catch(IOException e)
        
{
        System.out.println(
"发生异常!");
        }

        
        }

    }



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值