当前位置:网站首页>Stream, file, IO
Stream, file, IO
2022-07-24 17:45:00 【Bobo toilet cleaning spirit】
Read console input characters
Java The console input of is provided by System.in complete .
To get a character stream bound to the console , You can take System.in Packed in a BufferedReader Object to create a character stream .
public static void main(String args[]) throws IOException
{
char c;
// Use System.in establish BufferedReader
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.println(" The input character , Press down 'q' Key to exit .");
// Read character
do {
c = (char) br.read();
System.out.println(c);
} while(c != 'q');
}// Use BufferedReader Read characters on the console
import java.io.*;
public class BRReadLines {
public static void main(String args[]) throws IOException
{
// Use System.in establish BufferedReader
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
String str;
System.out.println("Enter lines of text.");
System.out.println("Enter 'end' to quit.");
do {
str = br.readLine();
System.out.println(str);
} while(!str.equals("end"));
}
}Console output
The output of the console is controlled by print() and println() complete . These methods are all made up of classes PrintStream Definition ,System.out Is a reference to the class object .
PrintStream Inherited OutputStream class
Read and write files
Java flow (Stream)、 file (File) and IO_w3cschool
Scanner class
java.util.Scanner yes Java5 New features , We can go through Scanner Class to get user input .
adopt Scanner Class next() And nextLine() Method to get the input string , Understand the difference between the two methods
import java.util.Scanner;
public class ScannerDemo {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// Receive data from keyboard
//next Method receive string
System.out.println("next Mode reception :");
// Judge whether there is any input
if(scan.hasNext()){
String str1 = scan.next();
System.out.println(" The data entered is :"+str1);
}
}
} Output results

The character after the space is not output
import java.util.Scanner;
public class ScannerDemo {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// Receive data from keyboard
//nextLine Method receive string
System.out.println("nextLine Mode reception :");
// Judge whether there is any input
if(scan.hasNextLine()){
String str2 = scan.nextLine();
System.out.println(" The data entered is :"+str2);
}
}
} 
next() And nextLine() difference
next():
- 1、 You must read valid characters before you can end the input .
- 2、 Whitespace encountered before entering valid characters for ,next() Method will automatically remove it .
- 3、 The space entered after a valid character is used as a separator or terminator only after it is entered .
- next() Cannot get string with space .
nextLine():
- 1、 With Enter End of character , in other words nextLine() Method returns all the characters before enter .
- 2、 Can get blank .
If you want to enter int or float Data of type , stay Scanner Class is also supported , But it's better to use it before entering hasNextXxx() Method validation , Reuse nextXxx() To read :
import java.util.Scanner;
public class ScannerDemo {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// Receive data from keyboard
int i = 0 ;
float f = 0.0f ;
System.out.print(" Input integer :");
if(scan.hasNextInt()){
// Judge whether the input is an integer
i = scan.nextInt() ;
// Receive integer
System.out.println(" Integer data :" + i) ;
}else{
// Enter the wrong information
System.out.println(" Input is not an integer !") ;
}
System.out.print(" Input decimal :");
if(scan.hasNextFloat()){
// Determine whether the input is a decimal
f = scan.nextFloat() ;
// Receive decimal
System.out.println(" Fractional data :" + f) ;
}else{
// Enter the wrong information
System.out.println(" The input is not a decimal !") ;
}
}
}
边栏推荐
- List of stringutils and string methods
- 如何遵循“低耦合”设计原则?
- [network security] analysis vulnerability of website Middleware
- mac数据库管理软件Navicat Premium Essentials Mac
- C language to achieve a static version of the address book
- 仅需一个依赖给Swagger换上新皮肤,既简单又炫酷!
- awk从入门到入土(19)awk扩展插件,让awk如虎添翼
- SV casts and constants
- In the morning, Tencent took out 38K, which let me see the ceiling of the foundation
- Huawei machine test - topic core test point
猜你喜欢

es(1)
Link editing tips of solo blog posts illegal links

Eth POS 2.0 stacking test network pledge process

Analog electricity - what is the resistance?

C语言自定义类型讲解 — 结构体

Pay close attention! List of the latest agenda of 2022 open atom open source Summit

Heuristic merging (including examples of general formula and tree heuristic merging)

C语言自定义类型讲解 — 联合体

2022 Niuke summer multi school K - link with bracket sequence I (linear DP)

Trends of semiconductor industry
随机推荐
阿里巴巴/166获得店铺的所有商品 API使用说明
面会菜评论分析
Ipaylinks, a cross-border payment integration service, won the 3A Asia Award of treasury
Today, I met a 38K from Tencent, which let me see the ceiling of the foundation
Development Series III of GaN (lapgan, srgan)
Six ways for JS to implement inheritance
生信常用分析图形绘制02 -- 解锁火山图真谛!
JS image conversion Base64 Base64 conversion to file object
What are the pitfalls from single architecture to distributed architecture?
C语言自定义类型讲解 — 联合体
Definition and storage of adjacency table and adjacency storage of directed graph and undirected graph
Hcip fourth day notes
Baidu PaddlePaddle easydl x wesken: see how to install the "eye of AI" in bearing quality inspection
213. 打家劫舍 II-动态规划
如何遵循“低耦合”设计原则?
Array learning navigation
简单测试JS代码
es(1)
Shengxin commonly used analysis graphics drawing 02 -- unlock the essence of volcano map!
es(1)