Tag: Its delimiter succeeds scanner basic representation of the write file input stream tput
Scanner class
Java.util.Scanner is a new feature of JAVA5, and we can get the user's input through the Scanner class.
Get the input string by the next () and Nextline () method of the Scanner class
Example:
import java.util.Scanner;public class ScannerDemo {public static void main(String[] args) {Scanner scan = new Scanner(System.in);// 从键盘接收数据//next方式接收字符串System.out.println("next方式接收:");// 判断是否还有输入if(scan.hasNext()){String str1 = scan.next();System.out.println("输入的数据为:"+str1);}}}
Next () differs from nextline ()
Next ():
1. Be sure to read the valid characters before you can end the input.
2. The next () method will automatically remove the whitespace that is encountered before entering a valid character.
3. Enter only the valid characters before entering the blanks as delimiters or terminators.
Next () cannot get a string with a space.
Nextline ():
1. Enter as the Terminator, meaning that the nextline () method returns all characters before the input carriage return.
2, you can get blank.
Stream File IO class
The java.io package contains almost all the required classes for operation input and output. All of these flow classes represent the input source and output destination.
The streams in the Java.io package support many formats, such as basic types, objects, localized character sets, and so on.
A stream can be understood as a sequence of data. The input stream represents reading data from one source, and the output stream represents writing data to a target.
Java provides powerful and flexible support for I/O, making it more widely available in file transfer and network programming.
Read input from console
Java console input is completed by system.in.
BufferedReader br = new BufferedReader (new InputStreamReader (system.in));
Use the Read () method to read a character from the console, or to read a string with the ReadLine () method.
Example:
// 使用 BufferedReader 在控制台读取字符import java.io.*;public class BRRead {public static void main(String args[]) throws IOException{char c;// 使用 System.in 创建 BufferedReaderBufferedReader br = new BufferedReader(newInputStreamReader(System.in));System.out.println("输入字符, 按下 ‘q‘ 键退出。");// 读取字符do {c = (char) br.read();System.out.println(c);} while(c != ‘q‘);}}
Console output
The output of the console is done by print () and println (). These methods are defined by the class PrintStream, and System.out is a reference to the class object.
Read and write files
FileInputStream
The stream is used to read data from a file, and its objects can be created with the keyword new.
File F = new file ("C:/java/hello");
InputStream out = new FileInputStream (f);
When you create a InputStream object, you can use the following methods to read the stream or perform other flow operations.
FileOutputStream
This class is used to create a file and write data to the file.
If the stream does not exist before the file is opened for output, the stream creates the file.
Once the OutputStream object is created, you can use the following methods to write to the stream or perform other flow operations.
Directories in Java
To create a directory:
There are two methods in the file class that can be used to create a folder:
The MkDir () method creates a folder, returns True if successful, and returns False if it fails. Failure indicates that the path specified by the file object already exists, or that the folder cannot be created because the entire path does not yet exist.
The Mkdirs () method creates a folder and all its parent folders.
Read Directory
A directory is actually a file object that contains other files and folders.
If you create a File object and it is a directory, then calling the Isdirectory () method returns True.
You can extract a list of the files and folders it contains by calling the list () method on the object.
import java.io.File;public class DeleteFileDemo {public static void main(String args[]) {// 这里修改为自己的测试目录File folder = new File("/tmp/java/");deleteFolder(folder);}//删除文件及目录public static void deleteFolder(File folder) {File[] files = folder.listFiles();if(files!=null) {for(File f: files) {if(f.isDirectory()) {deleteFolder(f);} else {f.delete();} }}folder.delete();}}
Java Basics-Common tool classes (ii)