1. Read input
New Scanner (system.in);
Enter a line (with spaces)
String str = in.nextline ()
Reads a word (with whitespace as a delimiter)String firstName = In.next ();Reads an integerint age = In.nextint ();Read a floating-point numberin.nextdouble ()The input is visible, the scanner class is not suitable for reading passwords from the console, and Java 6 introduces the console implementationConsole cons = System.Console ();char[] passwd = Cons.readpassword ("Password:");For security reasons, the returned password is stored in a one-dimensional character array instead of in a string. Once the password has been processed, the array element should be overwritten with a padding value immediately. Use console input to read only one line at a time. Java.util.Scanner5.0Scanner (InputStream in)Creates a scanner object with the given input stream String nextline ()Reads the next line of inputString Next ()Reads the next word of the input (with a space as a delimiter)int Nextint ()double nextdouble ()Reads and converts the next character sequence that represents an integer or floating-point numberboolean hasnext ()Detect if there are other words in the inputboolean hasnextint ()boolean hasnextdouble ()Detects if there is also the next character sequence that represents an integer or floating-point number Java.lang.System1.0static Console Console () 6 If it is possible to interoperate, return a console object for the user interacting through the console window. Otherwise, NULL is returned. You can use the console object for any program that is launched through a console window. Otherwise, its availability will be related to the system in use.Java.io.Console 6Static char[] Readpassword (String Prompt,object...args)static string ReadLine (String Prompt,object...args)Displays the string primpt and reads the user input, knowing that the input line ends. The args parameter can be used to provide an input format. 2. Format the output System.out.printf ()Each format specifier that starts with a% character is replaced with the corresponding parameter. The conversion character at the end of the format specifier indicates the numeric type being formatted: F for floating-point numbers, and d for decimal integers. The conversion character for printf:
Flags for PRINTG
+ &N Bsp printing of symbols for positive and negative numbers +3333.33 Spaces Add a space before positive numbers | 3333.33| System.out.printf ("%, (. 2f)") use a grouped delimiter and enclose a negative number in parentheses you can format any object with the S-conversion character. The Formatto method is called for any object that implements the Formattable interface, otherwise the ToString method is called, which converts the object to a string. You can use the static String.Format method to create a formatted string without printing the output: String message = String.Format ("hello,%s. Next year,you ' ll be%d ", name,age); time and date conversion
A formatted string can be used to spend the parameter index to be formatted, the index must be immediately after the% and terminated with $, for example: System.out.printf ("%1 $ s%2$TB%2$te,%2$ty," Date: ", new Date ());d ate: February 9, 2004 you can also choose to use the < flag, indicating that the parameters in the preceding format description will be used again. System.out.printf ("%s%tb%<te%<ty", "Date:", new Date ());
Parameter index starting from 1
3. File input and output reads, constructs a scanner object with the Files objectScanner in = new Scanner (Path.get ("MyFile.txt"));If the file name contains a backslash symbol, remember to add an extra backslash before each backslash: "C:\\mydirectory\\myfile.txt" then you can read and write the file using any of the scanner methods The PrintWriter object needs to be constructed. In the constructor, you only need to provide the file namePrintWriter out = new PrintWriter ("MyFile.txt");If the file does not exist, create the file. You can use print,println,printf like System.out. If you construct a scanner with a nonexistent file, or if you construct a printwriter with a filename that cannot be created, an exception occurs. public static void Main (string[] args)Throw FileNotFoundException{Scanner in = new Scanner (Path.get ("MyFile.txt")); ...} Java.util.Scanner5.0Scanner (File f)Constructs a scanner that reads data from a given fileScanner (String data)Constructs a scanner that reads data from a given stringjava.io.PointWriter 1.1 printwriter (String filename)Constructs a printwriter that writes data to a file. Java.nio.file.Paths7static Path Get (String pathname)Constructs a path based on the given path name
Java basic Syntax < three > Input output