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;
}
}
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();
}
}
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;
}
}
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);
}
}
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");
}
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有