常用的類有BufferedReader,
Scanner。
實(shí)例程序:
一,利用 Scanner 實(shí)現(xiàn)從鍵盤讀入integer或float 型數(shù)據(jù)
import
java.util.*;
//import
java.io.*;
class Abc
{
public static
void main(String args[])
{
Scanner in=new
Scanner(System.in); //使用Scanner類定義對象
System.out.println("please input a float number");
float
a=in.nextFloat(); //接收float型數(shù)據(jù)
System.out.println(a);
System.out.println("please input a integer number");
int
b=in.nextInt(); //接收整形數(shù)據(jù)
System.out.println(b);
}
}
二,利用 BufferedReader實(shí)現(xiàn)從鍵盤讀入字符串并寫進(jìn)文件abc.txt中
import java.io.*;
public class
Test1
{
public static
void main(String[] args) throws IOException
{
BufferedReader buf = new
BufferedReader (new
InputStreamReader(System.in));
BufferedWriter buff = new
BufferedWriter(new FileWriter("abc.txt"));
String str = buf.readLine();
while(!str.equals("exit"))
{
buff.write(str);
buff.newLine();
str = buf.readLine();
}
buf.close();
buff.close();
}
}
關(guān)于JDK1.5 Scanner類的說明
Scanner是SDK1.5新增的一個類,可是使用該類創(chuàng)建一個對象.
Scanner reader=new Scanner(System.in);
然后reader對象調(diào)用下列方法(函數(shù)),讀取用戶在命令行輸入的各種數(shù)據(jù)類型:
next.Byte(),nextDouble(),nextFloat,nextInt(),nextLine(),nextLong(),nextShot()
使用nextLine()方法輸入行中可能包含空格.如果讀取的是一個單詞,則可調(diào)用
.next()方法