一. Java Scanner 类
java.util.Scanner是 Java5 的新特征,我们可以通过 Scanner 类来获取用户的输入。
1. 创建Scanner对象
Scanner sc = new Scanner(System.in);
Java 的控制台输入由 System.in 完成。
2. next() 与 nextLine() 方法
通过 Scanner 类的 next() 与 nextLine() 方法获取输入的字符串,在读取前我们一般需要使用 hasNext 与 hasNextLine 判断是否还有输入的数据。
2.1 使用 next 方法
实例
import java.util.Scanner; //导包
public class ScannerDemo {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// 从键盘接收数据
// next方式接收字符串
System.out.println("next方式接收:");
// 判断是否还有输入
while (scan.hasNext()) {
String str1 = scan.next();
System.out.println("输入的数据为:" + str1);
}
System.out.println("welcome");
scan.close();
}
}
运行结果:
next方式接收:
new place
输入的数据为:new
输入的数据为:place
|
最下面一行光标依然在闪烁,表示可以继续输入,并且System.out.println("welcome");
语句也未输出
方法解释
如果此扫描器的输入(缓冲区)中有另一个token(输入的字符或数字),则返回true。根本没有提到什么时候返回false。其实执行过程是这样的(重点),当执行到hasNext()时,它会先扫描缓冲区中是否有字符,有则返回true,继续扫描。直到扫描为空,这时并不返回false,而是将方法阻塞,等待你输入内容然后继续扫描。
解决方法
使用带有参数的重载方法,当扫描到的字符与参数值匹配时返回true
修改后的代码如下:
import java.util.Scanner; //导包
public class ScannerDemo {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// 从键盘接收数据
// next方式接收字符串
System.out.println("next方式接收:");
// 判断是否还有输入
while (!scan.hasNext("#")) { //匹配#返回true,然后取非,即输入#退出循环
String str1 = scan.next();
System.out.println("输入的数据为:" + str1);
}
System.out.println("welcome");
scan.close();
}
}
运行结果如下:
next方式接收:
new place #
输入的数据为:new
输入的数据为:place
welcome
Process finished with exit code 0
2.2 使用 nextLine 方法
实例
import java.util.Scanner;
public class ScannerDemo {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// 从键盘接收数据
// nextLine方式接收字符串
System.out.println("nextLine方式接收:");
// 判断是否还有输入
while (scan.hasNextLine()) {
String str2 = scan.nextLine();
System.out.println("输入的数据为:" + str2);
}
scan.close();
}
}
运行结果如下:
nextLine方式接收:
new place
输入的数据为:new place
new new place
输入的数据为:new new place
|
可以使用 if 语句来判断是否为空行来结束循环
代码如下:
import java.util.Scanner;
public class ScannerDemo {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// 从键盘接收数据
// nextLine方式接收字符串
System.out.println("nextLine方式接收:");
// 判断是否还有输入
while (scan.hasNextLine()) {
String str2 = scan.nextLine();
if(str2.equals(""))
break;//输入回车结束循环
System.out.println("输入的数据为:" + str2);
}
scan.close();
}
}
运行结果如下:
nextLine方式接收:
new place
输入的数据为:new place
Process finished with exit code 0
next() 与 nextLine() 区别
next():
- 一定要读取到有效字符后才可以结束输入。
- 对输入有效字符之前遇到的空白,next() 方法会自动将其去掉。
- 只有输入有效字符后才将其后面输入的空白作为分隔符或者结束符。
next() 不能得到带有空格的字符串。
nextLine():
- 以Enter为结束符,也就是说 nextLine()方法返回的是输入回车之前的所有字符。
- 可以获得空白。
如果要输入 int 或 float 类型的数据,在 Scanner 类中也有支持,但是在输入之前最好先使用 hasNextXxx() 方法进行验证,再使用 nextXxx() 来读取。
2. 读写文件
字节流和字符流:
- 字节流:以字节为单位,每次次读入或读出是8位数据。可以读任何类型数据。
- 字符流:以字符为单位,每次次读入或读出是16位数据。其只能读取字符类型数据。
输出流和输入流:
- 输出流:从内存读出到文件。只能进行写操作。
- 输入流:从文件读入到内存。只能进行读操作。
注意:这里的出和入,都是相对于系统内存而言的。
节点流和处理流:
- 节点流:直接与数据源相连,读入或读出。
- 处理流:与节点流一块使用,在节点流的基础上,再套接一层,套接在节点流上的就是处理流。
为什么要有处理流?直接使用节点流,读写不方便,为了更快的读写文件,才有了处理流。
下面将要讨论的两个重要的流是 FileInputStream 和 FileOutputStream:
2.1 FileInputStream
该流用于从文件读取数据,它的对象可以用关键字 new 来创建。
InputStream f = new FileInputStream("C:/java/hello");
创建了InputStream对象,就可以使用下面的方法来读取流或者进行其他的流操作。
- public void close() throws IOException{}
关闭此文件输入流并释放与此流有关的所有系统资源。抛出IOException异常。 - protected void finalize()throws IOException {}
这个方法清除与该文件的连接。确保在不再引用文件输入流时调用其 close 方法。抛出IOException异常。 - public int read(int r)throws IOException{}
这个方法从 InputStream 对象读取指定字节的数据。返回为整数值。返回下一字节数据,如果已经到结尾则返回-1。 - public int read(byte[] r) throws IOException{}
这个方法从输入流读取r.length长度的字节。返回读取的字节数。如果是文件结尾则返回-1。 - public int available() throws IOException{}
返回下一次对此输入流调用的方法可以不受阻塞地从此输入流读取的字节数。返回一个整数值。
2.2 FileOutputStream
该类用来创建一个文件并向文件中写数据。
OutputStream f = new FileOutputStream("C:/java/hello")
创建OutputStream 对象完成后,就可以使用下面的方法来写入流或者进行其他的流操作。
- public void close() throws IOException{}
关闭此文件输入流并释放与此流有关的所有系统资源。抛出IOException异常。 - protected void finalize()throws IOException {}
这个方法清除与该文件的连接。确保在不再引用文件输入流时调用其 close 方法。抛出IOException异常。 - public void write(int w)throws IOException{}
这个方法把指定的字节写到输出流中。 - public void write(byte[] w)
把指定数组中w.length长度的字节写到OutputStream中。