(由于服务器崩溃,图片资源已丢失)
Sanner输入字符
在C语言中,读取键盘输入是很简单的一件事情,只需要scanf()就可以了,但是在Java中,就有点麻烦了,可以大致概括为如下步骤
- 构建一个Scanner类
- 声明一个字符串变量
- 类的实例化
- 用Scanner中的方法读取输入到声明的字符串变量
在IDEA中构建一个Java类,输入一下代码
import java.util.Scanner;
public class Test {
public static void main(String[] args){
Scanner in=new Scanner(System.in);
String name=in.nextLine();
System.out.println(name);
}
}
可以看到,先构建了一个Scanner类,之后调用in.nextLine将输入的值赋给name变量
nextLine的意思是读取下一行,即到换行符为止,对于这里的方法还有很多选择,比如,如果想输入一个int怎么办,可以调用in.nextInt,同样的,如果想输入一个浮点数,就是in.nextDouble。
更多相关函数详细见《Java核心技术 卷1》 P57
在这本书上还有一个问题,Scanner的输入都是可见的,但是很多时候我们都需要要求某些输入不能被看到,比如输入密码,那么这样该怎么办
应该使用Console类
按照书上的方法这样输入代码
import java.io.Console;
public class Test {
public static void main(String[] args){
Console cons=System.console();
String username=cons.readLine("Username:");
char[] passwd=cons.readPassword();
}
}
结果报错,报错显示NullPointerException,即空指针错误,从Csdn上查了之后,才知道
以Javaw所执行的应用程式(IDEA)没有主控制台(console),所以取不到console物件,System.console()只能是null了。
没办法,这个只能以后有机会再试验了(挖坑)
Scanner输入文件
与输入字符的过程大体类似,但是为了与输入对于,还要多介绍一个写入文件的方法(PrintWriter)。
首先在桌面建立一个测试.txt文件,里面记录”高麒璧是”,并保存
然后更改之前的代码:
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.Scanner;
public class Test {
public static void main(String[] args){
Scanner in= null;
try {
in = new Scanner(Path.of("C:\\Users\\86185\\Desktop\\测试.txt"), StandardCharsets.UTF_8);
} catch (IOException e) {
e.printStackTrace();
}
String name=in.nextLine();
System.out.println(name);
try {
PrintWriter out=new PrintWriter("C:\\Users\\86185\\Desktop\\测试.txt",StandardCharsets.UTF_8);
out.write("大帅哥");
} catch (IOException e) {
e.printStackTrace();
}
String name_2=in.nextLine();
System.out.println(name_2);
}
}
结果报错:
分析后认为是文件已经读取到最后一行,再读取就会找不到新的一行,于是在加入in.close()之后重新运行,问题没有得到解决。、
之后打开测试.txt文件查看,发现里面的内容没有了。。。
淦
推测是PrintWriter覆盖掉了原来文件,那么如何将PrintWriter改成在原来的基础上写入呢
从网上查询之后发现需要将append参数设置成true,但是PrintWriter是没有append参数的,所有需要加入FileOutStream,但是如果这样的话,PrintWriter第一个参数就成为了一个流,之前那样写会报错,需要将编码指定去掉
运行一遍,发现还是报错。。。
从网上查询,才知道原因是PrintWriter刚开始只是将数据写入到了缓冲区,如果想要将缓冲区的数据写入到文件当中,就需要flush(),或者close掉PrintWriter,类似于终端当中的回车操作。
而我选择了一个更方便的方法,将PrintWriter的第二个参数autoFlush设置成true,这样,print,println或者printf函数就会自动加上flush操作了。
最终代码:
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.Scanner;
public class Test {
public static void main(String[] args){
Scanner in= null;
try {
in = new Scanner(Path.of("C:\\Users\\86185\\Desktop\\测试.txt"), StandardCharsets.UTF_8);
} catch (IOException e) {
e.printStackTrace();
}
String name=in.nextLine();
System.out.println(name);
in.close();
try {
PrintWriter out=new PrintWriter(new FileOutputStream("C:\\Users\\86185\\Desktop\\测试.txt",true),true);
out.println("大帅哥");
} catch (IOException e) {
e.printStackTrace();
}
Scanner in_2 = null;
try {
in_2 = new Scanner(Path.of("C:\\Users\\86185\\Desktop\\测试.txt"), StandardCharsets.UTF_8);
} catch (IOException e) {
e.printStackTrace();
}
String name_2=in_2.nextLine();
System.out.println(name_2);
}
}
运行结果: