>  기사  >  Java  >  try에서 리소스 파일과 메모리를 사용하는 방법

try에서 리소스 파일과 메모리를 사용하는 방법

Mary-Kate Olsen
Mary-Kate Olsen원래의
2024-10-24 06:09:02996검색

비준수 How to use resources file and memory in try

아래 자바 코드 예시

준수


try 블록에서 여러 리소스를 정의할 수 있음을 알려드립니다
public class PasswordProtectedPDF {
    private static final Logger logger = Logger.getLogger(PasswordProtectedPDF.class.getName());
    static final String USER_PASSWORD = "111";
    static final String OWNER_PASSWORD = "111";

    public static void main(String[] args) {
        try {
            File f = new File("1_protected.pdf");
            FileOutputStream out = new FileOutputStream(f);
            File pdfFile = new File("1.pdf");
            PdfReader reader = new PdfReader(pdfFile.getPath());
            PdfStamper stamper = new PdfStamper(reader, out);

            HashMap<String, String> info = new HashMap<>();
            info.put("Producer", "");
            reader.getInfo().forEach((key, value) -> {
                logger.info("Key: " + key + ", Value: " + value);
            });
            stamper.setInfoDictionary(info);
            stamper.setEncryption(USER_PASSWORD.getBytes(), OWNER_PASSWORD.getBytes(), PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128);

            stamper.close();
            logger.info("Password protected PDF created successfully.");
        } catch (IOException e) {
            logger.severe("Error creating password protected PDF: " + e.getMessage());
        }
    }
}

public class PasswordProtectedPDF {
    private static final Logger logger = Logger.getLogger(PasswordProtectedPDF.class.getName());
    static final String USER_PASSWORD = "111";
    static final String OWNER_PASSWORD = "111";

    public static void main(String[] args) {
        try (
                FileOutputStream out = new FileOutputStream(new File("1_protected.pdf"));
                PdfReader reader = new PdfReader(new File("1.pdf").getPath())
        ) {
            PdfStamper stamper = new PdfStamper(reader, out);

            HashMap<String, String> info = new HashMap<>();
            info.put("Producer", "");
            reader.getInfo().forEach((key, value) -> {
                logger.info("Key: " + key + ", Value: " + value);
            });
            stamper.setInfoDictionary(info);
            stamper.setEncryption(USER_PASSWORD.getBytes(), OWNER_PASSWORD.getBytes(), PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128);

            stamper.close();
            logger.info("Password protected PDF created successfully.");
        } catch (IOException e) {
            logger.severe("Error creating password protected PDF: " + e.getMessage());
        }
    }
}

여기에서 전체 작업 예제를 찾아보세요


위 내용은 try에서 리소스 파일과 메모리를 사용하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.