In the previous lesson, we talked about simple integer data and floating point data, but not the char character type. Today we will talk about the char character type.
[Java char type]
The character type is provided in Java. Similar to C/C ++, the characters in Java are encoded. Exp: The encoded value = 65 is 'A ';
In Java, the Unicode encoding format is used. In general, we think Unicode is a 16-bit encoding rule.
However, many kids still don't understand this, and I am confused. The following is the answer that DU Niang knows:
According to the encoding rules, this assignment is compatible. The system determines the output based on the "value" type. As mentioned above
System. Out. println (numchar + 4 );
What will be output here? Let's take a look:
/* This program try to test the char data type */public class chartest {public static void main (string [] ARGs) {char chartestvar = 4; system. out. println (chartestvar + 4 );}}
The output is as follows:
And the followingProgramSection:
/* This program try to test the char data type */public class chartest {public static void main (string [] ARGs) {char chartestvar = 4; system. out. println (chartestvar + 4); system. out. println (chartestvar );}}
The output is as follows:
Why? We know that in the expression calculation process, type conversion is performed, usually to a large value range; therefore, the first sectionCodeThe output is 8, while the second code outputs 8 and square.
[Java console input stream]
In the Java class library, the system. out class is defined in the Java. lang package. This is a default package. The output data in Java is not like this. When you need to input data from the console, you need
Reference the class library package provided by Java, which is the same as the C language (C language implements input and output through the library functions provided by the system ).
In Java, you can use a struct to input console data. A struct is a class that constructs an object through the constructor of a class, and then is implemented through a class method (similar to a member function in C ++ ).
Read data.
Construct struct object Syntax: jsonobjectidentify = new struct (system. In );
When using this class, you need to import the java. util package. Its syntax is as follows:
Import java. util .*;
Note: Because import is a complete Java statement, there must be fewer semicolons.
The following describes how to input data using a simple code:
/* This program test scaner */import Java. util. *; public class scanerin {public static void main (string [] ARGs) {response getdata = new response (system. in); system. out. print ("Please enter you name:"); system. out. println ("You name is:" + getdata. nextline ());}}
The execution result of this Code is as follows:
The struct object provides many different methods to input different data types:
1. Merge (inputstream in)
Creates a sequence object with the specified input stream.
2. String nextline ()
Read the content of the next line and save it as a string object.
3. String next ()
Reads the next word entered. The words are separated by blank characters. The blank spaces and tabs are counted as blank characters (blank charecter)
4. Int nextint ()
Reads the next integer value of the input.
5. doublet nextdouble ()
Reads the next floating point value of the input.
6. boolean hasnext ()
Checks whether other words exist in the input stream.
7. boolean hasnextint ()
Checks whether an integer exists in the input stream.
8. boolean hasdouble ()
Checks whether there are floating point values in the input stream.
Let me test the method for writing this class:
/* This program test scaner */import Java. util. *; public class scanerin {public static void main (string [] ARGs) {response getdata = new response (system. in); system. out. print ("Please enter you name:"); system. out. println ("You name is:" + getdata. nextline (); system. out. print ("now we have got you name, can we get you age:"); system. out. println ("your age is:" + getdata. nextint (); system. out. print ("can you show The specified CT salary: "); system. out. println ("your CT salary is:" + getdata. nextdouble (); If (getdata. hasnext () system. out. println ("you hava finish you input data !!! ");}}
The program execution process is as follows:
I am a little confused here? The execution process of the IF clause is different from what I think.
[Java Console Password Input]
As in the test above, we know that when using the keystore object in console input, it is in plain text. Sometimes we need to enter a password. Obviously, the password cannot be in plain text.
Otherwise, it will lose the role of confidentiality. In consideration of this, Java provides another input method to implement password input.
Use the console class in Java to input passwords. The usage is as follows:
// First, use the console class to construct an object
Console codeinput = system. Console
String username = codeinput. Readline ();
String Password = codeinput. readpassword ();
The instance code is as follows:
Console codeinput = system. console (); string username; char [] password; username = codeinput. readline ("enter your name:"); Password = codeinput. readpassword ("enter the password :");
Shows the code execution process:
In this way, the plaintext is not displayed, and Java inherits the Unix/Linux features and does not display * or other mask symbols when entering the password.
Note: To use the console class, you must import java. Io. Console; otherwise, it cannot be used. At the same time, the value returned by the object. readpassword () method is of the array type and the string object cannot be used.
Storage. Otherwise, an error is reported. As follows:
[Java reads file content]
Like other programming languages, Java also provides a mechanism for reading and writing files. Unlike C, it is very troublesome to read and write files in C, in Java, you can read and write files through the objects object described earlier.
You need to construct the objects file before use. As follows:
Specified filein = new partition (new file ("test.txt "));
In this way, a file input and output object is constructed, and the file can be read and written through the inventory file object.
To prevent "Pointer" from pointing to file read/write, exception capture is required in the file read/write statement; otherwise, no exception is reported during compilation.
I have not understood how to handle the exception type, so I will not talk about it here. I will discuss it later.
Below I did not look at the code for reading the file:
/* This program test scaner */import Java. util. *; import Java. io. console; import Java. io. file; public class scanerin {public static void main (string [] ARGs) {/* // testing object testing counter getdata = new counter (system. in); system. out. print ("Please enter you name:"); system. out. println ("You name is:" + getdata. nextline (); system. out. print ("now we have got you name, can we get you age:"); system. out. println ("You R age is: "+ getdata. nextint (); system. out. print ("can you show the specific CT salary:"); system. out. println ("your CT salary is:" + getdata. nextdouble (); // console object test console codeinput = system. console (); string username; char [] password; username = codeinput. readline ("enter your name:"); Password = codeinput. readpassword ("enter the password:"); * // file object test try {export filein = new partition (new file ("tes Ttext.txt "); While (filein. hasnext () system. out. println (filein. nextline ();} catch (exception e) {system. out. println (E);}/* If (! Getdata. hasnext () system. Out. println ("you hava finish you input data !!! ");*/}}
The code execution process is as follows:
By taking a closer look at the above execution process, we can find that if there is no file, we cannot read the file and report an exception not found in the file. If we do not need to capture the try structure, in this case, the program runs and crashes. If
No file: texttext.txt. The execution result is as follows:
You can see the objects object and cannot create files. How can I create files? Let's talk about it later. Let's take a look at the file writing.
[Write content to files in Java]
Writing a file in Java is as easy as reading the file content.
To implement a "write" file, we need to construct a printwriter object, as shown below:
Printwriter fileout = new printwriter ("filename.txt ");
This object must reference the java. Io. printerwriter object. to omit many references, we usually reference:
Java. Io .*;
But I recommend that you import different objects when you are not familiar with them. This will increase your understanding of the class library.
Let's look at an example code:
/* This program test scaner */import Java. util. *; import Java. io. console; import Java. io. file; import Java. io. printwriter; import Java. io. *; public class scanerin {public static void main (string [] ARGs) {/* // testing object testing counter getdata = new counter (system. in); system. out. print ("Please enter you name:"); system. out. println ("You name is:" + getdata. nextline (); system. out. print ("now we have got you name, CA N we get you age: "); system. out. println ("your age is:" + getdata. nextint (); system. out. print ("can you show the specific CT salary:"); system. out. println ("your CT salary is:" + getdata. nextdouble (); // console object test console codeinput = system. console (); string username; char [] password; username = codeinput. readline ("enter your name:"); Password = codeinput. readpassword ("enter the password :");*//************ * ***** // File object test try {folder filein = new folder (new file ("testtext.txt"); While (filein. hasnext () system. out. println (filein. nextline ();} catch (exception e) {system. out. println (E );} * ************* // Write File test try {printwriter fileout = new printwriter (New filewriter ("testtext.txt"); fileout. println ("Hello Wellcome to my word"); fileout. println ("this is my Fisrt to write a file via Java"); fileo Ut. println (":)"); fileout. close ();} catch (exception e) {system. out. println (E);} // test whether the file is successfully written. Test whether to append the file or overwrite all. Try {writable filein = new writable (new file ("testtext.txt ")); while (filein. hasnext () system. out. println (filein. nextline ();} catch (exception e) {system. out. println (E);}/* If (! Getdata. hasnext () system. Out. println ("you hava finish you input data !!! ");*/}}
The execution result of the above Code is as follows:
The program is successfully executed. Key point: we can see that repeated import/export operations do not report errors.
In addition, when such a file is written, all the original files will be overwritten. If the original content cannot be overwritten, it must be processed. I found a piece of code as follows for your reference:
Import Java. io. *; import Java. util. *; public class testprintwriter {public static void main (string [] ARGs) throws ioexception {file = new file ("temp2.txt"); If (file. exists () {system. out. println ("file temp.txt already exists. "); system. exit (0);} printwriter output = new printwriter (New filewriter (File); For (INT I = 0; I <10; I ++) {output. print (INT) (math. random () * 100) + "");} output. close (); buffered Reader Input = new bufferedreader (New filereader ("temp2.txt"); int Total = 0; string line; while (line = input. Readline ())! = NULL) {stringtokenizer tokens = new stringtokenizer (line); While (tokens. hasmoretokens () Total + = integer. parseint (tokens. nexttoken ();} output = new printwriter (New filewriter (file, true); output. printf ("\ n"); output. printf ("total is % d", total); output. close ();}}
I have written it here today. I hope that this book will be read from the beginning and end, and I will write a complete summary of this book based on the learning progress.