Java 字符終端上獲得輸出三種的方法分享。本站提示廣大學習愛好者:(Java 字符終端上獲得輸出三種的方法分享)文章只能為提供參考,不一定能成為您想要的結果。以下是Java 字符終端上獲得輸出三種的方法分享正文
在Java 字符終端上獲得輸出有三種方法:
1、java.lang.System.in (今朝JDK版本均支撐)
2、java.util.Scanner (JDK版本>=1.5)
3、java.io.Console(JDK版本>=1.6),特點:能不回顯暗碼字符
參考:
這裡記載Java中從掌握台讀入信息的幾種方法
(1)JDK 1.4(JDK 1.5和JDK 1.6也都兼容這類辦法)
public class TestConsole1 {
public static void main(String[] args) {
String str = readDataFromConsole("Please input string:);
System.out.println("The information from console: + str);
}
/**
* Use InputStreamReader and System.in to read data from console
*
* @param prompt
*
* @return input string
*/
private static String readDataFromConsole(String prompt) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = null;
try {
System.out.print(prompt);
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
(2)JDK 1.5(應用Scanner停止讀取)
public class TestConsole2 {
public static void main(String[] args) {
String str = readDataFromConsole("Please input string:");
System.out.println("The information from console:" + str);
}
/**
* Use java.util.Scanner to read data from console
*
* @param prompt
*
* @return input string
*/
private static String readDataFromConsole(String prompt) {
Scanner scanner = new Scanner(System.in);
System.out.print(prompt);
return scanner.nextLine();
}
}
Scanner還可以很便利的掃描文件,讀取外面的信息並轉換成你要的類型,好比對“2 2.2 3.3 3.33 4.5 done”如許的數據乞降,見以下代碼:
public class TestConsole4 {
public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter("num.txt");
fw.write("2 2.2 3.3 3.33 4.5 done");
fw.close();
System.out.println("Sum is "+scanFileForSum("num.txt"));
}
public static double scanFileForSum(String fileName) throws IOException {
double sum = 0.0;
FileReader fr = null;
try {
fr = new FileReader(fileName);
Scanner scanner = new Scanner(fr);
while (scanner.hasNext()) {
if (scanner.hasNextDouble()) {
sum = sum + scanner.nextDouble();
} else {
String str = scanner.next();
if (str.equals("done")) {
break;
} else {
throw new RuntimeException("File Format is wrong!");
}
}
}
} catch (FileNotFoundException e) {
throw new RuntimeException("File " + fileName + " not found!");
} finally {
if (fr != null)
fr.close();
}
return sum;
}
}
(3)JDK 1.6(應用java.io.Console停止讀取)
JDK6中供給了java.io.Console類公用來拜訪基於字符的掌握台裝備.
你的法式假如要與Windows下的cmd或許Linux下的Terminal交互,便可以用Console類代庖.(相似System.in和System.out)
但我們不老是能獲得可用的Console, 一個JVM能否有可用的Console依附於底層平台和JVM若何被挪用.
假如JVM是在交互式敕令行(好比Windows的cmd)中啟動的,而且輸出輸入沒有重定向到別的的處所,那末便可以獲得一個可用的Console實例。
在應用 IDE 的情形下,是沒法獲得到Console實例的,緣由在於在 IDE 的情況下,從新定向了尺度輸出和輸入流,也是就是將體系掌握台上的輸出輸入重定向到了 IDE 的掌握台中
public class TestConsole3 {
public static void main(String[] args) {
String str = readDataFromConsole("Please input string:");
System.out.println("The information from console:" + str);
}
/**
* Use java.io.console to read data from console
*
* @param prompt
*
* @return input string
*/
private static String readDataFromConsole(String prompt) {
Console console = System.console();
if (console == null) {
throw new IllegalStateException("Console is not available!");
}
return console.readLine(prompt);
}
}
Console類還有個特點就是,專門對暗碼(輸出無回顯)等平安字符,停止了處置。專門供給 readPassword()辦法,詳細運用見以下代碼:
public class TestConsole5 {
public static void main(String[] args) {
Console console = System.console();
if (console == null) {
throw new IllegalStateException("Console is not available!");
}
while(true){
String username = console.readLine("Username: ");
char[] password = console.readPassword("Password: ");
if (username.equals("Chris") && String.valueOf(password).equals("GoHead")) {
console.printf("Welcome to Java Application %1$s.\n", username);
// 應用後應立刻將數組清空,以削減其在內存中占用的時光,加強平安性
password = null;
System.exit(-1);
}
else {
console.printf("Invalid username or password.\n");
}
}
}
}