Java zero foundation entry series – Day7 Java input and output
This article mainly introduces the input and output of Java. Of course, this is about the input and output under the console. The design of window program will be described in detail in subsequent chapters.
The output of Java is very simple. Call system out. Println () prints out what you want to output. We've seen it before. When the println () method is called, a newline character will be automatically added after it. If you don't want it to add a newline character, you need to use the print () method, and the subsequent printout will follow immediately instead of starting another line. As like as two peas, there is another common method, printf (), which is familiar to printf () if you have studied C or C++, yes, it is almost the same in Java. Can be used to format the output. The first parameter represents the format. There will be one or more converters in the format. The subsequent parameters represent the replacement content and are used to replace the converter. It's no use saying so much. Go straight to the code.
package pers.frank.test;import java.util.Date;public class Test{static void main(String[] args) {/*** 输出字符串 ***/// %s表示输出字符串,也就是将后面的字符串替换模式中的%sSystem.out.printf("%s",new Integer(1212)); %n表示换行System.out.printf("%s%n","end line"); 还可以支持多个参数System.out.printf("%s = %s%n","Name","Zhangsan" %s将字符串以大写形式输出System.out.printf("%s = %s%n",1)"> 支持多个参数时,可以在%s之间插入变量编号,1$表示第一个字符串,3$表示第3个字符串System.out.printf("%1$s = %3$s %2$s%n","san","Zhang");* 输出boolean类型 ***/System.out.printf("true = %b; false = ",1)">true); System.out.printf("%b%n",1)">false* 输出整数类型**Integer iObj = 342; %d表示将整数格式化为10进制整数System.out.printf("%d; %d; %d%n",-500,2343L,iObj); %o表示将整数格式化为8进制整数System.out.printf("%o; %o; %o%n",1)"> %x表示将整数格式化为16进制整数System.out.printf("%x; %x; %x%n",1)"> %X表示将整数格式化为16进制整数,并且字母变成大写形式System.out.printf("%X; %X; %X%n",iObj);* 输出浮点类型**Double dObj = 45.6d %e表示以科学技术法输出浮点数System.out.printf("%e; %e; %e%n",-756.403f,7464.232641d %f表示以十进制格式化输出浮点数System.out.printf("%f; %f; %f%n",1)"> 还可以限制小数点后的位数System.out.printf("%.1f; %.3f; %f%n",dObj);* 输出日期类型** %t表示格式化日期时间类型,%T是时间日期的大写形式,在%t之后用特定的字母表示不同的输出格式Date date = new Date();long dataL = date.getTime(); 格式化年月日 %t之后用y表示输出日期的年份(2位数的年,如99) %t之后用m表示输出日期的月份,%t之后用d表示输出日期的日号System.out.printf("%1$ty-%1$tm-%1$td; %2$ty-%2$tm-%2$td%n"<span style="color: rgba(0,date,dataL); %t之后用Y表示输出日期的年份(4位数的年), %t之后用B表示输出日期的月份的完整名, %t之后用b表示输出日期的月份的简称System.out.printf("%1$tY-%1$tB-%1$td; %2$tY-%2$tb-%2$td%n"<span style="color: rgba(0,dataL); 以下是常见的日期组合 %t之后用D表示以 "%tm/%td/%ty"格式化日期System.out.printf("%1$tD%n"<span style="color: rgba(0,date);%t之后用F表示以"%tY-%tm-%td"格式化日期System.out.printf("%1$tF%n"<span style="color: rgba(0,date);* 输出时间类型** 输出时分秒 %t之后用H表示输出时间的时(24进制),%t之后用I表示输出时间的时(12进制), %t之后用M表示输出时间的分,%t之后用S表示输出时间的秒System.out.printf("%1$tH:%1$tM:%1$tS; %2$tI:%2$tM:%2$tS%n" %t之后用L表示输出时间的秒中的毫秒System.out.printf("%1$tH:%1$tM:%1$tS %1$tL%n" %t之后p表示输出时间的上午或下午信息System.out.printf("%1$tH:%1$tM:%1$tS %1$tL %1$tp%n" 以下是常见的时间组合 %t之后用R表示以"%tH:%tM"格式化时间System.out.printf("%1$tR%n" %t之后用T表示以"%tH:%tM:%tS"格式化时间System.out.printf("%1$tT%n" %t之后用r表示以"%tI:%tM:%tS %Tp"格式化时间System.out.printf("%1$tr%n"* 输出星期** %t之后用A表示得到星期几的全称System.out.printf("%1$tF %1$tA%n" %t之后用a表示得到星期几的简称System.out.printf("%1$tF %1$ta%n" 输出时间日期的完整信息System.out.printf("%1$tc%n"<span style="color: rgba(0,date); } }
OK, this is actually picking someone else's code.
Most of the output formats are included here. You can use multiple codes several times. It's still that sentence. You don't need to remember it all. You just need to have a concept to know that such an effect can be achieved. It's not too late to use it and check it again. After that, practice makes perfect.
In string type, the usage of format method is exactly the same as this, except that it will not be output to the console, but will be used in subsequent code after processing the string.
Input in Java is not difficult, but it is a little more troublesome than output. You need to construct a scanner object first, and then associate system In. See code:
java.util.Scanner; main(String[] args) { Scanner in = Scanner(system.in); System.out.println("What's your name?"); String name = in.nextLine(); System.out.printf("Hello!%s!How old are you?"<span style="color: rgba(0,name);int age = in.nextInt(); System.out.printf("So,you are %d years old."<span style="color: rgba(0,age); } }
When the nextline () method is called, the program pauses until you enter data and press enter. Then return the row of data you entered as the result. Nextline reads in a line of string, while the nextint () method expects to read in an integer. If the input is not an integer, an error will be reported. In addition to these two methods, there is the next () method, which reads in a word. Nextdouble(), reads in a floating-point number, hasnext() judges whether there are other words in the input, hasnextint() hasnextdouble() judges whether there are integers or floating-point numbers.
In addition to reading input from the console, it can also be read from a file.
Scanner in = new Scanner(Path.get("myfile.txt"));
If the file name contains a backslash, you need to add an additional backslash before the backslash: "C: \ \ windows \ \ text. TXT"
If the file does not exist, a file will be created and can be output to system Use the print, println, and printf methods the same as out.
The file reading and writing will be described in detail in the chestnuts later. I won't say more here for the time being.
So far, the content of this article is over. Welcome to continue your attention!