Standard input:
Scanner cin = new Scanner (system.in); Cin.nextline ();: Enter a line, there may be a space, features similar to the C in the gets (); Cin.next (): Enter a word with a blank character as the end flag, similar to scanf ("%s", str); Cin.nextint ();: Input shaping Data cin.nextdouble (); Enter floating-point data
Above 4 input methods these inputs are visible and do not apply to the console read password
Console password read, application Console class: read one line at a time
For security reasons, the returned password is stored in a one-dimensional array, the password is processed, and the array element is overwritten with a padding value
Code:
Import Java.io.console;public class Main {public static void main (string[] args) {Console cin = System.Console (); String name = Cin.readline ("Username:"); char[] Password = Cin.readpassword ("Password:"); String Pass = "";//beginners do not know if there is any good way for (int i = 0;i<password.length;i++) {pass = pass + password[i];} if (Pass.equals ("123456") && name.equals ("xxx")) {System.out.println ("password is correct");} else {System.out.println ("bad password");}}}
Standard output:
It's more simple.
System.out.println ("Hello");//Plus ln will automatically wrap, without adding.
System.out.println ("Heado" + "," + "World" +2014);//print hello,world2014, for string connection, overloaded with + operator
At the same time, JAVA SE5.0 also inherits the printf () in C ; methods, usage, and C basically the same
Double x = 0.56; System.out.printf ("%.2f", x);//Just No &
System.out.println (); It's the most common.
Input and output of the file:
1. To read a file, construct a scanner object with the files object, such as:
Scanner cin = new Scanner (Paths.get ("MyFile.txt"));
If there is a backslash in the file name, precede each backslash with a backslash:
"C:\\myfile\\myfile.txt"
2. To write, construct the printwriter object, just provide the file name
PrintWriter out = new PrintWriter ("MyFile.txt");
If the file does not exist, the file is created and can be output to System.out with commands like print,println,printlf , and more
Attention:
It is possible to construct a Scannerwith the parameters of the parent string, but this Scanner interprets the string as data instead of the file name
Scanner in = new Scanner ("myfile.txt");
this scanner 10 ' m ' " y " f "
Ps:
When specifying a relative file name, example:myfile.txt or .../myfile.txt
The file is located in the relative location of the Java virtual machine boot path
1. Start the program with the following command if you are in command line mode
Java. Myprog// boot path is the current path of the command interpreter
2. If you are using an integrated development environment, the boot path is controlled by the IDE and you can find the location of the path using the following invocation method:
String dir = system.getproperty ("User.dir");
If you find it troublesome to locate a file, consider using an absolute path, such as "c:\\madirectory\\myfile.txt" or /home/me/directory/myfile.txt
Warning:
If you construct a Scanner with a nonexistent file or construct a printwriter with a file name that cannot be created , then an exception will occur, these exceptions, in the Java is considered to be more severe than "divisible 0", so it is necessary to use the throws tag in the main method
public static void Main (string[] args) throws filenotfoundexception{printwriter out = new PrintWriter ("MyFile.txt");}
PS: When starting a human program with a command line, you can bundle files to system.in and System.out with redirects
Java Myprog <myfile.txt> output.txt
So you don't have to worry about handling filenotfoundexception exceptions.
Java.util.Scanner 5.0
Scanner (File f)
Constructs a Scanner that reads data from a given file
Scanner(String data)
Constructs a Scanner that reads data from a given string
Import Java.io.PrintWriter 1.1
Printewriter (String FileName)
Constructs a printwriterthat writes data to a file, and the file name is specified by the parameter
Java.nio.file.Paths 7
Static Path Get (String Pathname)
Constructs a path based on the given path
Java Input and output summary