第一次接触到Java的输入输出,感觉有点复杂,作此纪录
输出
常用有 System.out.println(); System.out.print(); System.out.printf(); System.out.write();
print 和 println 都是直接输出,区别在于println 在结束时会换行
printf 和C中的 printf 用法类似,用于标准化格式输出
以上输出为字节流,可用于任何对象的处理,包括二进制对象,而 write 是为字符流设计的,只能处理字符或字符串,即 write 中所有输入将以字符输出
同时,如果使用 System.out.write() 方法进行打印,必须在其后刷新缓冲区,否则数据会丢失无法打印,加上 System.out.write(’\n’); 或 System.out.println(); 或 System.out.print() 或 System.out.flush(); 即可
public class test_1 {
public static void main(String[] args){
System.out.println("hello world");//换行输出
System.out.print("hello world");//不换行输出
System.out.printf("%.2f", 3.14);//格式化输出
System.out.write(50);//字节输出,即依照ASCII码转换
}
}
输入
-
使用 Scanner 类
使用 java.util.* 包,构建 Scanner 对象(在 IDEA 中构建 Scanner 对象时会自动 import 相关包)
import java.util.Scanner; public class test_1 { public static void main(String[] args){ Scanner sc = new Scanner(System.in); String str_1 = new String(); String str_2 = new String(); int num_1 = sc.nextInt();//输入整数 double num_2 = sc.nextFloat();//输入浮点数 str_1 = sc.nextLine();//输入字符串,可包含空格 str_2 = sc.next();//输入字符串,不包含空格 sc.close(); } }
通过创建 Scanner 类的对象,调用类中的方法完成。常用的方法有 nextInt() 输入整数,nextFloat() 输入浮点数,nextLine() 输入字符串,next() 输入不带空格字符串
注意:在完成输入后应该加上 close() 方法来关闭流,否则会警告
-
使用 BufferedReader 类
使用 java.io.* 包,构建 BufferedReader 类对象(在 IDEA 中构建 BufferedReader 对象时会自动 import 相关包)
import java.io.*; public class test_1 { public static void main(String[] args){ int num_1; String str_1 = new String(); BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); try { str_1 = bf.readLine();//读取字符串 num_1 = bf.read();//读取单个字符并返回随对应的ASCII码 bf.close(); } catch (IOException e){ e.printStackTrace(); } } }
通过创建 BufferedReader 类的对象,调用类中的方法完成。常用的方法有 readLine() 读取带空格字符串,read() 读取单个字符并返回其 ASCII 码(如果读取到EOF则返回 -1)
注意:
- 在 read() 中输入1和11得到的数值相同,因为只会读取第一个字符
- 在使用 BufferedReader 创建输入流时必须要 try~catch 或者 throw 防止IO异常(才能用)
-
使用 java.io.Console(不常用)
import java.io.Console; public class test_1 { public static void main(String[] args){ char[] passwords; String str_1 = new String(); Console con = System.console(); str_1 = con.readLine(); passwords = con.readPassword(); } }
java.io.Console 只能在输出流未重新定向的原始控制台中使用,即只能通过命令行启动 java 程序时才起作用,因此该方法没有以上两种方法常见
其特点是拥有 readPassword() 方法,该方法不会回显用户键入的字符
同时,为了防止出现可预见的 IO 错误,尽量使用 try~catch 或者 throw
-
文本 IO
import java.io.*; import java.util.Scanner; //文件写入 public class test_1 { public static void main(String[] args){ try { File fil = new File("D:\\words.txt"); fil.createNewFile(); Scanner sc = new Scanner(System.in); String str = sc.nextLine(); FileWriter wfil = new FileWriter(fil); wfil.write(str); wfil.flush(); sc.close(); wfil.close(); }catch (IOException e) { e.printStackTrace(); } } }
import java.io.*; //文件读取 public class test_1 { public static void main(String[] args){ try { File fil = new File("D:\\words.txt"); FileReader rfil = new FileReader(fil); char[] a = new char[500]; int len = 0; while ((len = rfil.read(a)) > 0){ String str = new String(a, 0, len); System.out.print(str); } rfil.close(); }catch (IOException e) { e.printStackTrace(); } } }
一些理解
-
java 的 IO 都是通过构建类并使用类中的方法实现的
-
Java中关于类,对象及其方法的使用的最典型的语句是
System.out.print();
查阅System.java 的源码我们可以得知,System 是 java.lang 包中的一个 public final class,out 是 System 类中的一个成员,定义为
public static final PrintStream out = null;
因为已经被 static 修饰,所以可以直接通过 System.out 引用,print 是 java.io 包中 PrintStream 类的一个方法,out 作为静态变量可以直接调用 print 方法实现输出,即面向对象编程的最基本的逻辑,通过类中调用对象,对象调用方法实现
- 写入最常见的是 Scanner 类和 BufferedReader 类,当确定输入数据类型时 Scanner 更加方便简洁,但 BufferedReader 读取速度更快,因为其只是读取字符或字符串
- 在 Scanner 中当使用除 nextLine() 以外的 next…() 方法之后使用 nextLine(),那么 nextLine() 将不会读入任何数据,因为 nextLine() 不会忽略换行符而其他方法会将其忽略
更多文章详见个人网站ouz2hou.top,最新文章将第一时间在个人网站发表