软件构造-java的输入与输出(Software construction – input and output of Java)

读取输入

读取“标准输入流”(即控制台窗口),首先需要构造一个与“标准输入流”System.in关联的Scanner对象。

Scanner in = new Scanner(Sysyem.in);

之后,就可以使用Scanner类中的各种方法来进行读取输入了。例如,nextLine方法将读取一行输入:

String name = in.nextLine();

读取一个整数,就调用nextInt方法,读取浮点数,调用nextDouble方法,读取一个单词(以空白符作为分隔),调用next方法。

int a = in.nextInt();
String name = in.next();

Scanner类定义在java.util包中,使用import指令导入相应的包。

import java.util*;

格式化输出

可以使用语句System.out.print(x)将数值x输出到控制台。这条指令将以x的类型所允许的最大非0数位个数打印输出x。例如:

double x = 10000.0/3.0;
System.out.orint(x);

打印输出:3333.3333333333335.

但这样的输入会引发一些麻烦,所以在java5之后沿用了C语言函数库中的printf方法。例如,调用System.out.printf(“%8.2f”,x),会以一个字段宽度打印x,这包括8个字符,另外精度为小数点后的2个字符,也就是说,这回打印一个前导的空格和七个字符,如下所示:

 3333.33

其他使用方法与c语言的printf类似。

文件的输入与输出

读取文件

要想读取一个文件,需要构造一个Scanner对象,如下所示:

Scanner in = new Scanner(Path.of("myfile.txt"),StandardCharsets.UTF_8);

如果文件名中包含反斜杠符号,就要记住在每一个反斜杠之前再额外加一个反斜杠转义。

写入文件

要写入文件,需要构造一个PrintWriter对象,在构造器中,需要提供文件名和字符编码:

PrintWriter out = new PrintWriter("myfile.txt",StandardCharsets.UTf_8);

如果文件不存在,那么则会创建该文件,可以像输出到System.out一样使用print、println以及printf命令;

文件读写异常

在读取文件而文件不存在等情况时,就会发生异常,这需要我们处理IOException类型的异常,可以直接在main函数中使用throws IOException子句标记,或者使用try catch语句来进行异常后的处理,如输出提示信息等。

————————

Read input

To read the “standard input stream” (i.e. console window), you first need to construct a system with the “standard input stream” In associated scanner object.

Scanner in = new Scanner(Sysyem.in);

After reading various methods in the scanner class, you can use them. For example, the nextline method will read one line of input:

String name = in.nextLine();

Read an integer, call nextint method, read floating-point number, call nextdouble method, read a word (separated by blank character), and call next method.

int a = in.nextInt();
String name = in.next();

The scanner class is defined in Java In the util package, use the import instruction to import the corresponding package.

import java.util*;

Format output

You can use the statement system out. Print (x) outputs the value x to the console. This command will print out x with the maximum number of non-zero digits allowed by the type of X. For example:

double x = 10000.0/3.0;
System.out.orint(x);

Printout: 3333.3335

However, such input will cause some trouble, so the printf method in the C language function library is used after java5. For example, call system out. Printf (“% 8.2f”, x) will print x with a field width, which includes 8 characters, and the accuracy is 2 characters after the decimal point, that is, a leading space and seven characters will be printed this time, as shown below:

 3333.33

Other usage methods are similar to printf in C language.

File input and output

read file

To read a file, you need to construct a scanner object, as shown below:

Scanner in = new Scanner(Path.of("myfile.txt"),StandardCharsets.UTF_8);

If the file name contains a backslash symbol, remember to add an extra backslash escape before each backslash.

write file

To write a file, you need to construct a printwriter object. In the constructor, you need to provide the file name and character code:

PrintWriter out = new PrintWriter("myfile.txt",StandardCharsets.UTf_8);

If the file does not exist, it will be created and can be output to system Use the print, println and printf commands the same way as out;

File read / write exception

Exceptions will occur when the file is read but does not exist, which requires us to deal with IOException type exceptions. You can directly use the throw IOException clause in the main function to mark, or use the try catch statement to deal with the exceptions, such as outputting the prompt information.