FileReader是从InputStreamReader继承而来,按字符流读取数据。
FileWriter是从OutputStreamReader继承而来,按字符流写入数据。
public static void main(String[] args) throws Exception {
File file = new File("F:/cc.txt");
if (!file.exists()) {
file.createNewFile();
}
FileReader fr = new FileReader(file);
FileWriter fw = new FileWriter(file);
fw.write("创建FIleReader对象成功后,可以参照以下列表里的方法操作文件。");
fw.flush();
fw.close();
int fd;
char[] bs = new char[300];
fr.read(bs);
for (char c : bs) {
System.out.print(c);
}
fr.close();
Scanner scanner = new Scanner(System.in);
if (scanner.hasNextLine()) {
String next = scanner.nextLine();
System.out.println(next);
}
Scanner sc = new Scanner(System.in);
if(sc.hasNext()){
String ts = sc.next();
System.out.println(ts);
}
}
next()和nextLine()方法的区别在于:
next():
- 1、一定要读取到有效字符后才可以结束输入。
- 2、对输入有效字符之前遇到的空白,next() 方法会自动将其去掉。
- 3、只有输入有效字符后才将其后面输入的空白作为分隔符或者结束符。
- next() 不能得到带有空格的字符串。
nextLine():
- 1、以Enter为结束符,也就是说 nextLine()方法返回的是输入回车之前的所有字符。
- 2、可以获得空白。