📜  Java中的 DigestInputStream.toString() 方法及示例

📅  最后修改于: 2022-05-13 01:55:26.213000             🧑  作者: Mango

Java中的 DigestInputStream.toString() 方法及示例

Java.security.DigestInputStream类的toString()方法提供了字符串格式的 DigestInputStream 对象。
句法:

public String toString()

返回值:该方法以字符串形式返回 DigestInputStream 的对象。
注意:本文中的所有程序都不会在在线 IDE 上运行,因为不存在“名称”文件。您可以在系统上的Java编译器上检查此代码。要检查此代码,请在您的系统上创建一个文件“名称”。
下面是说明toString()方法的示例:
示例 1:

Java
// Java program to demonstrate
// toString() method
 
import java.security.*;
import java.util.*;
import java.io.*;
 
public class GFG {
    public static void main(String[] argv)
    {
        try {
            // creating the object of MessageDigest
            // and getting instance
            // By using getInstance() method
            MessageDigest sr
                = MessageDigest.getInstance("MD5");
 
            // creating and initializing
            // object of InputStream
            InputStream is
                = new FileInputStream("f:/java/name.txt");
 
            // creating and initializing
            // object of DigestInputStream
            DigestInputStream di
                = new DigestInputStream(is, sr);
 
            // getting the string representation
            // of DigestInputStream
            // using toString() method
            String str = di.toString();
 
            // display the result
            System.out.println("Status : " + str);
        }
 
        catch (NoSuchAlgorithmException e) {
 
            System.out.println("Exception thrown : " + e);
        }
        catch (NullPointerException e) {
 
            System.out.println("Exception thrown : " + e);
        }
        catch (FileNotFoundException e) {
 
            System.out.println("Exception thrown : " + e);
        }
    }
}


Java
// Java program to demonstrate
// toString() method
 
import java.security.*;
import java.util.*;
import java.io.*;
 
public class GFG {
    public static void main(String[] argv)
    {
        try {
            // creating the object of MessageDigest
            // and getting instance
            // By using getInstance() method
            MessageDigest sr
                = MessageDigest.getInstance("SHA-1");
 
            // creating and initializing
            // object of InputStream
            InputStream is
                = new FileInputStream("f:/java/name.txt");
 
            // creating and initializing
            // object of DigestInputStream
            DigestInputStream di
                = new DigestInputStream(is, sr);
 
            // getting the string
            // representation of DigestInputStream
            // using toString() method
            String str = di.toString();
 
            // display the result
            System.out.println("Status : " + str);
        }
 
        catch (NoSuchAlgorithmException e) {
 
            System.out.println("Exception thrown : " + e);
        }
        catch (NullPointerException e) {
 
            System.out.println("Exception thrown : " + e);
        }
        catch (FileNotFoundException e) {
 
            System.out.println("Exception thrown : " + e);
        }
    }
}


输出:
Status : [Digest Input Stream] MD5 Message Digest from SUN, 

示例 2:

Java

// Java program to demonstrate
// toString() method
 
import java.security.*;
import java.util.*;
import java.io.*;
 
public class GFG {
    public static void main(String[] argv)
    {
        try {
            // creating the object of MessageDigest
            // and getting instance
            // By using getInstance() method
            MessageDigest sr
                = MessageDigest.getInstance("SHA-1");
 
            // creating and initializing
            // object of InputStream
            InputStream is
                = new FileInputStream("f:/java/name.txt");
 
            // creating and initializing
            // object of DigestInputStream
            DigestInputStream di
                = new DigestInputStream(is, sr);
 
            // getting the string
            // representation of DigestInputStream
            // using toString() method
            String str = di.toString();
 
            // display the result
            System.out.println("Status : " + str);
        }
 
        catch (NoSuchAlgorithmException e) {
 
            System.out.println("Exception thrown : " + e);
        }
        catch (NullPointerException e) {
 
            System.out.println("Exception thrown : " + e);
        }
        catch (FileNotFoundException e) {
 
            System.out.println("Exception thrown : " + e);
        }
    }
}
输出:
Status : [Digest Input Stream] SHA-1 Message Digest from SUN, 

参考: https://docs.oracle.com/javase/9/docs/api/ Java/security/DigestInputStream.html#toString–