JAVA input and output summary

Source: Internet
Author: User

JAVA input and output summary

Standard input:

Cin = new partition (System. in); cin. nextLine ();: enter a line, which may contain spaces. The function is similar to gets (); cin in C. next ();: enter a word, ending with a blank character, similar to scanf ("% s", str); cin. nextInt ();: Enter the integer data cin. nextDouble (); input floating point data

 

The preceding four input methods are visible and do not apply to password reading on the console.

Console Password reading. Application Console class: reads one line at a time

For security, the returned password is stored in a one-dimensional array, and the password is processed. The array element is overwritten with a fill 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 = ""; // you do not know if there is any good solution for (int I = 0; I <Password. length; I ++) {Pass = Pass + Password [I];} if (Pass. equals ("123456") & name. equals ("xxx") {System. out. println ("correct password");} else {System. out. println ("Incorrect password ");}}}

Standard output:

It's easy.

System. out. println ("hello"); // when ln is added, the new line is automatically wrapped.
System. out. println ("heado" + "," + "world" + 2014); // print hello, world2014. The + operator is overloaded for string connection.

At the same time, JAVA SE5.0 also follows the printf (); Method in C, and its usage is basically the same as that in C.

Double x = 0.56; System. out. printf ("%. 2f", x); // only the & operator is missing

System. out. println (); is the most common


File input and output:

1. to read a File, you must use the File object to construct a readable object, such:

Scanner cin = new Scanner(Paths.get("myfile.txt"));

If there is a backslash in the file name, add a backslash before each backslash:

“C:\\MyFile\\myfile.txt”

2. To write data, you must construct a PrintWriter object and provide the file name.

PrintWriter out = new PrintWriter("myfile.txt");

If the file does not exist, create a file and use the print, println, printlf, and other commands to output it to System. out.

 

Note:


You can construct a token with the parent string parameter, but this delimiter interprets the string as data rather than the file name.

Using in = new scanner(using myfile.txt ");

This token uses the parameter as data containing 10 characters: 'M', 'y', and 'F '.


PS:

When a relative file name is specified, for example, myfile.txt or.../myfile.txt

The file is located at the relative location of the startup path of the Java Virtual Machine.

 

1. Run the following command to start the program:

Java. MyProg // the startup path is the current path of the command interpreter

2. If the integrated development environment is used, the startup path is controlled by IDE. You can use the following call method to locate the path:

String dir = System.getProperty(“user.dir”);

If it is difficult to locate a file, you can use an absolute path, for example, "C: \ madirectory \ myfile.txt" or/home/me/directory/myfile.txt.


Warning:

If you use a non-existing file to construct a principal or a file name that cannot be created to construct a PrintWriter, exceptions will occur, java is considered to be more serious than "Division by zero". Therefore, throws must be used in the main method to mark

public static void main(String[] args)throws FileNotFoundException{PrintWriter out = new PrintWriter("myfile.txt");}

PS: when you start a program using the command line, you can use redirection to bind the file to System. in And System. out.

Java MyProg <myfile.txt> output.txt

In this way, you don't have to worry about processing the FileNotFoundException exception.


Java. util. Memory 5.0

Partition (File f)

Construct a token for reading data from a given file

Round (String data)

Construct a token for reading data from a given string

ImportJava. io. PrintWriter 1.1

PrinteWriter (String FileName)

Construct a PrintWriter that writes data to a file. The file name is specified by the parameter.

Java. nio. file. Paths 7

Static Path get (String Pathname)

Construct a Path based on the given Path







How many types of JAVA input/output streams are available?

The Java input/output (I/O) mechanism provides a simple and standardized API for reading and writing characters and bytes of data from different data sources. In the article "Object-Oriented Programming: Java collection more effectively manages elements", we have discussed classes and functions in the Java collection class architecture and introduced their sorting functions. In this article, we will learn about these I/O classes, interfaces, and operations provided by the Java platform. Let's start by learning about Java data streams.

Data Stream

All Java I/O mechanisms are based on data streams, which represent the flow sequence of character or byte data. Java I/O Stream provides a standard method for reading and writing data. Any object in Java that represents a data source can read and write data in the form of data streams.

Java. io is the main software package for most data stream-oriented input/output classes. This package contains two abstract classes: InputStream and OutputStream. The input/output classes of all other surface image data streams must be extended.

The java. io package provides classes and interfaces that define useful abstractions at the top of the read/write operations provided by the InputStream and OuputStream classes. For example, the ObjectInputStream class allows you to read data from input/output streams as objects, while the ObjectOutputStream class allows you to write Java objects into data streams.

Optimize the read/write process
JDK 1.1 adds a set of read/write classes that provide more useful abstraction and better input/output performance than existing data stream classes. For example, BufferedReader and BufferedWriter are used to read and write text from character-based input and output streams. The BufferdReader class caches characters to read strings, arrays, and text strings more efficiently. BufferedWriter class cache characters to write strings, arrays, and text strings more efficiently. BufferedReader and BufferedWriter can be set as needed.

The reader and writer classes in the Java input/output architecture include LineNumberReader, CharArrayReader, FileReader, FilterReader, PushbackReader, PipedReader, StringReader, and other classes. These classes are the wrap classes at the top of the InputStream and OuputStream classes, so they provide methods similar to the InputStream and OuputStream classes. However, these classes provide more efficient and useful abstractions for reading and writing specific objects, such as files, character arrays, and strings.

Read data
When you extract an input stream from a corresponding data source object or create a reader object, an input stream is automatically opened. For example, to open an input stream for a file, we only need to pass the file name to the constructor of the Java. io. FileReader object in the following way:

Java. io. FileReader fileReader = new java. io. FileReader ("/home/me/myfile.txt ");

To read one byte data in the input stream at the bottom layer of FileReader in sequence, you only need to use the read method without parameters. The code segment in Table A reads text data from A file, one character at A time, and then writes it into System. out.

To read the specified number of bytes from the input stream to the char array, you only need to use the read method with a char [] parameter. The length of the array is used to determine the number of characters to be read. Table B demonstrates this technology.

To disable an input stream and all system resources used by the stream, you only need to wait for the following content...>

Input and Output in java

Int a, B, c;
S = new partition (System. in ());
A = s. nextInt ();
B = s. nextInt ();
C = a + B;
System. out. println (a + "+" + B + "=" + c );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.