Java 从键盘读一个String型变量

在网易论坛JAVA版里看见这么一个问题:
主题:Java 从键盘读一个String型变量 怎么写啊?

Re:Java 从键盘读一个String型变量 怎么写啊?
用    DataInputStream
给你写个简单的例子,你看看:
(从键盘输入,判断输入是否为中文)
Code:

import java.io.*;
class strHz{
 public static void main(String args[]){
   DataInputStream is=new DataInputStream(System.in);
   try{
       String str=is.readLine();
       byte a[]=str.getBytes();
       if(a[0]>0)
          System.out.println("You enter is chinese");
       else
          System.out.println("You enter is not chinese");
        }catch(Exception e){ }
  }
}


   近几天在研究JAVA中的流,真的是稀里糊涂。
依稀记得DataInputStream 没有readLine()方法。
查了一下手册,得到如下结论:
嗯,DataInputStream的readLine()方法已经不提倡使用了。
应该使用BufferedReader的readLine()方法代替它。
应该这样,
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();

JDK参考手册上的内容:

readLine()  
         Deprecated. This method does not properly convert bytes to characters. As of JDK 1.1, the preferred way to read lines of text is via the BufferedReader.readLine() method. Programs that use the DataInputStream class to read lines can be converted to use the BufferedReader class by replacing code of the form:  
    DataInputStream d = new DataInputStream(in);
 
with:  
    BufferedReader d
         = new BufferedReader(new InputStreamReader(in));
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值