java console类_Java中的Java.io.Console类 - Break易站

Java I/O总结

Java中的Java.io.Console类

Java.io.Console类提供了访问与当前Java虚拟机关联的基于字符的控制台console设备(如果有的话)的方法。Console类由JDK 6添加到java.io中。

重点:

它用于读取和写入控制台(如果存在)。

控制台主要是一个便利的类,因为它的大部分功能都可以通过System.in和System.out获得。但是,它的使用可以简化某些类型的控制台交互,尤其是在从控制台读取字符串时。

控制台不提供构造函数。而是通过调用System.console()来获取Console对象,如下所示:

static Console console( )

如果控制台可用,则返回对其的引用。否则,返回null。控制台在所有情况下都不可用。因此,如果返回null,则不可能有控制台I / O。

它提供读取文本和密码的方法。如果使用Console类读取密码,则不会向用户显示。java.io.Console类在内部与系统控制台相连。

重要方法:

编写器: 检索与此控制台关联的唯一PrintWriter对象。

句法:

public PrintWriter writer()

Returns: The printwriter associated with this console

reader: 检索与此控制台关联的唯一Reader对象。

句法:

public Reader reader()

Returns:The reader associated with this console

格式:使用指定的格式字符串和参数将格式化的字符串写入此控制台的输出流。

句法:

public Console format(String fmt, Object... args)

Parameters:

fmt - A format string as described in Format string syntax

args - Arguments referenced by the format specifiers in the format string.

If there are more arguments than format specifiers, the extra arguments are ignored.

Returns:This console

Throws:IllegalFormatException

printf:使用指定的格式字符串和参数将格式化的字符串写入此控制台的输出流的便捷方法。

句法:

public Console printf(String format, Object... args)Parameters:

format - A format string as described in Format string syntax.

args - Arguments referenced by the format specifiers in the format string.

If there are more arguments than format specifiers, the extra arguments are ignored.

Returns:This console

Throws:IllegalFormatException

readLine:提供格式化的提示,然后从控制台读取一行文本。

句法:

public String readLine(String fmt,Object... args)

Parameters:

fmt - A format string as described in Format string syntax.

args - Arguments referenced by the format specifiers in the format string.

If there are more arguments than format specifiers, the extra arguments are ignored.

Returns:A string containing the line read from the console,

not including any line-termination characters, or null

if an end of stream has been reached.

Throws:IllegalFormatException

IOError - If an I/O error occurs.

readLine:从控制台读取一行文本。

句法:

public String readLine()

Returns: A string containing the line read from the console,

not including any line-termination characters, or null

if an end of stream has been reached.

Throws:IOError

readPassword:提供格式化的提示,然后从控制台读取禁用回显的密码或密码。

句法:

public char[] readPassword(String fmt,Object... args)

Parameters:

fmt - A format string as described in Format string syntax for the prompt text.

args - Arguments referenced by the format specifiers in the format string.

Returns: A character array containing the password or passphrase read

from the console, not including any line-termination characters, or null

if an end of stream has been reached.

Throws:IllegalFormatException

IOError

readPassword:从禁用回显的控制台读取密码或密码

语法:

public char[] readPassword()Returns: A character array containing the password or passphrase

read from the console, not including any line-termination characters, or null

if an end of stream has been reached.

Throws:IOError

flush: 刷新控制台并强制任何缓冲输出立即写入。

句法:

public void flush()Specified by: flush in interface Flushable

程序:

// Java Program to demonstrate Console Methods

import java.io.*;

class ConsoleDemo

{

public static void main(String args[])

{

String str;

//Obtaining a reference to the console.

Console con = System.console();

// Checking If there is no console available, then exit.

if(con == null)

{

System.out.print("No console available");

return;

}

// Read a string and then display it.

str = con.readLine("Enter your name: ");

con.printf("Here is your name: %s\n", str);

//to read password and then display it

System.out.println("Enter the password: ");

char[] ch=con.readPassword();

//converting char array into string

String pass = String.valueOf(ch);

System.out.println("Password is: " + pass);

}

}

输出:

Enter your name: Nishant Sharma

Here is your name: Nishant Sharma

Enter the password:

Password is: dada

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SHA-512是一种哈希函数,它将输入数据转换为长度为512位的哈希值。在Java,可以使用Java.security.MessageDigest来执行SHA-512哈希加密。 以下是一个示例代码,演示如何在Java使用SHA-512哈希加密: ```java import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class SHA512Example { public static void main(String[] args) { String passwordToHash = "mypassword"; String generatedPassword = null; try { MessageDigest md = MessageDigest.getInstance("SHA-512"); md.update(passwordToHash.getBytes()); byte[] bytes = md.digest(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < bytes.length; i++) { sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1)); } generatedPassword = sb.toString(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } System.out.println(generatedPassword); } } ``` 在这个例子,我们使用"mypassword"字符串作为输入数据。我们首先获取一个MessageDigest实例,然后使用update()方法更新摘要信息。最后,我们使用digest()方法计算哈希值,并将其转换为十六进制字符串形式。 运行该程序,将输出以下SHA-512哈希加密值: ``` 8b5d8e6e7f8b1a1e2e9b6b5b2c4a5eeb9e251e4c3c43c54d6e4b4d0a1c29f3d1cf16e7ee1e1b2e5b5e198d6b6b7e5f9e6ebe1d2d5d6e8f1e9e6e6e7f3f4f5e4c4 ``` 请注意,SHA-512哈希加密是不可逆的,这意味着无法从哈希值恢复原始数据。因此,SHA-512通常用于存储密码等敏感信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值